如何在检测到每个新Trackable后重置Coroutine?

时间:2016-01-13 02:19:48

标签: c# unity3d augmented-reality vuforia unity5

Coroutine需要在20秒后或检测到新的可跟踪时重置

{{1}}

3 个答案:

答案 0 :(得分:3)

使用InvokeRepeatingCancelInvoke可以执行以下操作:

在开始/清醒方法中:

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);
    ...
}

然后在碰撞方法中:

void OnCollisionEnter(Collision collision){
    ...
    // Cancel the invoke to 'reset' it
    CancelInvoke("VoicePrompt");
    // And start it again
    InvokeRepeating("VoicePrompt", 0f, 20f);
    ...
}

答案 1 :(得分:0)

我不知道什么是可跟踪的,但如果您想使用Coroutine,那么您可以在Coroutine中使用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

答案 2 :(得分: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);       
          }