您的当前位置:首页正文

UGUI文字渐变颜色

来源:化拓教育网

多行文字颜色渐变

之前找到的文字渐变颜色逻辑不支持多行渐变,然后照着把它改成了多行,写完后搜索类似的方案,发现有更简洁的做法:

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

[AddComponentMenu("UI/Effects/GameGradient")]
public class GameGradient : BaseMeshEffect
{
    [SerializeField]
    private Color32 topColor = Color.white;

    [SerializeField]
    private Color32 bottomColor = Color.black;

    private List<UIVertex> mVertexList;
    public override void ModifyMesh(VertexHelper vh)
    {
        if (!IsActive())
        {
            return;
        }

        if (mVertexList == null)
        {
            mVertexList = new List<UIVertex>();
        }

        vh.GetUIVertexStream(mVertexList);
        ApplyGradient(mVertexList);

        vh.Clear();
        vh.AddUIVertexTriangleStream(mVertexList);
    }
        
    private void ApplyGradient(List<UIVertex> vertexList)
    {
        for (int i = 0; i < vertexList.Count; )
        {
            ChangeColor( vertexList, i, topColor);
            ChangeColor( vertexList, i + 1, topColor);
            ChangeColor( vertexList, i + 2, bottomColor);
            ChangeColor( vertexList, i + 3, bottomColor);
            ChangeColor( vertexList, i + 4, bottomColor);
            ChangeColor( vertexList, i + 5, topColor);
            i += 6;
        }
    }

    private void ChangeColor( List<UIVertex> verList, int index, Color color)
    {
        UIVertex temp = verList[index];
        temp.color = color;
        verList[index] = temp;
    }
}

多颜色渐变