1
+ using System ;
1
2
using System . Runtime . CompilerServices ;
2
3
using Coffee . UIEffectInternal ;
3
4
using UnityEngine ;
@@ -21,6 +22,7 @@ public abstract class UIEffectBase : UIBehaviour, IMeshModifier, IMaterialModifi
21
22
private Graphic _graphic ;
22
23
private Material _material ;
23
24
private UIEffectContext _context ;
25
+ private Action _setVerticesDirtyIfVisible ;
24
26
25
27
public Graphic graphic => _graphic ? _graphic : _graphic = GetComponent < Graphic > ( ) ;
26
28
public virtual uint effectId => ( uint ) GetInstanceID ( ) ;
@@ -52,13 +54,20 @@ protected override void OnEnable()
52
54
53
55
protected override void OnDisable ( )
54
56
{
57
+ if ( _setVerticesDirtyIfVisible != null )
58
+ {
59
+ UIExtraCallbacks . onBeforeCanvasRebuild -= _setVerticesDirtyIfVisible ;
60
+ }
61
+
55
62
MaterialRepository . Release ( ref _material ) ;
56
63
SetMaterialDirty ( ) ;
57
64
SetVerticesDirty ( ) ;
58
65
}
59
66
60
67
protected override void OnDestroy ( )
61
68
{
69
+ _setVerticesDirtyIfVisible = null ;
70
+ _graphic = null ;
62
71
s_ContextPool . Return ( ref _context ) ;
63
72
}
64
73
@@ -70,7 +79,27 @@ public virtual void ModifyMesh(VertexHelper vh)
70
79
{
71
80
if ( ! isActiveAndEnabled || context == null ) return ;
72
81
73
- context . ModifyMesh ( graphic , transitionRoot , vh , canModifyShape ) ;
82
+ if ( CanModifyMesh ( ) )
83
+ {
84
+ context . ModifyMesh ( graphic , transitionRoot , vh , canModifyShape ) ;
85
+ }
86
+ // If you can't modify the mesh, retry next frame.
87
+ else
88
+ {
89
+ UIExtraCallbacks . onBeforeCanvasRebuild += _setVerticesDirtyIfVisible ??= SetVerticesDirtyIfVisible ;
90
+ }
91
+ }
92
+
93
+ private bool CanModifyMesh ( )
94
+ {
95
+ // The transitionRoot is same as the transform => true.
96
+ var root = transitionRoot ;
97
+ if ( transform == root ) return true ;
98
+
99
+ // The transitionRoot is not visible => false.
100
+ var scale1 = root . lossyScale ;
101
+ var scale2 = transform . lossyScale ;
102
+ return ! Mathf . Approximately ( scale1 . x * scale1 . y * scale2 . x * scale2 . y , 0 ) ;
74
103
}
75
104
76
105
public virtual Material GetModifiedMaterial ( Material baseMaterial )
@@ -148,6 +177,15 @@ public virtual void SetVerticesDirty()
148
177
}
149
178
}
150
179
180
+ private void SetVerticesDirtyIfVisible ( )
181
+ {
182
+ if ( CanModifyMesh ( ) )
183
+ {
184
+ UIExtraCallbacks . onBeforeCanvasRebuild -= _setVerticesDirtyIfVisible ??= SetVerticesDirtyIfVisible ;
185
+ SetVerticesDirty ( ) ;
186
+ }
187
+ }
188
+
151
189
public virtual void SetMaterialDirty ( )
152
190
{
153
191
if ( graphic )
0 commit comments