From 6af520ed408f2e5eb4d84ec0df484338719ebc8e Mon Sep 17 00:00:00 2001 From: mob-sakai <12690315+mob-sakai@users.noreply.github.com> Date: Sat, 24 May 2025 17:40:35 +0900 Subject: [PATCH] feat: improve performance for TextMeshPro --- Packages/src/Runtime/Utilities/TmpProxy.cs | 24 ++++++++-------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/Packages/src/Runtime/Utilities/TmpProxy.cs b/Packages/src/Runtime/Utilities/TmpProxy.cs index 5ad0e56..ee1c67d 100644 --- a/Packages/src/Runtime/Utilities/TmpProxy.cs +++ b/Packages/src/Runtime/Utilities/TmpProxy.cs @@ -59,7 +59,7 @@ namespace Coffee.UIEffects { if (enabled) { - OnChangeText(textMeshProUGUI); + s_ChangedInstances.Add(textMeshProUGUI); } else if (0 < textMeshProUGUI.textInfo?.meshInfo?.Length && 0 < textMeshProUGUI.textInfo.meshInfo[0].vertexCount) @@ -126,9 +126,6 @@ namespace Coffee.UIEffects #endif private static void InitializeOnLoad() { - s_ChangedInstances.Clear(); - s_RegisteredInstances.Clear(); - s_SdfScaleCache.Clear(); Register(new TmpProxy()); // When the text is changed, add it to the changed list @@ -154,11 +151,11 @@ namespace Coffee.UIEffects // If the scale has changed, add to the changed list if (s_SdfScaleCache.TryGetValue(id, out var prev) && !Mathf.Approximately(prev, lossyScaleY)) { - OnChangeText(textMeshProUGUI); + s_ChangedInstances.Add(textMeshProUGUI); } // Update the scale cache - s_SdfScaleCache[id] = textMeshProUGUI.transform.lossyScale.y; + s_SdfScaleCache[id] = lossyScaleY; } // Remove destroyed objects later else if (!ReferenceEquals(textMeshProUGUI, null)) @@ -183,6 +180,9 @@ namespace Coffee.UIEffects foreach (var textMeshProUGUI in s_ChangedInstances) { if (!textMeshProUGUI || !textMeshProUGUI.isActiveAndEnabled) continue; + if (!textMeshProUGUI.TryGetComponent(out var _)) continue; + + s_RegisteredInstances.Add(textMeshProUGUI); ModifyMesh(textMeshProUGUI); } @@ -207,14 +207,6 @@ namespace Coffee.UIEffects #endif } - private static void OnChangeText(Object obj) - { - if (obj is TextMeshProUGUI textMeshProUGUI && textMeshProUGUI.isActiveAndEnabled) - { - s_ChangedInstances.Add(textMeshProUGUI); - } - } - private static void ModifyMesh(TextMeshProUGUI textMeshProUGUI) { if (!s_Mesh) @@ -223,11 +215,10 @@ namespace Coffee.UIEffects s_Mesh.MarkDynamic(); } - s_RegisteredInstances.Add(textMeshProUGUI); - var effect = textMeshProUGUI.GetComponent(); var subMeshes = InternalListPool.Rent(); var modifiers = InternalListPool.Rent(); var subModifiers = InternalListPool.Rent(); + textMeshProUGUI.TryGetComponent(out var effect); textMeshProUGUI.GetComponentsInChildren(subMeshes, 1); textMeshProUGUI.GetComponents(modifiers); @@ -303,6 +294,7 @@ namespace Coffee.UIEffects InternalListPool.Return(ref modifiers); InternalListPool.Return(ref subModifiers); s_Mesh.Clear(false); + Misc.QueuePlayerLoopUpdate(); } private static TMP_SubMeshUI GetSubMeshUI(List subMeshes, Material material, int start)