Game Development Reference
In-Depth Information
Coroutines
StartCoroutine
returns a
Coroutine
. Instances of this class are only used to refer-
ence these
coroutines
and do not hold any exposed properties or functions.
A
coroutine
is a function that can suspend its
yield
execution until the given
YieldInstruction
class finishes calling.
An example of
coroutine
is as follows:
// JavaScript user:
function Start() {
// Starting = 0.0
Debug.Log ("Starting = " + Time.time);
// Start function WaitAndPrint as a Coroutine
yield WaitAndPrint();
// Done WaitAndPrint = 5.0
Debug.Log ("Done WaitAndPrint = " + Time.time);
}
function WaitAndPrint() {
//Suspend execution for 5 seconds
yield WaitForSeconds(5);
// WaitAndPrint = 5.0
Debug.Log ("WaitAndPrint = " + Time.time);
}
// C# user:
IEnumerator Start() {
// Starting = 0.0
Debug.Log ("Starting = " + Time.time);
// Start function WaitAndPrint as a Coroutine
yield return WaitAndPrint();
// Done WaitAndPrint = 5.0
Debug.Log ("Done WaitAndPrint = " + Time.time);