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

}

Unity C# 脚本:点击按钮实现图片颜色渐变动画

原文地址: https://www.cveoy.top/t/topic/oaj1 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录