unity3d曲线text文本

简介: 测试.png using System;using System.Collections.Generic;namespace UnityEngine.UI.Extensions{ /// /// Curved text.让文本按照曲线进行显示 【注意对Image的变形 也是可以的】 /// 说明: 对Text的操作就和 shadow 和 outline 组件类似。
img_0dfbbcce44f44ccc8a24433975c83f59.png
测试.png
 using System;
using System.Collections.Generic;

namespace UnityEngine.UI.Extensions
{
    /// <summary>
    /// Curved text.让文本按照曲线进行显示 【注意对Image的变形 也是可以的】
    /// 说明: 对Text的操作就和 shadow 和 outline 组件类似。
    /// </summary>
    // [RequireComponent(typeof(Text), typeof(RectTransform))]
    [AddComponentMenu("UI/Effects/Extensions/Curved Text")]
    public class CurvedText : BaseMeshEffect
    {
        // 曲线类型
        public AnimationCurve curveForText = AnimationCurve.Linear(0, 0, 1, 10);
        // 曲线程度
        public float curveMultiplier = 1;
        private RectTransform rectTrans;


#if UNITY_EDITOR
        protected override void OnValidate()
        {
            base.OnValidate();
            if (curveForText[0].time != 0)
            {
                var tmpRect = curveForText[0];
                tmpRect.time = 0;
                curveForText.MoveKey(0, tmpRect);
            }
            if (rectTrans == null)
                rectTrans = GetComponent<RectTransform>();
            if (curveForText[curveForText.length - 1].time != rectTrans.rect.width)
                OnRectTransformDimensionsChange();
        }
#endif
        protected override void Awake()
        {
            base.Awake();
            rectTrans = GetComponent<RectTransform>();
            OnRectTransformDimensionsChange();
        }
        protected override void OnEnable()
        {
            base.OnEnable();
            rectTrans = GetComponent<RectTransform>();
            OnRectTransformDimensionsChange();
        }
        /// <summary>
        /// Modifies the mesh. 最重要的重载函数
        /// </summary>
        /// <param name="mesh">Mesh.</param>
        public override void ModifyMesh(VertexHelper vh)
        {
            if (!IsActive())
                return;

            // 从mesh 得到 顶点集
            List<UIVertex> verts = new List<UIVertex>();

            vh.GetUIVertexStream(verts);


            // 顶点的 y值按曲线变换
            for (int index = 0; index < verts.Count; index++)
            {
                var uiVertex = verts[index];
                //Debug.Log ();
                uiVertex.position.y += curveForText.Evaluate(rectTrans.rect.width * rectTrans.pivot.x + uiVertex.position.x) * curveMultiplier;
                verts[index] = uiVertex;
            }

            // 在合成mesh

            vh.AddUIVertexTriangleStream(verts);


        }
        protected override void OnRectTransformDimensionsChange()
        {
            var tmpRect = curveForText[curveForText.length - 1];
            if (rectTrans != null)
            {
                tmpRect.time = rectTrans.rect.width;
                curveForText.MoveKey(curveForText.length - 1, tmpRect);

            }
        }

    }
}

相关文章
|
11天前
|
图形学 开发者
Unity Text字体颜色渐变
此代码实现文本颜色的垂直渐变效果。通过定义起始和结束颜色,获取像素位置信息(UV坐标),根据渐变方向计算颜色插值,并应用到每个像素上。具体步骤包括:1) 定义颜色范围;2) 获取UV坐标;3) 计算线性插值;4) 应用颜色。脚本挂载在Text组件上,可调节顶部和底部颜色实现渐变效果。
|
11天前
|
图形学
Unity Text修改字间距
本文介绍了两种实现字间距调整的方法。第一种方法直接通过修改顶点位置实现字间距调整,代码简洁但换行时效果不佳。第二种方法引入了`Line`结构和对齐方式(左、中、右),能够处理多行文本并支持不同对齐方式,更加灵活和通用。两种方法均基于Unity的`BaseMeshEffect`类,通过重写`ModifyMesh`方法来调整UI文本的字间距。最终运行测试可验证效果。
|
6月前
|
图形学
小功能⭐️Unity自动更改文本框高度,以显示全部文本
小功能⭐️Unity自动更改文本框高度,以显示全部文本
|
8月前
|
图形学
【unity小技巧】unity读excel配置表操作,excel转txt文本,并读取txt文本内容,实例说明
【unity小技巧】unity读excel配置表操作,excel转txt文本,并读取txt文本内容,实例说明
318 0
|
9月前
|
图形学
Unity Hololens2开发|(七)MRTK3子系统 TextToSpeechSubsystem(文本转语音)
Unity Hololens2开发|(七)MRTK3子系统 TextToSpeechSubsystem(文本转语音)
|
API 图形学
[√]unity渲染一个文本的细节
[√]unity渲染一个文本的细节
101 0
|
人工智能 图形学
Unity 接入有道智云AI - 文本翻译
Unity 接入有道智云AI - 文本翻译
443 1
Unity 接入有道智云AI - 文本翻译
|
图形学
Unity 【Content Size Fitter】- 聊天气泡自动适配Text文本框大小
Unity 【Content Size Fitter】- 聊天气泡自动适配Text文本框大小
600 1
|
图形学
unity之CanvasGroup解决文本异色问题
CanvasGroup解决文本异色问题
unity之CanvasGroup解决文本异色问题