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();
請幫我....請
我的上帝啊.......這是不工作 –
@alikarimifard,我認爲你所面對的問題是,所有的協同程序開始在一起,但你只想要啓動特定的協程。 我無法從你的描述中瞭解更多。 我根據它提供的解決方案。 我不明白,如果你只是說,「這是行不通的。」請更具體地說明問題。 – ZayedUpal
,我很抱歉,解決方案不工作......如何啓動特定的協程? –