UGUI渐变色

只实现了横纵两个方向的两个色值渐变,未能实现Gradient多色,后面会寻找更好地方法。

/*******************************************************************************
* 版本声明:v1.0.0
* 类 名 称:GradientEffect
* 创建日期:2020-06-02 17:05:44
* 作者名称:末零
* 功能描述:渐变色
* 修改记录:
* 
******************************************************************************/

using System.Collections.Generic;

using UnityEngine;
using UnityEngine.UI;

namespace LastZero
{
    /// 
    /// 渐变色
    /// 
    public class GradientEffect : BaseMeshEffect
    {
        public enum Direction
        {
            Horizontal,
            Vertical
        }

        public Direction direction;

        public Color32 startColor = Color.white;
        public Color32 endColor = Color.black;

        public override void ModifyMesh(VertexHelper vh)
        {
            if (!IsActive())
            {
                return;
            }

            var count = vh.currentVertCount;
            if (count == 0)
                return;

            var vertexs = new List();
            for (var i = 0; i < count; i++)
            {
                var vertex = new UIVertex();
                vh.PopulateUIVertex(ref vertex, i);
                vertexs.Add(vertex);
            }

            switch (direction)
            {
                case Direction.Horizontal:
                    var leftX = vertexs[0].position.x;
                    var rightX = vertexs[0].position.x;

                    for (var i = 1; i < count; i++)
                    {
                        var x = vertexs[i].position.x;
                        if (x < leftX)
                        {
                            leftX = x;
                        }
                        else if (x > rightX)
                        {
                            rightX = x;
                        }
                    }

                    var width = rightX - leftX;
                    for (var i = 0; i < count; i++)
                    {
                        var vertex = vertexs[i];

                        var color = Color32.Lerp(startColor, endColor, (vertex.position.x - leftX) / width);

                        vertex.color = color;

                        vh.SetUIVertex(vertex, i);
                    }
                    break;
                case Direction.Vertical:
                    var topY = vertexs[0].position.y;
                    var bottomY = vertexs[0].position.y;

                    for (var i = 1; i < count; i++)
                    {
                        var y = vertexs[i].position.y;
                        if (y > topY)
                        {
                            topY = y;
                        }
                        else if (y < bottomY)
                        {
                            bottomY = y;
                        }
                    }

                    var height = topY - bottomY;
                    for (var i = 0; i < count; i++)
                    {
                        var vertex = vertexs[i];

                        var color = Color32.Lerp(endColor, startColor, (vertex.position.y - bottomY) / height);

                        vertex.color = color;

                        vh.SetUIVertex(vertex, i);
                    }
                    break;
            }
        }
    }
}

 

你可能感兴趣的:(Unity3D)