0

我试图在 1 秒内在两种颜色之间转换,从黑色到蓝色,但我似乎无法正确处理。目前,当我单击按钮时,颜色保持黑色并且永远不会变为蓝色。我需要解决什么问题?谢谢!

private Color startColor = Color.black;
        private Color endColor = new Color(0.0f, 0.71f, 1.0f, 1.0f);
        private float duration = 1.0F;

        void OnButtonClick ()
        {
            AppData.SelectedPageConfig = Page ;
            AnalyticsWrapper.CustomEvent ("SelectPicture", new Dictionary<string, object> {
                { "PictureName", Page.name }        
            }) ;

            StartCoroutine(DoChangeColor());
            StartCoroutine(DoChangeSceneDelay());
        }

        IEnumerator DoChangeColor()
        {
            float lerp = Mathf.PingPong(Time.deltaTime, duration) / duration;

            transform.Find("Creature Color Crop").transform.Find("Creature Image").GetComponent<Image>().color = Color.Lerp(startColor, endColor, lerp);

            yield return new WaitForEndOfFrame();
        }

        IEnumerator DoChangeSceneDelay()
        {
            yield return new WaitForSeconds(2);
            SceneManager.LoadScene("ColoringBook_ColoringScreen");
        }
4

1 回答 1

2

就像随着时间的推移旋转或​​移动 GameObjecss,XXXLerp 函数仍然以相同的方式运行。值得一读以了解 lerp 的工作原理。唯一需要改变的Quaternion.LerpColor.Lerp.

bool changingColor = false;

IEnumerator lerpColor(Image targetImage, Color fromColor, Color toColor, float duration)
{
    if (changingColor)
    {
        yield break;
    }
    changingColor = true;
    float counter = 0;

    while (counter < duration)
    {
        counter += Time.deltaTime;

        float colorTime = counter / duration;
        Debug.Log(colorTime);

        //Change color
        targetImage.color = Color.Lerp(fromColor, toColor, counter / duration);
        //Wait for a frame
        yield return null;
    }
    changingColor = false;
}

用法

Image imageToLerp;

void Start()
{
    imageToLerp = transform.Find("Creature Color Crop").transform.Find("Creature Image").GetComponent<Image>();
    StartCoroutine(lerpColor(imageToLerp, Color.black, Color.blue, 1f));
}
于 2017-12-02T20:27:29.537 回答