Game Development Reference
In-Depth Information
return;
}
}
The preceding code basically executes
step 0
and
step 1
and then goes back to
step 0
(as a loop), but if there are more events, execution will happen after
step 1
and so on. Too many
if
statements can make the code look ugly in the long run. In this
case, it's more convenient to use the
yield
statement. The
yield
statement is a special
kind of
return
that ensures that the function will continue from the line after the
yield
statement is called the next time. The result could be something like the following code:
// JavaScript user:
function Start() {
while (true) { //Use this line instead of Update()
//do step 0
Debug.Log("Do step 0");
yield; //wait for one frame
//do step 1
Debug.Log("Do step 1");
yield; //wait for one frame
}
}
// C# user
IEnumerator Start() {
while (true) { //Use this line instead of Update()
//do step 0
Debug.Log("Do step 0");
yield return null; //wait for one frame
//do step 1
Debug.Log("Do step 1");
yield return null; //wait for one frame
}
}