Game Development Reference
In-Depth Information
StartCoroutine
Starts a corouine.
The execuion of a corouine can be paused at any point using the
yield
statement. The
yield
return value speciies when the corouine is resumed. Corouines are excellent when
modeling behavior over several frames. Corouines have virtually no performance overhead.
StartCoroutine
funcion always returns immediately, however you can yield the result.
This will wait unil the corouine has inished execuion.
When using JavaScript it is not necessary to use
StartCoroutine
,
as the compiler will do this for you. When wriing C# code you must
call
StartCoroutine
. (For more details, refer to
Appendix C
,
Major differences Between C# and Unity JavaScript.
)
In the following example, we show how to invoke a
coroutine
and coninue execuing the
funcion in parallel:
function Start() {
// Starting = 0.0
Debug.Log ("Starting = " + Time.time);
// StartCoroutine WaitAndPrint (In JavaScript, you can also use
WaitAndPrint(5.0)
which will get the same result.
StartCoroutine(WaitAndPrint(5.0));
// Before WaitAndPrint = 5.0
Debug.Log ("Before WaitAndPrint = " + Time.time);
}
function WaitAndPrint(waitTime : float) {
//Suspend execution for 5 seconds
yield WaitForSeconds(waitTime);
// WaitAndPrint = 5.0
Debug.Log ("WaitAndPrint = " + Time.time);
}