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 } }
虽然支持的内容不多,但是小而精,希望对大家有用。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
上一篇:c#开发cad预览图块步骤详解
栏 目:C#教程
本文标题:Unity3D实现渐变颜色效果
本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/4856.html
您可能感兴趣的文章
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频 器左下角滚动新闻效果的方法
- 01-10C#实现清空回收站的方法
- 01-10C#实现读取注册表监控当前操作系统已安装软件变化的方法
- 01-10C#实现多线程下载文件的方法
- 01-10C#实现Winform中打开网页页面的方法
- 01-10C#实现远程关闭计算机或重启计算机的方法
- 01-10C#自定义签名章实现方法
- 01-10C#文件断点续传实现方法
- 01-10winform实现创建最前端窗体的方法


阅读排行
本栏相关
- 01-10C#通过反射获取当前工程中所有窗体并
- 01-10关于ASP网页无法打开的解决方案
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频 器左下角滚动新
- 01-10C#停止线程的方法
- 01-10C#实现清空回收站的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的
- 01-10C#实现读取注册表监控当前操作系统已
随机阅读
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-10C#中split用法实例总结
- 01-10SublimeText编译C开发环境设置
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-11ajax实现页面的局部加载
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 04-02jquery与jsp,用jquery
- 01-10delphi制作wav文件的方法