unity3dText渐变效果

unity3dText渐变效果

效果

unity3dText渐变效果_第1张图片
unity3dText渐变效果_第2张图片

代码

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

namespace Kola {

    [AddComponentMenu("UI/Kola/UIEffectGradientColor")]
    [ExecuteInEditMode]

#if !UNITY_5_1
    public class UIEffectGradientColor : BaseMeshEffect {

        List vertices = new List();
        public override void ModifyMesh(VertexHelper vh) {
            vertices.Clear();
            vh.GetUIVertexStream(vertices);

            float topX = 0f, topY = 0f, bottomX = 0f, bottomY = 0f;
            for (int i= 0; i < vertices.Count; ++i) {
                UIVertex vertex = vertices[i];
                topX = Mathf.Max(topX, vertex.position.x);
                topY = Mathf.Max(topY, vertex.position.y);
                bottomX = Mathf.Min(bottomX, vertex.position.x);
                bottomY = Mathf.Min(bottomY, vertex.position.y);
            }
            float width = topX - bottomX;
            float height = topY - bottomY;

            for (int i = 0; i < vertices.Count; ++i) {
                UIVertex vertex = vertices[i];
                Color tempVertex = vertex.color;
                Vector3 pos = vertex.position;
                Color colorOrg = tempVertex;
                Color colorV = Color.Lerp(colorBottom, colorTop, (pos.y - bottomY) / height);
                Color colorH = Color.Lerp(colorLeft, colorRight, (pos.x - bottomX) / width);
                switch (direction) {
                    case DIRECTION.Both:
                        tempVertex = colorOrg * colorV * colorH;
                        break;
                    case DIRECTION.Vertical:
                        tempVertex = colorOrg * colorV;
                        break;
                    case DIRECTION.Horizontal:
                        tempVertex = colorOrg * colorH;
                        break;
                }
                vertex.color = tempVertex;
                vertices[i] = vertex;
            }

            vh.Clear();
            vh.AddUIVertexTriangleStream(vertices);
        }
#endif

#if UNITY_5_1
    public class UIEffectGradientColor : BaseVertexEffect {
        public override void ModifyVertices(List verts) {
        }
#endif

        public enum DIRECTION {
            Vertical,
            Horizontal,
            Both,
        }

        public DIRECTION direction = DIRECTION.Vertical;
        public Color colorTop = Color.white;
        public Color colorBottom = Color.black;
        public Color colorLeft = Color.red;
        public Color colorRight = Color.blue;

    }
}

你可能感兴趣的:(unity项目)