Unity startcoroutine and yield return

Source: Internet
Author: User

Startcoroutine and yield return have a good understanding of the surface meaning. startcoroutine is to start a coroutine, and yield return is the place where the iterator Block returns the call iteration.

Right? I don't know how you feel. I think it is still necessary to study it in depth. OK, here we go!

 

First, let's take a look at startcoroutine's official explanation in unity.

The yield statement can be used to suspend the execution of a coroutine. The value of yield return determines when the coroutine resumes execution. Coroutine is of great use in coordinating operations executed in several frames. coroutine has almost no performance overhead.

Startcoroutine usually returns immediately, but you can also obtain the value of the returned result. However, this step takes effect only when the coroutine is finished.

 

OK, it is not difficult to understand the meaning. We will analyze a program according to his meaning.

The running result is:

Start1

Test1

Start2

Test2

 

This is clear at a glance. When startcoroutine just called, it can be understood as a normal function call, and then look at the called function.

When the called function is executed to yield return NULL; (pause the coroutine and wait for the next frame to continue execution), The coroutine program will be paused according to unity interpretation, in fact, I personally think that his explanation is not accurate enough. First, return to the place where the coroutine starts, and then pause the coroutine. That is to say, first notify the call, "let's go first, don't worry about me", and then pause the coroutine .. Why? Believe it? Then let's write a demo to verify it.

Execution result:

Start1

Test1

Start2

Test2 (this Test2 is printed three seconds later)

 

Just by the way to verify the "yield return value determines when the coroutine resume execution" this sentence, in fact, yield return after the value can be a lot of usage, you can see this post: http://blog.sina.com.cn/s/blog_aaa4ce8d010131kr.html

In fact, looking back, coroutine> collaborative program> is actually a collaboration between two tasks. On the surface, it looks very simple, but it is used in some slightly larger projects, it will be a bit obscure for new users...

For example

Ienumerator Init () {yield return startcoroutine (init1 (); debug. log ("init1 finish"); yield return startcoroutine (init2 (); debug. log ("init2 finish"); yield return startcoroutine (init3 (); debug. log ("init3 finish");} ienumerator init1 () {// simulate initializing yield return New waitforseconds (2); //} ienumerator init2 () {// do somthing .. yield return New waitforseconds (2); //} ienumerator init2 () {// do somthing .. yield return New waitforseconds (2 );//}

In fact, it is a problem of execution sequence. This ensures that init1, init2, and init3 are executed one by one, so that the Code executed later does not reference a variable not initialized previously...

Next, let's take a look at the following code for the execution sequence.

     Void  Start () {Debug. Log (  " Start1  "  ); Startcoroutine (test (); debug. Log (  "  Start2  "  );} Ienumerator test () {Debug. Log (  "  Test1  "  );  Yield   Return  Startcoroutine (dosomething (); debug. Log (  " Test2  "  );} Ienumerator dosomething () {Debug. Log (  "  Load 1  "  );  Yield   Return   Null  ; Debug. Log (  "  Load 2  "  );} 

Execution result:

Start1

Test1

Load1

Start2

Load2

Test2

In this startcoroutine, a yield return startcoroutine is nested. The first startcoroutine waits until all the code in the second startcoroutine is executed, and the second startcoroutine yield statement returns the first one first, then, the system immediately returns his call location, that is, the call area will continue to run, and the first startcoroutine will wait until the second execution is complete before continuing the execution.

If you want to continue, take a look at the iterator in C #, which shows what yield and ienumerator are.

OK. Let's talk about this today. Good luck!

 

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.