2017-09-04 31 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

,我很抱歉,解决方案不工作......如何启动特定的协程? –