我试图在两种颜色之间转换,在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");
}
答案 0 :(得分:2)
就像rotating或随着时间的推移移动GameObjecss一样,XXXLerp函数仍以相同的方式运行。值得一读,了解lerp的工作原理。唯一要改变的是Quaternion.Lerp
到Color.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));
}