2017-12-02 271 views
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"); 
     } 
+0

你的協同程序的兩個只執行一次,你需要有內部'DoChangeColor()',檢查它是否已達到最終顏色的循環(和重複顏色改變直到完成) – UnholySheep

回答

2

就像rotating或隨着時間的推移移動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)); 
} 
相關問題