简体   繁体   English

检测到每个新的Trackable后,如何重置协程?

[英]How can I reset the Coroutine after each new Trackable detected?

The Coroutine needs to reset after 20 seconds or when a new trackable is detected 协程需要在20秒后或检测到新的可跟踪对象时重置

IEnumerator VoicePrompt()
        {

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

        } 

By using InvokeRepeating and CancelInvoke you can do something like this: 通过使用InvokeRepeatingCancelInvoke,您可以执行以下操作:

In the Start/Awake method: 在启动/唤醒方法中:

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

Then in the Collision method: 然后在Collision方法中:

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

I don't know what is trackable, but if you want to use Coroutine for this then you can use a flag in your Coroutine and when you detect new trackable, change the flag to True : 我不知道什么是可跟踪的,但是如果您要为此使用协程,则可以在协程中使用一个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

This is the code and the OnTrackingFound() below the coroutine: 这是代码和协程下面的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM