Unity3D实现渐变颜色效果实现渐变颜色效果
基于unity3D实现渐变颜色的简单脚本,代码很少,就不废话了,直接上代码和效果图。
效果图:
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace ExtraFoundation.Components
{
[AddComponentMenu("UI/Effects/Gradient")] public class UIGradient : BaseMeshEffect
{
#region Public Declarations
public enum Type
{
Vertical,
Horizontal
}
#endregion
#region Public Properties
public Type GradientType = Type.Vertical;
[Range(-1f, 1f)] public float Offset = 0f;
public Gradient gradient;
#endregion
#region Public Methods
public override void ModifyMesh(VertexHelper helper)
{
if (!IsActive() || helper.currentVertCount == 0)
{
return;
}
vertexList.Clear();
helper.GetUIVertexStream(vertexList);
int nCount = vertexList.Count;
switch (GradientType)
{
case Type.Vertical:
{
float fBottomY = vertexList[0].position.y;
float fTopY = vertexList[0].position.y;
float fYPos = 0f;
for (int i = nCount - 1; i >= 1; --i)