2016-01-13 37 views
0

協程需要在檢測到新的可追蹤如何在每次檢測到新的Trackable後重新設置協程?

IEnumerator VoicePrompt() 
     { 

      yield return new WaitForSeconds (20f); 
      FindTheCard.Play(); 
      currentPointSound.GetComponent<AudioSource>().PlayDelayed (2.3f); 

     } 
+1

看一看[InvokeRepeating](http://docs.unity3d.com/ScriptReference/MonoBehaviour.InvokeRepeating.html)和[CancelInvoke]( http://docs.unity3d.com/ScriptReference/MonoBehaviour.CancelInvoke.html)。您可以通過耦合這些方法來提出解決方案。 – TheOsirian

回答

3

通過使用InvokeRepeatingCancelInvoke你可以做這樣的事情後20秒重置或:

在開始/喚醒方法:

void OnCollisionEnter(Collision collision){ 
    ... 
    // Cancel the invoke to 'reset' it 
    CancelInvoke("VoicePrompt"); 
    // And start it again 
    InvokeRepeating("VoicePrompt", 0f, 20f); 
    ... 
} 
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); 
    ... 
} 
在碰撞方法

然後0

0

我不知道什麼是可追蹤的,但如果你想使用協程爲這個,那麼你可以在你的協程使用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 
+0

trackable是一個gameObject,它是在運行時爲相機顯示特定的FlashCard時創建的;對於e.x,當您用Nr顯示閃存卡時。 9然後目標將被檢測到。這是增強現實。我試過你的代碼,但它似乎沒有檢測到新的trackable時重置該值 – Butrint

0

這是協程下面的代碼和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);  
      } 
相關問題