using TMPro; using UnityEngine; [ExecuteAlways] [DisallowMultipleComponent] [RequireComponent(typeof(TMP_Text))] public class TMPArcBend : MonoBehaviour { public enum BendDirection { Up, Down } [Header("弯曲")] [Range(0f, 1f)] [Tooltip("0 = 平,1 = 弯成半圆")] public float arcAmount; public BendDirection direction = BendDirection.Up; TMP_Text _text; bool _applying; void OnEnable() { _text = GetComponent(); SubscribeToTextChanged(); RequestBend(); } void OnDisable() { UnsubscribeFromTextChanged(); } void OnDestroy() { UnsubscribeFromTextChanged(); } void SubscribeToTextChanged() { TMPro_EventManager.TEXT_CHANGED_EVENT.Add(OnTextChanged); } void UnsubscribeFromTextChanged() { TMPro_EventManager.TEXT_CHANGED_EVENT.Remove(OnTextChanged); } void OnValidate() { RequestBend(); } void OnTextChanged(Object obj) { if (!this || _text == null) return; if (obj == _text) RequestBend(); } public void SetArcAmount(float value) { arcAmount = Mathf.Clamp01(value); RequestBend(); } public void SetDirection(BendDirection dir) { direction = dir; RequestBend(); } void RequestBend() { if (!this) return; if (_text == null) _text = GetComponent(); if (_text == null) return; ApplyBend(); } void ApplyBend() { if (!this || _applying || _text == null) return; _applying = true; UnsubscribeFromTextChanged(); try { _text.ForceMeshUpdate(true); if (arcAmount <= 0.0001f) return; var textInfo = _text.textInfo; if (textInfo.characterCount == 0) return; float minX = float.MaxValue; float maxX = float.MinValue; for (int i = 0; i < textInfo.characterCount; i++) { if (!textInfo.characterInfo[i].isVisible) continue; var c = textInfo.characterInfo[i]; minX = Mathf.Min(minX, c.bottomLeft.x); maxX = Mathf.Max(maxX, c.topRight.x); } if (maxX <= minX) return; float width = maxX - minX; float centerX = (minX + maxX) * 0.5f; float arcRadians = arcAmount * Mathf.PI; float halfArc = arcRadians * 0.5f; float sinHalf = Mathf.Sin(halfArc); if (sinHalf < 0.0001f) return; float radius = (width * 0.5f) / sinHalf; float sign = direction == BendDirection.Up ? 1f : -1f; for (int i = 0; i < textInfo.characterCount; i++) { var charInfo = textInfo.characterInfo[i]; if (!charInfo.isVisible) continue; int meshIndex = charInfo.materialReferenceIndex; int vertIndex = charInfo.vertexIndex; var verts = textInfo.meshInfo[meshIndex].vertices; float cx = (charInfo.bottomLeft.x + charInfo.topRight.x) * 0.5f; float cy = (charInfo.bottomLeft.y + charInfo.topRight.y) * 0.5f; float t = Mathf.Clamp((cx - centerX) / (width * 0.5f), -1f, 1f); float theta = t * halfArc; var flatCenter = new Vector3(cx, cy, 0f); var arcCenter = new Vector3( centerX + Mathf.Sin(theta) * radius, cy + sign * (1f - Mathf.Cos(theta)) * radius, 0f); var rot = Quaternion.Euler(0f, 0f, -theta * Mathf.Rad2Deg); for (int v = 0; v < 4; v++) { var offset = verts[vertIndex + v] - flatCenter; verts[vertIndex + v] = arcCenter + rot * offset; } textInfo.meshInfo[meshIndex].mesh.vertices = verts; } for (int m = 0; m < textInfo.meshInfo.Length; m++) { var meshInfo = textInfo.meshInfo[m]; meshInfo.mesh.RecalculateBounds(); _text.UpdateGeometry(meshInfo.mesh, m); } } finally { if (this != null) SubscribeToTextChanged(); _applying = false; } } }