最近项目中需要用到物体渐变色,写了个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);
}
}
}