Unity3D实现渐变颜色效果

作者:Cattleya_ 时间:2022-09-03 08:14:32 

基于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
}
}

虽然支持的内容不多,但是小而精,希望对大家有用。

来源:https://blog.csdn.net/Cattleya_/article/details/80118419

标签:Unity3D,渐变颜色
0
投稿

猜你喜欢

  • Android实现语音播放与录音功能

    2022-01-21 15:39:09
  • java设置session过期时间的实现方法

    2022-02-18 20:25:29
  • Spring Boot启动banner定制的步骤详解

    2023-03-04 19:30:20
  • ref与out之间的区别深入解析

    2023-08-05 23:45:28
  • Spring Cloud 整合Apache-SkyWalking实现链路跟踪的方法

    2023-04-05 09:17:52
  • Spring整合Quartz实现动态定时器的示例代码

    2022-10-22 06:48:17
  • 详解Java中运算符及用法

    2023-11-29 08:17:57
  • ZooKeeper 实现分布式锁的方法示例

    2023-03-20 07:26:43
  • C#中时间类的使用方法详解

    2023-12-17 13:21:08
  • 详解Mybatis注解写法(附10余个常用例子)

    2023-01-19 03:39:53
  • Java根据ip地址获取归属地实例详解

    2023-11-25 06:24:38
  • 解析spring cloud ouath2中的Eureka

    2023-10-12 04:07:54
  • SpringCloud Eureka服务治理之服务注册服务发现

    2021-12-27 15:07:16
  • Activiti流程图查看实例

    2022-09-10 17:51:44
  • SpringCloud全面解析@FeignClient标识接口的过程

    2023-08-05 12:34:44
  • JAVA格式化时间日期的简单实例

    2022-10-06 09:14:58
  • C#验证给定字符串形式日期是否合法的方法

    2021-09-15 16:00:37
  • Spring Cloud Config RSA简介及使用RSA加密配置文件的方法

    2023-11-28 22:39:26
  • SpringMVC数据输出相关知识总结

    2022-04-06 10:35:06
  • C#泛型实例详解

    2021-09-11 04:21:35
  • asp之家 软件编程 m.aspxhome.com