UGUI_Text_Gradient: GradientLeftToRight / GradientTopToBottom

来源:互联网 发布:mac应用程序卸载不了 编辑:程序博客网 时间:2024/06/01 08:46
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[AddComponentMenu ("UI/Effect/GradientLeftToRight")]
public class GradientLeftToRight : BaseMeshEffect
{
    [SerializeField]
    private Color32 leftColor = Color.black;
    [SerializeField]
    private Color32 rightColor = Color.black;


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

        var vertextList = new List<UIVertex> ();
        vh.GetUIVertexStream (vertextList);
        int count = vertextList.Count;

        ApplyGradient (vertextList, 0, count);
        vh.Clear ();
        vh.AddUIVertexTriangleStream (vertextList);

    }

    private void ApplyGradient (List<UIVertex> vertexList, int start, int end)
    {
        float leftX = 0;
        float RightX = 0;
        print (end);

        for (int i = 0; i < end; i++) {
            float x = vertexList [i].position.x;

            if (x > RightX) {
                RightX = x;
            } else if (x < leftX) {
                leftX = x;
            }
        }

        float uiElementWeight = RightX - leftX;

        for (int i = 0; i < end; i++) {
            UIVertex uiVertex = vertexList [i];
            uiVertex.color = Color32.Lerp (leftColor, rightColor, (uiVertex.position.x - leftX) / uiElementWeight);
            vertexList [i] = uiVertex;
        }
    }

}  




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

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

    public override void ModifyMesh (VertexHelper vh)
    {
        var vertexList = new List<UIVertex> ();
        vh.GetUIVertexStream (vertexList);
        int count = vertexList.Count;

        ApplyGradient (vertexList, 0, count);
        vh.Clear ();
        vh.AddUIVertexTriangleStream (vertexList);
    }

    private void ApplyGradient (List<UIVertex> vertexList, int start, int end)
    {
        float bottomY = vertexList [0].position.y;
        float topY = vertexList [0].position.y;
        for (int i = start; i < end; ++i) {
            float y = vertexList [i].position.y;
            if (y > topY) {
                topY = y;
            } else if (y < bottomY) {
                bottomY = y;
            }
        }

        float uiElementHeight = topY - bottomY;
        for (int i = start; i < end; ++i) {
            UIVertex uiVertex = vertexList [i];
            uiVertex.color = Color32.Lerp (bottomColor, topColor, (uiVertex.position.y - bottomY) / uiElementHeight);
            vertexList [i] = uiVertex;
        }
    }
}
  

原创粉丝点击