66 lines
1.6 KiB
C#
66 lines
1.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
[AddComponentMenu("UI/Effects/Gradient")]
|
|
public class Gradient : BaseMeshEffect
|
|
{
|
|
[SerializeField]
|
|
public Color32 topColor = Color.white;
|
|
|
|
[SerializeField]
|
|
public Color32 bottomColor = Color.black;
|
|
|
|
public void SetColor(Color top, Color bottom)
|
|
{
|
|
topColor = top;
|
|
bottomColor = bottom;
|
|
}
|
|
|
|
public override void ModifyMesh(VertexHelper vh)
|
|
{
|
|
if (!IsActive())
|
|
{
|
|
return;
|
|
}
|
|
|
|
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)
|
|
{
|
|
if (vertexList.Count ==0)
|
|
{
|
|
return;
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
} |