请按照一下要求写出代码在unity中使用C#写一个脚本控制图片在点击button后调用一个名为showTime的函数实现该图片在025秒内轮番由绿到红由红到绿
public class ChangeColor : MonoBehaviour { public Image image; // 需要控制的图片 private bool isGreenToRed = true; // 默认绿色转红色
private void Start()
{
image.color = Color.green; // 初始颜色为绿色
}
public void OnButtonClick()
{
InvokeRepeating("ShowTime", 0.1f, 0.1f); // 每0.1秒调用一次ShowTime函数
}
private void ShowTime()
{
float time = 0.25f; // 动画时间为0.25秒
if (isGreenToRed)
{
image.color = Color.Lerp(Color.green, Color.red, time / Time.deltaTime); // 绿色转红色
}
else
{
image.color = Color.Lerp(Color.red, Color.green, time / Time.deltaTime); // 红色转绿色
}
if (image.color == Color.red)
{
isGreenToRed = false; // 红色转绿色
}
else if (image.color == Color.green)
{
isGreenToRed = true; // 绿色转红色
}
if (Time.time >= time)
{
CancelInvoke(); // 取消InvokeRepeating
}
}
原文地址: https://www.cveoy.top/t/topic/fiHn 著作权归作者所有。请勿转载和采集!