[英]How can I reset the Coroutine after each new Trackable detected?
協程需要在20秒后或檢測到新的可跟蹤對象時重置
IEnumerator VoicePrompt()
{
yield return new WaitForSeconds (20f);
FindTheCard.Play ();
currentPointSound.GetComponent<AudioSource> ().PlayDelayed (2.3f);
}
通過使用InvokeRepeating和CancelInvoke,您可以執行以下操作:
在啟動/喚醒方法中:
void Start(){
...
// Start the repetition by calling the InvokeRepeating
// 2nd argument (0f) is the delay before first invocation
// 3rd argument (20f) is the time between invocations
InvokeRepeating("VoicePrompt", 0f, 20f);
...
}
然后在Collision方法中:
void OnCollisionEnter(Collision collision){
...
// Cancel the invoke to 'reset' it
CancelInvoke("VoicePrompt");
// And start it again
InvokeRepeating("VoicePrompt", 0f, 20f);
...
}
我不知道什么是可跟蹤的,但是如果您要為此使用協程,則可以在協程中使用一個flag
,當您檢測到新的可跟蹤時,將標志更改為True
:
bool isNeedToRun = true;
IEnumerator flagChangeCounter()
{
While (true)
{
isNeedToRun = true;
yield return new WaitForSeconds (20f);
}
}
IEnumerator VoicePrompt()
{
While (true)
{
While (!isNeedToRun) //Loop until (isNeedToRun == true)
{
yield return new WaitForSeconds (0.1f);
}
FindTheCard.Play ();
currentPointSound.GetComponent<AudioSource> ().PlayDelayed (2.3f);
isNeedToRun = false;
}
}
// And change the 'isNeedToRun' value to 'true' when you detect new trackable
這是代碼和協程下面的OnTrackingFound():
private IEnumerator Countdown(int time){
while(time>0){
Debug.Log(time--);
yield return new WaitForSeconds(1);
}
FindTheCard.Play ();
currentPointSound.GetComponent<AudioSource> ().PlayDelayed (2.3f);
Debug.Log("Countdown Complete!");
}
public void OnTrackingFound()
{
show.SetActive (true);
Renderer[] rendererComponents = GetComponentsInChildren<Renderer> (true);
Collider[] colliderComponents = GetComponentsInChildren<Collider> (true);
AudioSource[] audiocomponents = GetComponentsInChildren<AudioSource> (true);
StartCoroutine ("Countdown", 20);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.