2023-11-26 17:52:36 +08:00
|
|
|
|
|
|
|
|
|
using System;
|
2015-02-03 07:07:31 +08:00
|
|
|
|
|
|
|
|
|
namespace UnityEngine.UI.Extensions
|
|
|
|
|
{
|
2023-11-26 17:52:36 +08:00
|
|
|
|
#if UNITY_2022_1_OR_NEWER
|
|
|
|
|
[Obsolete("CurvedText is not supported in Unity 2022.1 or newer. Use TMPro instead.")]
|
|
|
|
|
public class CurvedText : BaseMeshEffect
|
|
|
|
|
{
|
|
|
|
|
public override void ModifyMesh(VertexHelper vh)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#else
|
2023-02-07 22:35:43 +08:00
|
|
|
|
[RequireComponent(typeof(Text))]
|
|
|
|
|
[RequireComponent(typeof(RectTransform))]
|
2015-02-03 07:07:31 +08:00
|
|
|
|
[AddComponentMenu("UI/Effects/Extensions/Curved Text")]
|
2015-09-10 05:31:17 +08:00
|
|
|
|
public class CurvedText : BaseMeshEffect
|
2015-02-03 07:07:31 +08:00
|
|
|
|
{
|
2017-06-05 23:01:47 +08:00
|
|
|
|
[SerializeField]
|
|
|
|
|
private AnimationCurve _curveForText = AnimationCurve.Linear(0, 0, 1, 10);
|
|
|
|
|
|
|
|
|
|
public AnimationCurve CurveForText
|
|
|
|
|
{
|
|
|
|
|
get { return _curveForText; }
|
|
|
|
|
set { _curveForText = value; graphic.SetVerticesDirty(); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
|
private float _curveMultiplier = 1;
|
|
|
|
|
|
|
|
|
|
public float CurveMultiplier
|
|
|
|
|
{
|
|
|
|
|
get { return _curveMultiplier; }
|
|
|
|
|
set { _curveMultiplier = value; graphic.SetVerticesDirty(); }
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-03 07:07:31 +08:00
|
|
|
|
private RectTransform rectTrans;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
protected override void OnValidate()
|
|
|
|
|
{
|
|
|
|
|
base.OnValidate();
|
2017-06-05 23:01:47 +08:00
|
|
|
|
if (_curveForText[0].time != 0)
|
2015-02-03 07:07:31 +08:00
|
|
|
|
{
|
2017-06-05 23:01:47 +08:00
|
|
|
|
var tmpRect = _curveForText[0];
|
2015-02-03 07:07:31 +08:00
|
|
|
|
tmpRect.time = 0;
|
2017-06-05 23:01:47 +08:00
|
|
|
|
_curveForText.MoveKey(0, tmpRect);
|
2015-02-03 07:07:31 +08:00
|
|
|
|
}
|
|
|
|
|
if (rectTrans == null)
|
|
|
|
|
rectTrans = GetComponent<RectTransform>();
|
2017-06-05 23:01:47 +08:00
|
|
|
|
if (_curveForText[_curveForText.length - 1].time != rectTrans.rect.width)
|
2015-02-03 07:07:31 +08:00
|
|
|
|
OnRectTransformDimensionsChange();
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
protected override void Awake()
|
|
|
|
|
{
|
|
|
|
|
base.Awake();
|
|
|
|
|
rectTrans = GetComponent<RectTransform>();
|
|
|
|
|
OnRectTransformDimensionsChange();
|
|
|
|
|
}
|
|
|
|
|
protected override void OnEnable()
|
|
|
|
|
{
|
|
|
|
|
base.OnEnable();
|
|
|
|
|
rectTrans = GetComponent<RectTransform>();
|
|
|
|
|
OnRectTransformDimensionsChange();
|
|
|
|
|
}
|
2015-10-11 18:33:43 +08:00
|
|
|
|
public override void ModifyMesh(VertexHelper vh)
|
2015-02-03 07:07:31 +08:00
|
|
|
|
{
|
2015-10-11 18:33:43 +08:00
|
|
|
|
int count = vh.currentVertCount;
|
|
|
|
|
if (!IsActive() || count == 0)
|
|
|
|
|
{
|
2015-02-03 07:07:31 +08:00
|
|
|
|
return;
|
2015-10-11 18:33:43 +08:00
|
|
|
|
}
|
|
|
|
|
for (int index = 0; index < vh.currentVertCount; index++)
|
2015-02-03 07:07:31 +08:00
|
|
|
|
{
|
2015-10-11 18:33:43 +08:00
|
|
|
|
UIVertex uiVertex = new UIVertex();
|
|
|
|
|
vh.PopulateUIVertex(ref uiVertex, index);
|
2017-06-05 23:01:47 +08:00
|
|
|
|
uiVertex.position.y += _curveForText.Evaluate(rectTrans.rect.width * rectTrans.pivot.x + uiVertex.position.x) * _curveMultiplier;
|
2015-10-11 18:33:43 +08:00
|
|
|
|
vh.SetUIVertex(uiVertex, index);
|
2015-02-03 07:07:31 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
protected override void OnRectTransformDimensionsChange()
|
|
|
|
|
{
|
2017-05-05 21:06:52 +08:00
|
|
|
|
if (rectTrans)
|
|
|
|
|
{
|
2017-06-05 23:01:47 +08:00
|
|
|
|
Keyframe tmpRect = _curveForText[_curveForText.length - 1];
|
2017-05-05 21:06:52 +08:00
|
|
|
|
tmpRect.time = rectTrans.rect.width;
|
2017-06-05 23:01:47 +08:00
|
|
|
|
_curveForText.MoveKey(_curveForText.length - 1, tmpRect);
|
2017-05-05 21:06:52 +08:00
|
|
|
|
}
|
2015-02-03 07:07:31 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-26 17:52:36 +08:00
|
|
|
|
#endif
|
|
|
|
|
}
|