Game Development Reference
In-Depth Information
The yield instruction and coroutine
There are differences syntaxes between C# and JavaScript; they are as follows:
// JavaScript user:
yield WaitForSeconds(3); //pauses for 3 seconds
yield WaitForMyFunction(); //start coroutine
function WaitForMyFunction() {…} //coroutine function
// C# user:
yield return new WaitForSeconds(3); //pauses for 3 seconds
yield return WaitForMyFunction(); //start coroutine
IEnumerator WaitForMyFunction() {…} //coroutine function
Tip
JavaScript will automatically generate the return type to IEnumerator if you put the
yield instruction inside the function. On the other hand, in C#, you will need to specify
the return type to IEnumerator .
However, if we want to wait for the user input in C#, which may be over several frames,
you will have to use StartCoroutine . In JavaScript, the compilers will automatically
do it for you:
// JavaScript user:
yield WaitForMyFunction(5);
//This is similar to
yield StartCoroutine(WaitForMyFunction(5));
function WaitForMyFunction(waitTime : float) {…}
//coroutine function
Search WWH ::




Custom Search