2017-09-04 44 views
0

喜我親愛的朋友......問題在使用協程團結

我寫跟隨類的透明度遊戲對象,並添加幾個遊戲對象的組件,但是當我按下播放按鈕,所有這個類作爲一個組件開始打開和關閉的對象...如果我想打開一個名稱調用的遊戲對象......打開和關閉。請幫助我 請.....

using System.Collections; 
    using System.Collections.Generic; 
    using UnityEngine; 
    using UnityEngine.UI; 
    using System.Threading; 

    public class Transparent : MonoBehaviour { 

     private float duration = .7f; 
     public float waitTime; 
     IEnumerator co; 
     // Update is called once per frame void 
     public void Start() 
     { 
      this.co=this.blink(); 
      this.StartCoroutine (this.co); 
     } 
     IEnumerator blink() { 

      Color textureColor = this.GetComponent<SpriteRenderer>().material.color; 
       //textureColor.a = Mathf.PingPong(Time.time, duration)/duration; 
       //this.GetComponent<SpriteRenderer>().material.color = textureColor; 
       while (true) { // this could also be a condition indicating "alive or dead" 
        // we scale all axis, so they will have the same value, 
        // so we can work with a float instead of comparing vectors 


        textureColor.a = Mathf.PingPong (Time.time, duration)/duration; 
       this.GetComponent<SpriteRenderer>().material.color = textureColor; 


        // reset the timer 

        yield return new WaitForSeconds (waitTime); 



       } 
      //end of if(this.transform.childCount =0) 

     } 

     void stop_Transparency() 
     { 

       this.StopCoroutine (co); 

     } 

    } 

and write different class for transparency dice that has child follow class 

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 

public class dice_Transparent : MonoBehaviour { 
    private float duration = .7f; 
    public float waitTime; 
    IEnumerator co; 
    // Update is called once per frame void 
    public void Start() 
    { 
     this.co=this.dice_blink(); 
     this.StartCoroutine (this.co); 
    } 
    IEnumerator dice_blink() { 

     Color textureColor0 = this.transform.GetChild (0).GetComponent<SpriteRenderer>().material.color; 
     Color textureColor1 = this.transform.GetChild (1).GetComponent<SpriteRenderer>().material.color; 
     Color textureColor2 = this.transform.GetChild (2).GetComponent<SpriteRenderer>().material.color; 
     Color textureColor3 = this.transform.GetChild (3).GetComponent<SpriteRenderer>().material.color; 
     Color textureColor4 = this.transform.GetChild (4).GetComponent<SpriteRenderer>().material.color; 
     Color textureColor5 = this.transform.GetChild (5).GetComponent<SpriteRenderer>().material.color; 

     //textureColor.a = Mathf.PingPong(Time.time, duration)/duration; 
     //this.GetComponent<SpriteRenderer>().material.color = textureColor; 
     while (true) { // this could also be a condition indicating "alive or dead" 
      // we scale all axis, so they will have the same value, 
      // so we can work with a float instead of comparing vectors 
      textureColor0.a=Mathf.PingPong (Time.time, duration)/duration; 
      textureColor1.a=Mathf.PingPong (Time.time, duration)/duration; 
      textureColor2.a=Mathf.PingPong (Time.time, duration)/duration; 
      textureColor3.a=Mathf.PingPong (Time.time, duration)/duration; 
      textureColor4.a=Mathf.PingPong (Time.time, duration)/duration; 
      textureColor5.a=Mathf.PingPong (Time.time, duration)/duration; 
      this.transform.GetChild (0).GetComponent<SpriteRenderer>().material.color = textureColor0; 
      this.transform.GetChild (1).GetComponent<SpriteRenderer>().material.color = textureColor1; 
      this.transform.GetChild (2).GetComponent<SpriteRenderer>().material.color = textureColor2; 
      this.transform.GetChild (3).GetComponent<SpriteRenderer>().material.color = textureColor3; 
      this.transform.GetChild (4).GetComponent<SpriteRenderer>().material.color = textureColor4; 
      this.transform.GetChild (5).GetComponent<SpriteRenderer>().material.color = textureColor5; 

      // reset the timer 

      yield return new WaitForSeconds (waitTime); 



     } 
     //end of if(this.transform.childCount =0) 

    } 

    void stop_Transparency() 
    { 

     this.StopCoroutine (this.co); 

    } 
} 

從兩個coroutines程序的任何地方每一個上面類開始coroutine之後執行........

我調用了組件,以便測試對象指向圖形對象。

我的電話組件:

test.gameObject.GetComponent<Transparent>().Start(); 

請幫我....請

回答

1

開始()Monobehaviours保留功能,啓用了腳本時默認執行。
您可以在此閱讀有關啓動功能的信息:
https://docs.unity3d.com/ScriptReference/MonoBehaviour.Start.html
如果您想手動調用功能,請使用其他功能名稱。
例如,在名爲MyStart()的函數中複製啓動函數的內容,然後調用該函數。就像這樣:

test.gameObject.GetComponent<Transparent>().MyStart(); 
+0

我的上帝啊.......這是不工作 –

+0

@alikarimifard,我認爲你所面對的問題是,所有的協同程序開始在一起,但你只想要啓動特定的協程。 我無法從你的描述中瞭解更多。 我根據它提供的解決方案。 我不明白,如果你只是說,「這是行不通的。」請更具體地說明問題。 – ZayedUpal

+0

,我很抱歉,解決方案不工作......如何啓動特定的協程? –