| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- 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<TMP_Text>();
- 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<TMP_Text>();
- 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;
- }
- }
- }
|