Skip to content

Commit

Permalink
fix: UIEffect with TextMeshPro is not working in play mode in editor
Browse files Browse the repository at this point in the history
close #306
  • Loading branch information
mob-sakai committed Feb 9, 2025
1 parent 31b1632 commit ffda5cf
Showing 1 changed file with 36 additions and 17 deletions.
53 changes: 36 additions & 17 deletions Packages/src/Runtime/Utilities/TmpProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using Object = UnityEngine.Object;

namespace Coffee.UIEffects
{
Expand Down Expand Up @@ -54,7 +55,7 @@ public override void OnPreModifyMesh(Graphic graphic)

public override void SetVerticesDirty(Graphic graphic)
{
if (graphic is TextMeshProUGUI textMeshProUGUI)
if (graphic is TextMeshProUGUI textMeshProUGUI && textMeshProUGUI.isActiveAndEnabled)
{
s_ChangedInstances.Add(textMeshProUGUI);
}
Expand Down Expand Up @@ -107,36 +108,53 @@ static Vector2 UnpackUV(float input)

#if UNITY_EDITOR
[InitializeOnLoadMethod]
#else
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
#endif
private static void InitializeOnLoad()
{
if (!EditorApplication.isPlayingOrWillChangePlaymode)
{
RuntimeInitializeOnLoad();
}
}
#endif

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void RuntimeInitializeOnLoad()
{
Register(new TmpProxy());

// When the text is changed, add it to the changed list
TMPro_EventManager.TEXT_CHANGED_EVENT.Add(obj =>
TMPro_EventManager.TEXT_CHANGED_EVENT.Remove(OnChangeText);
TMPro_EventManager.TEXT_CHANGED_EVENT.Add(OnChangeText);

// Check SDF scale (lossyScale.y) change
UIExtraCallbacks.onBeforeCanvasRebuild -= CheckSdfScale;
UIExtraCallbacks.onBeforeCanvasRebuild += CheckSdfScale;

// Modify the changed TMP mesh
UIExtraCallbacks.onAfterCanvasRebuild -= ModifyMeshForChangedText;
UIExtraCallbacks.onAfterCanvasRebuild += ModifyMeshForChangedText;
return;

static void OnChangeText(Object obj)
{
if (obj is TextMeshProUGUI textMeshProUGUI)
if (obj is TextMeshProUGUI textMeshProUGUI && textMeshProUGUI.isActiveAndEnabled)
{
s_ChangedInstances.Add(textMeshProUGUI);
}
});
}

UIExtraCallbacks.onBeforeCanvasRebuild += () =>
static void CheckSdfScale()
{
// Check SDF scale (lossyScale.y) change
var toRemove = InternalListPool<TextMeshProUGUI>.Rent();
foreach (var textMeshProUGUI in s_RegisteredInstances)
{
if (textMeshProUGUI)
if (textMeshProUGUI && textMeshProUGUI.isActiveAndEnabled)
{
var id = textMeshProUGUI.GetHashCode();
var lossyScaleY = textMeshProUGUI.transform.lossyScale.y;

// If the scale has changed, add to the changed list
if (s_SdfScaleCache.TryGetValue(id, out var prev)
&& !Mathf.Approximately(prev, lossyScaleY))
if (s_SdfScaleCache.TryGetValue(id, out var prev) && !Mathf.Approximately(prev, lossyScaleY))
{
s_ChangedInstances.Add(textMeshProUGUI);
}
Expand All @@ -157,23 +175,24 @@ private static void InitializeOnLoad()
s_SdfScaleCache.Remove(textMeshProUGUI.GetHashCode());
s_RegisteredInstances.Remove(textMeshProUGUI);
}
};

// Modify the changed TMP mesh
UIExtraCallbacks.onAfterCanvasRebuild += () =>
InternalListPool<TextMeshProUGUI>.Return(ref toRemove);
}

static void ModifyMeshForChangedText()
{
foreach (var textMeshProUGUI in s_ChangedInstances)
{
if (!textMeshProUGUI || !textMeshProUGUI.isActiveAndEnabled) continue;
ModifyMesh(textMeshProUGUI);
}

s_ChangedInstances.Clear();
};
}
}

private static void ModifyMesh(TextMeshProUGUI textMeshProUGUI)
{
if (!textMeshProUGUI || !textMeshProUGUI.isActiveAndEnabled) return;
if (!textMeshProUGUI.TryGetComponent<UIEffectBase>(out var effect)) return;
if (!effect || !effect.isActiveAndEnabled) return;

Expand Down

0 comments on commit ffda5cf

Please sign in to comment.