溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Unity3D如何實現(xiàn)漸變顏色效果

發(fā)布時間:2020-08-03 11:40:41 來源:億速云 閱讀:1326 作者:小豬 欄目:編程語言

小編這次要給大家分享的是Unity3D如何實現(xiàn)漸變顏色效果,文章內(nèi)容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。

Unity3D如何實現(xiàn)漸變顏色效果

效果圖:

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

看完這篇關(guān)于Unity3D如何實現(xiàn)漸變顏色效果的文章,如果覺得文章內(nèi)容寫得不錯的話,可以把它分享出去給更多人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI