#if TMP_ENABLE using System; using System.Collections.Generic; using Coffee.UIEffectInternal; using UnityEditor; using UnityEngine; using UnityEngine.UI; using TMPro; using Object = UnityEngine.Object; namespace Coffee.UIEffects { internal sealed class TmpProxy : GraphicProxy { /// /// Check if the graphic is valid for this proxy. /// protected override bool IsValid(Graphic graphic) { if (graphic == null) return false; if (graphic is TextMeshProUGUI) return true; if (graphic is TMP_SubMeshUI sub) { // If TMP_SubMeshUI is used for text return sub.spriteAsset == null || sub.sharedMaterial != sub.spriteAsset.material; } return false; } /// /// Check if the graphic is a text. /// public override bool IsText(Graphic graphic) { return true; } /// /// Called before modifying the mesh. /// public override void OnPreModifyMesh(Graphic graphic, Canvas canvas) { UIVertexUtil.onLerpVertex = s_OnLerpVertex; ShadowUtil.onMarkAsShadow = s_OnMarkAsShadow; UIEffectContext.onModifyVertex = s_OnModifyVertex; if (canvas != null) { canvas.additionalShaderChannels |= AdditionalCanvasShaderChannels.TexCoord2; } } public override void SetVerticesDirty(Graphic graphic, bool enabled) { if (graphic is TextMeshProUGUI textMeshProUGUI && textMeshProUGUI.isActiveAndEnabled) { if (enabled) { s_ChangedInstances.Add(textMeshProUGUI); } else if (0 < textMeshProUGUI.textInfo?.meshInfo?.Length && 0 < textMeshProUGUI.textInfo.meshInfo[0].vertexCount) { textMeshProUGUI.UpdateVertexData(); } } } private static readonly Func s_OnLerpVertex = #if UNITY_2023_2_OR_NEWER null; #else (vt, a, b, t) => { // TMP 3.x uses packed UV vt.uv1.x = PackUV(Vector2.Lerp(UnpackUV(a.uv1.x), UnpackUV(b.uv1.x), t)); return vt; static float PackUV(Vector2 input) { return (int)(input.x * 511) * 4096 + (int)(input.y * 511); } static Vector2 UnpackUV(float input) { return new Vector2(input / 4096 / 511, input % 4096 / 511); } }; #endif private static readonly Func s_OnMarkAsShadow = (vt, s) => { vt.uv2.x -= s; return vt; }; private static readonly Func s_OnModifyVertex = (vt, uvMask) => { vt.uv2 = new Vector4(uvMask.xMin, uvMask.yMin, uvMask.xMax, uvMask.yMax); return vt; }; private static Mesh s_Mesh; private static readonly VertexHelper s_VertexHelper = new VertexHelper(); private static readonly HashSet s_ChangedInstances = new HashSet(); private static readonly HashSet s_RegisteredInstances = new HashSet(); private static readonly Dictionary s_SdfScaleCache = new Dictionary(); [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] private static void RuntimeInitializeOnLoadMethod() { s_ChangedInstances.Clear(); s_RegisteredInstances.Clear(); s_SdfScaleCache.Clear(); } #if UNITY_EDITOR [InitializeOnLoadMethod] #else [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] #endif private static void InitializeOnLoad() { Register(new TmpProxy()); // When the text is changed, add it to the changed list TMPro_EventManager.TEXT_CHANGED_EVENT.Add(obj => { if (obj is TextMeshProUGUI textMeshProUGUI && textMeshProUGUI.isActiveAndEnabled // fix: `TmpProxy` causes loss of modifications from other `IMeshModifiers` (#371) && textMeshProUGUI.TryGetComponent(out var _)) { s_ChangedInstances.Add(textMeshProUGUI); } }); UIExtraCallbacks.onBeforeCanvasRebuild += () => { // Check SDF scale (lossyScale.y) change var toRemove = InternalListPool.Rent(); foreach (var textMeshProUGUI in s_RegisteredInstances) { if (textMeshProUGUI != null && textMeshProUGUI.isActiveAndEnabled && !textMeshProUGUI.isTextObjectScaleStatic) { 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)) { s_ChangedInstances.Add(textMeshProUGUI); } // Update the scale cache s_SdfScaleCache[id] = lossyScaleY; } // Remove destroyed objects later else if (!ReferenceEquals(textMeshProUGUI, null)) { toRemove.Add(textMeshProUGUI); } } // Remove destroyed objects foreach (var textMeshProUGUI in toRemove) { s_SdfScaleCache.Remove(textMeshProUGUI.GetHashCode()); s_RegisteredInstances.Remove(textMeshProUGUI); } InternalListPool.Return(ref toRemove); }; // Modify the changed TMP mesh UIExtraCallbacks.onAfterCanvasRebuild += () => { foreach (var textMeshProUGUI in s_ChangedInstances) { if (textMeshProUGUI == null || !textMeshProUGUI.isActiveAndEnabled) continue; if (!textMeshProUGUI.TryGetComponent(out var _)) continue; s_RegisteredInstances.Add(textMeshProUGUI); ModifyMesh(textMeshProUGUI); } s_ChangedInstances.Clear(); }; #if UNITY_EDITOR UnityEditor.SceneManagement.EditorSceneManager.sceneSaved += _ => { foreach (var textMeshProUGUI in s_RegisteredInstances) { if (textMeshProUGUI == null || !textMeshProUGUI.isActiveAndEnabled) continue; if (textMeshProUGUI.TryGetComponent(out var effect) && effect.isActiveAndEnabled) { effect.ReleaseMaterial(); effect.SetMaterialDirty(); } } Misc.QueuePlayerLoopUpdate(); }; #endif } private static void ModifyMesh(TextMeshProUGUI textMeshProUGUI) { if (!textMeshProUGUI || !textMeshProUGUI.isActiveAndEnabled) return; if (textMeshProUGUI.textInfo == null || textMeshProUGUI.textInfo.meshInfo == null) return; var canvasRenderer = textMeshProUGUI.canvasRenderer; if (canvasRenderer == null) return; if (s_Mesh == null) { s_Mesh = new Mesh(); s_Mesh.MarkDynamic(); } var subMeshes = InternalListPool.Rent(); var modifiers = InternalListPool.Rent(); var subModifiers = InternalListPool.Rent(); textMeshProUGUI.TryGetComponent(out var effect); textMeshProUGUI.GetComponentsInChildren(subMeshes, 1); textMeshProUGUI.GetComponents(modifiers); for (var i = 0; i < textMeshProUGUI.textInfo.meshInfo.Length; i++) { var meshInfo = textMeshProUGUI.textInfo.meshInfo[i]; if (meshInfo.vertexCount == 0 || meshInfo.mesh == null) { // Clear mesh if no vertices. s_Mesh.Clear(false); if (i == 0) { canvasRenderer.SetMesh(s_Mesh); } else { var subMeshUI = GetSubMeshUI(subMeshes, meshInfo.material, meshInfo.mesh, i - 1); if (subMeshUI != null && subMeshUI.canvasRenderer != null) { subMeshUI.canvasRenderer.SetMesh(s_Mesh); } } continue; } s_VertexHelper.Clear(); meshInfo.mesh.CopyTo(s_VertexHelper); if (i == 0) { foreach (var modifier in modifiers) { modifier.ModifyMesh(s_VertexHelper); } s_VertexHelper.FillMesh(s_Mesh); canvasRenderer.SetMesh(s_Mesh); } else if (i - 1 < subMeshes.Count) { foreach (var modifier in modifiers) { if (modifier is UIEffectBase) continue; modifier.ModifyMesh(s_VertexHelper); } var subMeshUI = GetSubMeshUI(subMeshes, meshInfo.material, meshInfo.mesh, i - 1); if (subMeshUI == null) break; // fix: TMP Text sprite submesh lost when changing fontMaterial (#368) if (!subMeshUI.TryGetComponent(out var replica)) { replica = subMeshUI.gameObject.AddComponent(); textMeshProUGUI.RegisterDirtyMaterialCallback(() => { if (subMeshUI != null) subMeshUI.SetMaterialDirty(); }); } replica.SetTarget(effect); subMeshUI.GetComponents(subModifiers); foreach (var modifier in subModifiers) { modifier.ModifyMesh(s_VertexHelper); } s_VertexHelper.FillMesh(s_Mesh); if (subMeshUI.canvasRenderer != null) { subMeshUI.canvasRenderer.SetMesh(s_Mesh); } } else { break; } } InternalListPool.Return(ref subMeshes); InternalListPool.Return(ref modifiers); InternalListPool.Return(ref subModifiers); s_Mesh.Clear(false); Misc.QueuePlayerLoopUpdate(); } private static TMP_SubMeshUI GetSubMeshUI(List subMeshes, Material material, Mesh mesh, int start) { var count = subMeshes.Count; if (mesh != null) { for (var j = 0; j < count; j++) { var s = subMeshes[(j + start + count) % count]; if (s == null) continue; if (s.mesh == mesh) return s; } } for (var j = 0; j < count; j++) { var s = subMeshes[(j + start + count) % count]; if (s == null) continue; if (s.sharedMaterial == material) return s; } return null; } } } #endif