AkiraPixelWind/Assets/Scripts/Main/UI/UIYYControl/UIEfxGradient.cs

81 lines
1.9 KiB
C#
Raw Normal View History

2022-12-29 18:20:40 +08:00
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
/// <summary>
/// 渐变效果 右上->左下的渐变
/// </summary>
[AddComponentMenu("UI/Effects/UIEfxGradient")]
public class UIEfxGradient : BaseMeshEffect
{
public Color32 m_colorBottomLeft = Color.white;
public Color32 m_colorTopRight = Color.black;
public override void ModifyMesh(VertexHelper vh)
{
if (!IsActive())
{
return;
}
var count = vh.currentVertCount;
if (count == 0)
return;
var vertexs = new List<UIVertex>();
for (var i = 0; i < count; i++)
{
var vertex = new UIVertex();
vh.PopulateUIVertex(ref vertex, i);
vertexs.Add(vertex);
}
float posX = vertexs[0].position.x;
float posY = vertexs[0].position.y;
var topY = posY;
var bottomY = posY;
var leftX = posX;
var rightX = posX;
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 x = vertexs[i].position.x;
if (x < leftX)
leftX = x;
else if (x > rightX)
rightX = x;
}
var height = topY - bottomY;
var weith = rightX - leftX;
for (var i = 0; i < count; i++)
{
var vertex = vertexs[i];
var color1 = Color32.Lerp(m_colorBottomLeft, m_colorTopRight, (vertex.position.y - bottomY) / height);
var color2 = Color32.Lerp(m_colorBottomLeft, m_colorTopRight, (vertex.position.x - leftX) / weith);
var color = Color32.Lerp(color1, color2, 0.5f);
vertex.color = color;
vh.SetUIVertex(vertex, i);
}
}
}