How can I generate a delay while running an application in Unity?

1

I am creating an application in Unity and need some function that causes a delay during the execution of the application.

    
asked by anonymous 29.09.2015 / 03:26

1 answer

1

For asynchronous execution as well as delay, there are Co-Routines in Unity:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Start() {
        print("Iniciando " + Time.time);
        StartCoroutine(EsperarImprimir(2.0F));
        print("Antes da Co-Routine acabar " Time.time);
    }
    IEnumerator EsperarImprimir(float waitTime) {
        yield return new WaitForSeconds(waitTime);
        print("EsperarImprimir " + Time.time);
    }
}

If you want to continue running the code below only after Co-Routine runs out (only after the delay), use yield return:

public class ExampleClass : MonoBehaviour {
    IEnumerator Start() {
        print("Início " + Time.time);
        yield return StartCoroutine(EsperaImprime(2.0F));
        print("Fim, depois da coroutine executar " + Time.time);
    }
    IEnumerator EsperaImprime(float waitTime) {
        yield return new WaitForSeconds(waitTime);
        print("EsperaImprime " + Time.time);
    }
}

You can also call coroutines by passing function names via String. Anyway, check out the documentation at link

    
29.09.2015 / 03:36