通过BaseMeshEffect工具类实现UGUI文字段落渐变效果

预览效果:

继承BaseMeshEffect类,实现它的抽象方法。另一个BaseEffectVertcel类被弃用了用不来。要不然它的参数就是一个ui顶点的数组。

而这个抽象类的参数是一个vertexHelper类型的。

通过vh.currentVertexCount得到当前ui所有顶点的数目.

然后通过PopulateUIVertex取得UiVertex. 可以从Uivertex中获取到顶点的位置和颜色等.

PopulateUiVertex传入一个引用参数和一个索引值表示取出第几个顶点

通过遍历我可以全部取出来 并判断大小.最后取得一个从上到下,从左到右,最大和最小的值.这两个值之差就是该方向的高度即长度.

最后,在遍历  计算该顶点到最低点的一个偏移量(该顶点-最低点).除以该方向的长度,单位话这个值.因为以最低点为标准,最高的偏移量就是最低点到最高点的差,就是高度.

最后把单位化的值传入,gradient的曲线取值函数中,得到颜色值.uivert.设置颜色

把临时取出的uivert赋予之后,传回给vh.setUivertex

具体代码:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;

public class ChangeColor : BaseMeshEffect {

	public Text txt;
    public Gradient gradient;
    public GradientType gradientType;
public enum GradientType
    {
        Vertical,
        Horizontal
    }
    public override void ModifyMesh(VertexHelper vh)
    {
        
        if (!IsActive())//ui下面的成员方法 判断组件是否处于激活状态
        {
            return;
        }
        int count = vh.currentVertCount;//得到当前的顶点个数
        float bottomY = 0;//初始化最底部的Y
        float topY = 0;
        List<UIVertex> uIVertices=new List<UIVertex>();//初始化集合
        for (int i = 0; i < count; i++)
        {
            UIVertex uIVertex=new UIVertex();
             vh.PopulateUIVertex(ref uIVertex, i);//取得当前索引的UI顶点
            uIVertices.Add(uIVertex);//加入集合
            if (gradientType==GradientType.Vertical)
            {
                GetVerticalMaxAndMin(uIVertex.position, ref topY, ref bottomY);
            }
            else
            {
                GetHorizontalMaxAndMin(uIVertex.position, ref topY, ref bottomY);
            }
//             if (uIVertex.position.y > topY)
//             {
//                 topY = uIVertex.position.y;//遍历 最后得到最大的y和最小的y
//             }
//             if (uIVertex.position.y < bottomY)
//             {
//                 bottomY = uIVertex.position.y;
//             }
        }
        float height = topY - bottomY;//得到高度
        
        for (int i = 0; i < uIVertices.Count; i++)
        {
            var vertex = uIVertices[i];
            var offsetY=0.0f;
            if (gradientType==GradientType.Vertical)
            {
                offsetY = vertex.position.y - bottomY;
            }
            else
            {
                offsetY = vertex.position.x - bottomY;
            }
            //得到与底部的偏移量
            vertex.color = gradient.Evaluate(offsetY / height);//除以长度,单位化,去取得gradient曲线的值
            vh.SetUIVertex(vertex,i);//设置某顶点的uivertex信息
    }
 
    }
    public void GetVerticalMaxAndMin(Vector3 vec,ref float max,ref float min)
    {
        GetMaxAndMin(vec.y,ref max,ref min);
    }
    public void GetHorizontalMaxAndMin(Vector3 vec, ref float max, ref float min)
    {
        GetMaxAndMin(vec.x, ref max, ref min);
    }
    public void GetMaxAndMin(float val, ref float max, ref float min)
    {
        if (val > max)
        {
            max = val;//遍历 最后得到最大的y和最小的y
        }
        if (val < min)
        {
            min = val;
        }
    }
    public override void ModifyMesh(Mesh mesh)
    {
        Debug.Log(2);
        /* base.ModifyMesh(mesh);*/
        if (!IsActive())
        {
            return;
        }
        Vector3[] vertexs = mesh.vertices;
        if (vertexs.Length>0)
        {
            float bottomY = 0;
            float topY = 0;
            for (int i = 0; i < vertexs.Length; i++)
            {
                if (vertexs[i].y>topY)
                {
                    topY = vertexs[i].y;
                }
                if (vertexs[i].y<bottomY)
                {
                    bottomY = vertexs[i].y;
                }
            }
            List<Color> colors = new List<Color>();
            float height = topY - bottomY;
            Debug.Log(height);
            for (int i = 0; i < vertexs.Length; i++)
            {
                var offsetY = vertexs[i].y - bottomY;
                colors.Add(gradient.Evaluate(offsetY/height));
            }
            mesh.SetColors(colors);
        }
    }



    // Use this for initialization


    // Update is called once per frame
    void Update () {
	
	}
}

此篇文章来源笔者几年前做得笔记,在表达上可能稍有些乱,还请见谅。

 

作者:Miracle
来源:麦瑞克博客
链接:https://www.playcreator.cn/archives/unity/1642/
本博客所有文章除特别声明外,均采用CC BY-NC-SA 4.0许可协议,转载请注明!
THE END
分享
海报
通过BaseMeshEffect工具类实现UGUI文字段落渐变效果
预览效果: 继承BaseMeshEffect类,实现它的抽象方法。另一个BaseEffectVertcel类被弃用了用不来。要不然它的参数就是一个ui顶点的数组。 而这个抽象类的参数……
<<上一篇
下一篇>>
文章目录
关闭
目 录