Unity3d coroutines & Yield)

Source: Internet
Author: User

Game writingCode, The code is usually a continuous event. The result will look like this:
[It can implementProgramDelayed execution or continuous execution of each part in a period of time.]

 
Private int state = 0; void Update () {If (State = 0) {// do step 0 state = 1; return;} If (State = 1) {// do step 1 State = 2; return ;}//...}

The yield statement is more convenient. The yield statement is a special return type, which ensures that the function continues to be executed from the next line of the yield statement.

While (true) {// do step 0 yield return 0; // wait for one frame // do step 1 yield return 2; // wait for two frames //...}

You can also pass the time value to the yield statement. The update function will execute the next statement after yield ends.

 
// Do something yield return waitforseconds (5.0); // wait for 5 seconds // do something more...


You can log on to the stack and connect to the coroutine.

In this example, do is executed, but the print command after the do function is executed immediately.

 
Do (); console. writeline ("this is printed immediately"); ienumerator do () {console. writeline ("Do Now"); yield return New waitforseconds (2); console. writeline ("do 2 seconds later ");}

In this example, do is executed, and wait until the DO statement is completed before other statements are executed. [Note: here the wait refers to handing over the thread time to other tasks, rather than blocking wait]

 
// Start the coroutine yield return startcoroutine ("do"); console. writeline ("also after 2 seconds"); console. writeline ("this print will be displayed after the do coroutine is executed. "); Ienumerator do () {console. writeline (" Do Now "); yield return New waitforseconds (2); console. writeline (" do 2 seconds later ");}

Any event handler can be a collaborative program.

Note that you cannot use yield in the update or fixedupdate functions, but you can use startcoroutine to start a function.

View more information about yieldinstruction, waitforseconds, waitforfixedupdate, coroutine and monobehaviour. startcoroutine.
Yield return can be considered as a special return and will be returned to the parent class for further execution. However, the type or method after yield return has an execution condition, when the condition is met, a subfunction containing yield will be called back. For example, the following code
Example 1:

Void start () {print ("starting:" + time. time); startcoroutine (waitanprint (2.0f); print ("before waiandprint:" + time. time);} ienumerator waitandprint (float waittime) {yield return New waitforseconds (waittime); print ("waitandprint:" + time. time );}

When yield return New waitforseconds (waittime) is executed, the pause condition is not met, so it is returned to the start function for continued execution until the condition is met and then the waitandprint is called back. Therefore, the output is:

Starting: 0

Before waiandprint: 0

Waitandprint: 2.12291

Example 2:

Ienumerator start () {print ("starting:" + time. time); yield return startcoroutine (waitandprint (2.0f); print ("done:" + time. time);} ienumerator waitandprint (float waittime) {yield return New waitforseconds (waittime); print ("waitandprint:" + time. time );}

Because start is a top-level function, it will be blocked here until startcoroutine (waitandprint (2.0f) is executed and the output is:

Starting: 0

Waitandprint: 2.00315

Done: 2.00315

 
 

 
 
     
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.