YieldInstruction and coroutine

There are differences in the syntax of C# and JavaScript as follows:

JavaScript:

yield WaitForSeconds(3);          //pauses for 3 seconds
yield WaitForMyFunction();          //start coroutine 

function WaitForMyFunction() {...}      //coroutine function

C#:

yield return new WaitForSeconds(3);    //pauses for 3 seconds
yield return WaitForMyFunction();    //start coroutine

IEnumerator WaitForMyFunction() {...}    //coroutine function

Tip

In JavaScript, it will automatically generate the return type to IEnumerator if you put 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 might be over several frames, we will have to use StartCoroutine. In JavaScript, the compilers will automatically do it for us.

JavaScript:

yield WaitForMyFunction(5);  
//This is similar with
yield StartCoroutine(WaitForMyFunction(5)); 

function WaitForMyFunction(waitTime : float) {...}  
//coroutine function

C#:

//Need to put StartCoroutine
yield return StartCoroutine(WaitForMyFunction(5)); 

IEnumerator WaitForMyFunction(waitTime : float) {...}  
//coroutine function
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.96.86