6
6
using UnityEngine ;
7
7
using UnityEngine . UI ;
8
8
using TMPro ;
9
+ using Object = UnityEngine . Object ;
9
10
10
11
namespace Coffee . UIEffects
11
12
{
@@ -54,9 +55,9 @@ public override void OnPreModifyMesh(Graphic graphic)
54
55
55
56
public override void SetVerticesDirty ( Graphic graphic )
56
57
{
57
- if ( graphic is TextMeshProUGUI textMeshProUGUI )
58
+ if ( graphic is TextMeshProUGUI textMeshProUGUI && textMeshProUGUI . isActiveAndEnabled )
58
59
{
59
- s_ChangedInstances . Add ( textMeshProUGUI ) ;
60
+ OnChangeText ( textMeshProUGUI ) ;
60
61
}
61
62
}
62
63
@@ -107,11 +108,13 @@ static Vector2 UnpackUV(float input)
107
108
108
109
#if UNITY_EDITOR
109
110
[ InitializeOnLoadMethod ]
110
- #else
111
- [ RuntimeInitializeOnLoadMethod ( RuntimeInitializeLoadType . BeforeSceneLoad ) ]
112
111
#endif
112
+ [ RuntimeInitializeOnLoadMethod ( RuntimeInitializeLoadType . BeforeSceneLoad ) ]
113
113
private static void InitializeOnLoad ( )
114
114
{
115
+ s_ChangedInstances . Clear ( ) ;
116
+ s_RegisteredInstances . Clear ( ) ;
117
+ s_SdfScaleCache . Clear ( ) ;
115
118
Register ( new TmpProxy ( ) ) ;
116
119
117
120
// When the text is changed, add it to the changed list
@@ -129,16 +132,15 @@ private static void InitializeOnLoad()
129
132
var toRemove = InternalListPool < TextMeshProUGUI > . Rent ( ) ;
130
133
foreach ( var textMeshProUGUI in s_RegisteredInstances )
131
134
{
132
- if ( textMeshProUGUI )
135
+ if ( textMeshProUGUI && textMeshProUGUI . isActiveAndEnabled )
133
136
{
134
137
var id = textMeshProUGUI . GetHashCode ( ) ;
135
138
var lossyScaleY = textMeshProUGUI . transform . lossyScale . y ;
136
139
137
140
// If the scale has changed, add to the changed list
138
- if ( s_SdfScaleCache . TryGetValue ( id , out var prev )
139
- && ! Mathf . Approximately ( prev , lossyScaleY ) )
141
+ if ( s_SdfScaleCache . TryGetValue ( id , out var prev ) && ! Mathf . Approximately ( prev , lossyScaleY ) )
140
142
{
141
- s_ChangedInstances . Add ( textMeshProUGUI ) ;
143
+ OnChangeText ( textMeshProUGUI ) ;
142
144
}
143
145
144
146
// Update the scale cache
@@ -157,6 +159,8 @@ private static void InitializeOnLoad()
157
159
s_SdfScaleCache . Remove ( textMeshProUGUI . GetHashCode ( ) ) ;
158
160
s_RegisteredInstances . Remove ( textMeshProUGUI ) ;
159
161
}
162
+
163
+ InternalListPool < TextMeshProUGUI > . Return ( ref toRemove ) ;
160
164
} ;
161
165
162
166
// Modify the changed TMP mesh
@@ -172,6 +176,14 @@ private static void InitializeOnLoad()
172
176
} ;
173
177
}
174
178
179
+ private static void OnChangeText ( Object obj )
180
+ {
181
+ if ( obj is TextMeshProUGUI textMeshProUGUI && textMeshProUGUI . isActiveAndEnabled )
182
+ {
183
+ s_ChangedInstances . Add ( textMeshProUGUI ) ;
184
+ }
185
+ }
186
+
175
187
private static void ModifyMesh ( TextMeshProUGUI textMeshProUGUI )
176
188
{
177
189
if ( ! textMeshProUGUI . TryGetComponent < UIEffectBase > ( out var effect ) ) return ;
0 commit comments