TMPArcBend.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using TMPro;
  2. using UnityEngine;
  3. [ExecuteAlways]
  4. [DisallowMultipleComponent]
  5. [RequireComponent(typeof(TMP_Text))]
  6. public class TMPArcBend : MonoBehaviour
  7. {
  8. public enum BendDirection
  9. {
  10. Up,
  11. Down
  12. }
  13. [Header("弯曲")]
  14. [Range(0f, 1f)]
  15. [Tooltip("0 = 平,1 = 弯成半圆")]
  16. public float arcAmount;
  17. public BendDirection direction = BendDirection.Up;
  18. TMP_Text _text;
  19. bool _applying;
  20. void OnEnable()
  21. {
  22. _text = GetComponent<TMP_Text>();
  23. SubscribeToTextChanged();
  24. RequestBend();
  25. }
  26. void OnDisable()
  27. {
  28. UnsubscribeFromTextChanged();
  29. }
  30. void OnDestroy()
  31. {
  32. UnsubscribeFromTextChanged();
  33. }
  34. void SubscribeToTextChanged()
  35. {
  36. TMPro_EventManager.TEXT_CHANGED_EVENT.Add(OnTextChanged);
  37. }
  38. void UnsubscribeFromTextChanged()
  39. {
  40. TMPro_EventManager.TEXT_CHANGED_EVENT.Remove(OnTextChanged);
  41. }
  42. void OnValidate()
  43. {
  44. RequestBend();
  45. }
  46. void OnTextChanged(Object obj)
  47. {
  48. if (!this || _text == null)
  49. return;
  50. if (obj == _text)
  51. RequestBend();
  52. }
  53. public void SetArcAmount(float value)
  54. {
  55. arcAmount = Mathf.Clamp01(value);
  56. RequestBend();
  57. }
  58. public void SetDirection(BendDirection dir)
  59. {
  60. direction = dir;
  61. RequestBend();
  62. }
  63. void RequestBend()
  64. {
  65. if (!this)
  66. return;
  67. if (_text == null)
  68. _text = GetComponent<TMP_Text>();
  69. if (_text == null)
  70. return;
  71. ApplyBend();
  72. }
  73. void ApplyBend()
  74. {
  75. if (!this || _applying || _text == null)
  76. return;
  77. _applying = true;
  78. UnsubscribeFromTextChanged();
  79. try
  80. {
  81. _text.ForceMeshUpdate(true);
  82. if (arcAmount <= 0.0001f)
  83. return;
  84. var textInfo = _text.textInfo;
  85. if (textInfo.characterCount == 0)
  86. return;
  87. float minX = float.MaxValue;
  88. float maxX = float.MinValue;
  89. for (int i = 0; i < textInfo.characterCount; i++)
  90. {
  91. if (!textInfo.characterInfo[i].isVisible)
  92. continue;
  93. var c = textInfo.characterInfo[i];
  94. minX = Mathf.Min(minX, c.bottomLeft.x);
  95. maxX = Mathf.Max(maxX, c.topRight.x);
  96. }
  97. if (maxX <= minX)
  98. return;
  99. float width = maxX - minX;
  100. float centerX = (minX + maxX) * 0.5f;
  101. float arcRadians = arcAmount * Mathf.PI;
  102. float halfArc = arcRadians * 0.5f;
  103. float sinHalf = Mathf.Sin(halfArc);
  104. if (sinHalf < 0.0001f)
  105. return;
  106. float radius = (width * 0.5f) / sinHalf;
  107. float sign = direction == BendDirection.Up ? 1f : -1f;
  108. for (int i = 0; i < textInfo.characterCount; i++)
  109. {
  110. var charInfo = textInfo.characterInfo[i];
  111. if (!charInfo.isVisible)
  112. continue;
  113. int meshIndex = charInfo.materialReferenceIndex;
  114. int vertIndex = charInfo.vertexIndex;
  115. var verts = textInfo.meshInfo[meshIndex].vertices;
  116. float cx = (charInfo.bottomLeft.x + charInfo.topRight.x) * 0.5f;
  117. float cy = (charInfo.bottomLeft.y + charInfo.topRight.y) * 0.5f;
  118. float t = Mathf.Clamp((cx - centerX) / (width * 0.5f), -1f, 1f);
  119. float theta = t * halfArc;
  120. var flatCenter = new Vector3(cx, cy, 0f);
  121. var arcCenter = new Vector3(
  122. centerX + Mathf.Sin(theta) * radius,
  123. cy + sign * (1f - Mathf.Cos(theta)) * radius,
  124. 0f);
  125. var rot = Quaternion.Euler(0f, 0f, -theta * Mathf.Rad2Deg);
  126. for (int v = 0; v < 4; v++)
  127. {
  128. var offset = verts[vertIndex + v] - flatCenter;
  129. verts[vertIndex + v] = arcCenter + rot * offset;
  130. }
  131. textInfo.meshInfo[meshIndex].mesh.vertices = verts;
  132. }
  133. for (int m = 0; m < textInfo.meshInfo.Length; m++)
  134. {
  135. var meshInfo = textInfo.meshInfo[m];
  136. meshInfo.mesh.RecalculateBounds();
  137. _text.UpdateGeometry(meshInfo.mesh, m);
  138. }
  139. }
  140. finally
  141. {
  142. if (this != null)
  143. SubscribeToTextChanged();
  144. _applying = false;
  145. }
  146. }
  147. }