这篇文章主要为大家详细介绍了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)
{
fYPos = vertexList[i].position.y;
if (fYPos > fTopY)
fTopY = fYPos;
else if (fYPos < fBottomY)
fBottomY = fYPos;
}
float fUIElementHeight = 1f / (fTopY - fBottomY);
UIVertex v = new UIVertex();
for (int i = 0; i < helper.currentVertCount; i++)
{
helper.PopulateUIVertex(ref v, i);
v.color = gradient.Evaluate((v.position.y - fBottomY) *
fUIElementHeight - Offset);
helper.SetUIVertex(v, i);
}
}
break;
case Type.Horizontal:
{
float fLeftX = vertexList[0].position.x;
float fRightX = vertexList[0].position.x;
float fXPos = 0f;
for (int i = nCount - 1; i >= 1; --i)
{
fXPos = vertexList[i].position.x;
if (fXPos > fRightX)
fRightX = fXPos;
else if (fXPos < fLeftX)
fLeftX = fXPos;
}
float fUIElementWidth = 1f / (fRightX - fLeftX);
UIVertex v = new UIVertex();
for (int i = 0; i < helper.currentVertCount; i++)
{
helper.PopulateUIVertex(ref v, i);
v.color = gradient.Evaluate((v.position.x - fLeftX) *
fUIElementWidth - Offset);
helper.SetUIVertex(v, i);
}
}
break;
default:
break;
}
}
#endregion
#region Internal Fields
private List<UIVertex> vertexList = new List<UIVertex>();
#endregion
}
}
虽然支持的内容不多,但是小而精,希望对大家有用。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
沃梦达教程
本文标题为:Unity3D实现渐变颜色效果
基础教程推荐
猜你喜欢
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- C#类和结构详解 2023-05-30
- C# windows语音识别与朗读实例 2023-04-27
- ZooKeeper的安装及部署教程 2023-01-22
- winform把Office转成PDF文件 2023-06-14
- C# 调用WebService的方法 2023-03-09
- 一个读写csv文件的C#类 2022-11-06
- unity实现动态排行榜 2023-04-27
- C#控制台实现飞行棋小游戏 2023-04-22
- C# List实现行转列的通用方案 2022-11-02