Yield Learning Continuation: The application of yield return iteration block in Unity3d--the Association Process

Source: Internet
Author: User

Must read Good article recommended:

The Unity Process (coroutine) principle in-depth analysis

The Unity Process (coroutine) principle in-depth analysis and continuation

The above article is too thorough, so remember your study notes here.

The first thing to note is that the process is not a thread, the process is running in the main thread, and the main thread synchronous execution of the code, the difference is that the method of operation can be the yield return at the current frame to break, to the next frame can continue to run from the interrupted place.

Let's look at an example where there is an empty Gameobject object in the scene that binds the following script:

1 using Unityengine; 2 using System.Collections; 3  4 public class Test:monobehaviour 5 {6     int frame = 0; 7  8     void Start () 9     {Ten this         . Startcoroutine (Countdown ());     }12     void Update ()     {         Debug.Log ("Now is frame:" + (++frame );     }17     IEnumerator Countdown () (         "step-1");         yield return null;22         Debug.Log ("step-2");         yield return null;24         Debug.Log ("step-3");         yield return null;26         Debug.Log ("step-4");     }28}

Here is the result of the execution:

Let's take a look at how the logic works:

When the Start method begins to start the process, this time the process starts running, the output "Step1" after the first yield return to pause the operation of the frame, and then enter the Update method output "Frame1", because the call is after the update, So after the second frame starts, executes the second update output "Frame2", and then proceeds from the last pause of the co-operation, the output "Step2" after the second yield return after the execution of the pause this frame, so repeatedly, when the output "Step4" after the method has been executed , the co-process ends.

Let's look at the effect of yield break, which immediately interrupts the run of the process, with the following code:

1 using Unityengine; 2 using System.Collections; 3  4 public class Test:monobehaviour 5 {6     int frame = 0; 7  8     void Start () 9     {Ten this         . Startcoroutine (Countdown ());     }12     void Update ()     {         Debug.Log ("Now is frame:" + (+ +) (frame)),     }17     IEnumerator Countdown () (         "step-1"), and         yield return null;         Debug.Log ("step-2");         yield return null;24         Debug.Log ("step-3");         yield break;26         Debug.Log ("step-4");     }28}

Here is the result of the run:

We can see that "STEP4" has not been run.

Yield return value, we can return null or the number 0, the effect is consistent, but also can return 3 objects, respectively, as follows:

Yield return new waitforfixedupdate ();

• Wait until the next fixed frame rate update function.

Yield return new waitforendofframe ();

• Wait until all the cameras and the GUI are rendered after the frame is displayed before the screen.

Yield return new waitforseconds (1);

• Pauses the execution of the synergistic program within a given number of seconds.

Let's look at an example to modify the Test.cs of the first example:

 1 using Unityengine; 2 using System.Collections;  3 4 public class Test:monobehaviour 5 {6 int frame1 = 0; 7 int frame2 = 0; 8 int frame3 = 0; 9 void Start () {this. Startcoroutine (Countdown ()); Startcoroutine (Countdown_waitforfixedupdate ()); Startcoroutine (Countdown_waitforendofframe ()); Startcoroutine (Countdown_waitforseconds ());}17 void Update () {Debug.Log ("Update is FRA Me: "+ (++FRAME1)"),}22 fixedupdate () ("Fixedupdate is frame:" + (++frame2     );}27 lateupdate () {Debug.Log ("Lateupdate is frame:" + (++frame3)); 31}32 33 IEnumerator countdown () {Debug.Log ("yield-step-1"); yield return null;37 Debug.lo G ("yield-step-2"); yield return null;39 Debug.Log ("yield-step-3");}41 Countdown_waitfoRfixedupdate () (DEBUG.LOG) ("Yield waitforfixedupdate-step-1"), yield return new waitforfixed         Update (); Debug.Log ("Yield waitforfixedupdate-step-2"); yield return new waitforfixedupdate (); 48     Debug.Log ("yield waitforfixedupdate-step-3");}50 IEnumerator Countdown_waitforendofframe () 52 {Debug.Log ("yield waitforendofframe-step-1"); yield return new waitforendofframe (); Ug. Log ("Yield waitforendofframe-step-2"), the yield return to new Waitforendofframe (); Debug.Log ("Yield Wait Forendofframe-step-3 "),}59 IEnumerator countdown_waitforseconds () Debug.Log (" Yield Wai Tforseconds-step-1 "), Waitforseconds yield return new (1/60 * 3);//approx. three frame time (" yield Wait ") Debug.Log Forseconds-step-2 "); yield return new Waitforseconds (1/60 * 3); Debug.Log (" Yield waitforseconds -step-3 "); 67}68} 

The result of the operation is as follows, a bit long, I made two pictures:

With the output we can draw the following result:

    1. When the number of frames fluctuates, fixedupdate will perform multiple frame processing, we can find that between the two graphs fixedupdate from 31 straight to 15;
    2. Waitforfixedupdate that the co-process was followed by fixedupdate;
    3. Waitforendofframe that the co-process was followed by lateupdate;
    4. Waitforseconds amount ... Needless to say, you specify how long after the execution of the execution, of course, because it is based on frame operations, so may be inaccurate;

Finally, fill in a sequence diagram of the opening blog:

Yield Learning Continuation: The application of yield return iteration block in Unity3d--the Association Process

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.