欢迎来到入门教程网!

C#教程

当前位置:主页 > 软件编程 > C#教程 >

Unity3D实现渐变颜色效果

来源:本站原创|时间:2020-01-10|栏目:C#教程|点击:

基于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#教程

下一篇:Unity shader实现消融效果

本文标题:Unity3D实现渐变颜色效果

本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/4856.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有