Skip to content

Commit 7529a7c

Browse files
committed
fix: when modifying TextMeshPro vertices, element disapear
close #305
1 parent c51b5f3 commit 7529a7c

7 files changed

+365
-334
lines changed

Packages/src/Runtime/UIEffectBase.cs

+1-152
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
using System.Collections.Generic;
21
using System.Runtime.CompilerServices;
32
using Coffee.UIEffectInternal;
43
using UnityEngine;
54
using UnityEngine.EventSystems;
65
using UnityEngine.Profiling;
76
using UnityEngine.UI;
87
using UnityEditor;
9-
#if TMP_ENABLE
10-
using TMPro;
11-
#endif
128

139
[assembly: InternalsVisibleTo("UIEffect")]
1410
[assembly: InternalsVisibleTo("Coffee.UIEffect.Editor")]
@@ -19,9 +15,6 @@ namespace Coffee.UIEffects
1915
[DisallowMultipleComponent]
2016
public abstract class UIEffectBase : UIBehaviour, IMeshModifier, IMaterialModifier, ICanvasRaycastFilter
2117
{
22-
private static readonly VertexHelper s_VertexHelper = new VertexHelper();
23-
private static Mesh s_Mesh;
24-
2518
private static readonly InternalObjectPool<UIEffectContext> s_ContextPool =
2619
new InternalObjectPool<UIEffectContext>(() => new UIEffectContext(), x => true, x => x.Reset());
2720

@@ -52,27 +45,13 @@ public virtual UIEffectContext context
5245

5346
protected override void OnEnable()
5447
{
55-
#if TMP_ENABLE
56-
if (graphic is TextMeshProUGUI)
57-
{
58-
_prevLossyScaleY = transform.lossyScale.y;
59-
Canvas.willRenderCanvases += CheckSDFScaleForTMP;
60-
UIExtraCallbacks.onScreenSizeChanged += SetVerticesDirtyForTMP;
61-
}
62-
#endif
63-
6448
UpdateContext(context);
6549
SetMaterialDirty();
6650
SetVerticesDirty();
6751
}
6852

6953
protected override void OnDisable()
7054
{
71-
#if TMP_ENABLE
72-
Canvas.willRenderCanvases -= CheckSDFScaleForTMP;
73-
UIExtraCallbacks.onScreenSizeChanged -= SetVerticesDirtyForTMP;
74-
#endif
75-
7655
MaterialRepository.Release(ref _material);
7756
SetMaterialDirty();
7857
SetVerticesDirty();
@@ -159,24 +138,10 @@ protected override void OnDidApplyAnimationProperties()
159138

160139
public virtual void SetVerticesDirty()
161140
{
162-
#if TMP_ENABLE
163-
if (graphic is TextMeshProUGUI textMeshProUGUI && textMeshProUGUI.isActiveAndEnabled)
164-
{
165-
if (isActiveAndEnabled)
166-
{
167-
OnTMPChanged(textMeshProUGUI);
168-
}
169-
else if (0 < textMeshProUGUI.textInfo?.meshInfo?.Length
170-
&& 0 < textMeshProUGUI.textInfo.meshInfo[0].vertexCount)
171-
{
172-
textMeshProUGUI.UpdateVertexData();
173-
}
174-
}
175-
else
176-
#endif
177141
if (graphic)
178142
{
179143
graphic.SetVerticesDirty();
144+
GraphicProxy.Find(graphic).SetVerticesDirty(graphic);
180145
#if UNITY_EDITOR
181146
EditorApplication.QueuePlayerLoopUpdate();
182147
#endif
@@ -211,122 +176,6 @@ public virtual void ApplyContextToMaterial()
211176
#endif
212177
}
213178

214-
#if TMP_ENABLE
215-
#if UNITY_EDITOR
216-
[InitializeOnLoadMethod]
217-
#else
218-
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
219-
#endif
220-
private static void InitializeOnLoad()
221-
{
222-
TMPro_EventManager.TEXT_CHANGED_EVENT.Add(obj =>
223-
{
224-
if (obj is TextMeshProUGUI textMeshProUGUI)
225-
{
226-
OnTMPChanged(textMeshProUGUI);
227-
}
228-
});
229-
}
230-
231-
private static void OnTMPChanged(TextMeshProUGUI textMeshProUGUI)
232-
{
233-
if (!textMeshProUGUI.TryGetComponent<UIEffectBase>(out var effect)) return;
234-
if (!effect || !effect.isActiveAndEnabled) return;
235-
236-
if (!s_Mesh)
237-
{
238-
s_Mesh = new Mesh();
239-
s_Mesh.MarkDynamic();
240-
}
241-
242-
var target = effect is UIEffect uiEffect
243-
? uiEffect
244-
: effect is UIEffectReplica parentReplica
245-
? parentReplica.target
246-
: null;
247-
var subMeshes = InternalListPool<TMP_SubMeshUI>.Rent();
248-
var modifiers = InternalListPool<IMeshModifier>.Rent();
249-
textMeshProUGUI.GetComponentsInChildren(subMeshes, 1);
250-
for (var i = 0; i < textMeshProUGUI.textInfo.meshInfo.Length; i++)
251-
{
252-
var meshInfo = textMeshProUGUI.textInfo.meshInfo[i];
253-
s_VertexHelper.Clear();
254-
meshInfo.mesh.CopyTo(s_VertexHelper, meshInfo.vertexCount, meshInfo.vertexCount * 6 / 4);
255-
if (i == 0)
256-
{
257-
textMeshProUGUI.GetComponents(modifiers);
258-
foreach (var modifier in modifiers)
259-
{
260-
modifier.ModifyMesh(s_VertexHelper);
261-
}
262-
263-
s_VertexHelper.FillMesh(s_Mesh);
264-
textMeshProUGUI.canvasRenderer.SetMesh(s_Mesh);
265-
}
266-
else if (i - 1 < subMeshes.Count)
267-
{
268-
var subMeshUI = GetSubMeshUI(subMeshes, meshInfo.material, i - 1);
269-
if (!target || !subMeshUI) break;
270-
271-
var replica = subMeshUI.GetOrAddComponent<UIEffectReplica>();
272-
replica.target = target;
273-
274-
subMeshUI.GetComponents(modifiers);
275-
foreach (var modifier in modifiers)
276-
{
277-
modifier.ModifyMesh(s_VertexHelper);
278-
}
279-
280-
s_VertexHelper.FillMesh(s_Mesh);
281-
replica.ApplyContextToMaterial();
282-
subMeshUI.canvasRenderer.SetMesh(s_Mesh);
283-
}
284-
else
285-
{
286-
break;
287-
}
288-
}
289-
290-
InternalListPool<IMeshModifier>.Return(ref modifiers);
291-
InternalListPool<TMP_SubMeshUI>.Return(ref subMeshes);
292-
s_Mesh.Clear(false);
293-
}
294-
295-
private static TMP_SubMeshUI GetSubMeshUI(List<TMP_SubMeshUI> subMeshes, Material material, int start)
296-
{
297-
var count = subMeshes.Count;
298-
for (var j = 0; j < count; j++)
299-
{
300-
var s = subMeshes[(j + start + count) % count];
301-
if (s.sharedMaterial == material) return s;
302-
}
303-
304-
return null;
305-
}
306-
307-
private void SetVerticesDirtyForTMP()
308-
{
309-
if (graphic && graphic.isActiveAndEnabled)
310-
{
311-
graphic.SetVerticesDirty();
312-
}
313-
}
314-
315-
private void CheckSDFScaleForTMP()
316-
{
317-
var lossyScaleY = transform.lossyScale.y;
318-
if (Mathf.Approximately(_prevLossyScaleY, lossyScaleY)) return;
319-
320-
_prevLossyScaleY = lossyScaleY;
321-
if (graphic is TextMeshProUGUI textMeshProUGUI && graphic.isActiveAndEnabled)
322-
{
323-
OnTMPChanged(textMeshProUGUI);
324-
}
325-
}
326-
327-
private float _prevLossyScaleY;
328-
#endif
329-
330179
public abstract void SetRate(float rate, UIEffectTweener.CullingMask cullingMask);
331180
public abstract bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera);
332181
}

Packages/src/Runtime/UIEffectContext.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -434,9 +434,9 @@ private static void SetKeyword(Material material, string[] keywords, int index)
434434

435435
public void ModifyMesh(Graphic graphic, RectTransform transitionRoot, VertexHelper vh, bool canModifyShape)
436436
{
437-
var processor = ContextProcessor.FindProcessor(graphic);
438-
var isText = processor.IsText(graphic);
439-
processor.OnPreModifyMesh(graphic);
437+
var effectProxy = GraphicProxy.Find(graphic);
438+
var isText = effectProxy.IsText(graphic);
439+
effectProxy.OnPreModifyMesh(graphic);
440440

441441
var verts = s_WorkingVertices;
442442
var expandSize = GetExpandSize(canModifyShape);

Packages/src/Runtime/Utilities/ContextProcessor.cs

-178
This file was deleted.

0 commit comments

Comments
 (0)