Skip to content

Commit 365c43e

Browse files
committed
fix: UIEffect v5.3.3 with TextMeshPro is not working in play mode in editor
close #306
1 parent 31b1632 commit 365c43e

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

Packages/src/Runtime/Utilities/TmpProxy.cs

+20-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using UnityEngine;
77
using UnityEngine.UI;
88
using TMPro;
9+
using Object = UnityEngine.Object;
910

1011
namespace Coffee.UIEffects
1112
{
@@ -54,9 +55,9 @@ public override void OnPreModifyMesh(Graphic graphic)
5455

5556
public override void SetVerticesDirty(Graphic graphic)
5657
{
57-
if (graphic is TextMeshProUGUI textMeshProUGUI)
58+
if (graphic is TextMeshProUGUI textMeshProUGUI && textMeshProUGUI.isActiveAndEnabled)
5859
{
59-
s_ChangedInstances.Add(textMeshProUGUI);
60+
OnChangeText(textMeshProUGUI);
6061
}
6162
}
6263

@@ -107,11 +108,13 @@ static Vector2 UnpackUV(float input)
107108

108109
#if UNITY_EDITOR
109110
[InitializeOnLoadMethod]
110-
#else
111-
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
112111
#endif
112+
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
113113
private static void InitializeOnLoad()
114114
{
115+
s_ChangedInstances.Clear();
116+
s_RegisteredInstances.Clear();
117+
s_SdfScaleCache.Clear();
115118
Register(new TmpProxy());
116119

117120
// When the text is changed, add it to the changed list
@@ -129,16 +132,15 @@ private static void InitializeOnLoad()
129132
var toRemove = InternalListPool<TextMeshProUGUI>.Rent();
130133
foreach (var textMeshProUGUI in s_RegisteredInstances)
131134
{
132-
if (textMeshProUGUI)
135+
if (textMeshProUGUI && textMeshProUGUI.isActiveAndEnabled)
133136
{
134137
var id = textMeshProUGUI.GetHashCode();
135138
var lossyScaleY = textMeshProUGUI.transform.lossyScale.y;
136139

137140
// 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))
140142
{
141-
s_ChangedInstances.Add(textMeshProUGUI);
143+
OnChangeText(textMeshProUGUI);
142144
}
143145

144146
// Update the scale cache
@@ -157,6 +159,8 @@ private static void InitializeOnLoad()
157159
s_SdfScaleCache.Remove(textMeshProUGUI.GetHashCode());
158160
s_RegisteredInstances.Remove(textMeshProUGUI);
159161
}
162+
163+
InternalListPool<TextMeshProUGUI>.Return(ref toRemove);
160164
};
161165

162166
// Modify the changed TMP mesh
@@ -172,6 +176,14 @@ private static void InitializeOnLoad()
172176
};
173177
}
174178

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+
175187
private static void ModifyMesh(TextMeshProUGUI textMeshProUGUI)
176188
{
177189
if (!textMeshProUGUI.TryGetComponent<UIEffectBase>(out var effect)) return;

0 commit comments

Comments
 (0)