I am creating an application in Unity and need some function that causes a delay during the execution of the application.
I am creating an application in Unity and need some function that causes a delay during the execution of the application.
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