UGUI文字渐变颜色

作者: e497b7005759 | 来源:发表于2017-03-14 22:24 被阅读152次

    多行文字颜色渐变

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

    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;
        }
    }
    

    多颜色渐变

    还发现了多颜色渐变的实现UNITY之文本TEXT颜色渐变

    相关文章

      网友评论

        本文标题:UGUI文字渐变颜色

        本文链接:https://www.haomeiwen.com/subject/dzhknttx.html