Как я могу сбросить Coroutine после обнаружения каждого нового Trackable?

Coroutine необходимо сбросить через 20 секунд или при обнаружении нового отслеживаемого объекта.

IEnumerator VoicePrompt()
        {

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

        } 

person Butrint    schedule 13.01.2016    source источник
comment
Взгляните на InvokeRepeating и CancelInvoke. Вероятно, вы сможете найти решение, объединив эти методы.   -  person xtheosirian    schedule 13.01.2016


Ответы (3)


Используя InvokeRepeating и CancelInvoke вы можете сделать что-то вроде этого:

В методе Start/Awake:

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);
    ...
}
person xtheosirian    schedule 13.01.2016

Я не знаю, что отслеживается, но если вы хотите использовать для этого Coroutine, вы можете использовать flag в своей Coroutine, и когда вы обнаружите новые отслеживаемые, измените флаг на 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
person Hossein Rashno    schedule 13.01.2016
comment
отслеживаемый — это игровой объект, который создается во время выполнения, когда вы показываете камере конкретную флэш-карту; например, когда вы показываете флэш-карту с Nr. 9, то цель будет обнаружена. Это дополненная реальность. Я попробовал ваш код, но, похоже, он не сбрасывает значение при обнаружении нового отслеживаемого объекта. - person Butrint; 14.01.2016

Это код и 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);       
          }
person Butrint    schedule 14.01.2016