Unity 3D 物体之渐变色切换

 最近项目中需要用到物体渐变色,写了个demo留着

 

using UnityEngine;
public class test : MonoBehaviour
{
 public Color ColorBegin;
 public Color ColorEnd;
 
 public float Timer = 2.0f;
 
 private Color beginColor;
 private Color endColor;
 private float currentTimer;
 void OnEnable()
 {
  beginColor = ColorBegin;
  endColor = ColorEnd;
 }
 
 void Update()
 {
  currentTimer += Time.deltaTime;
  if (currentTimer >= Timer)
  {
   currentTimer = 0.0f;
   
   this.renderer.sharedMaterial.color = endColor;
   
   if (beginColor == ColorBegin)
   {
    beginColor = ColorEnd;
    endColor = ColorBegin;
   }
   else
   {
    beginColor = ColorBegin;
    endColor = ColorEnd;
   }
  }
  else
  {
   this.renderer.sharedMaterial.color = Color.Lerp(beginColor, endColor, currentTimer / Timer);
  }
 }
}

发布了14 篇原创文章 · 获赞 4 · 访问量 9145

猜你喜欢

转载自blog.csdn.net/zhangzhaoyuxunlei/article/details/44621521