yield学习续:yield return迭代块在Unity3D中的应用——协程

必读好文推荐:函数

Unity协程(Coroutine)原理深刻剖析学习

Unity协程(Coroutine)原理深刻剖析再续this

 

上面的文章说得太透彻,因此这里就记一下本身的学习笔记了。spa

首先要说明的是,协程并非线程,协程是运行在主线程中的,是和主线程同步执行的代码,不一样的地方是运行的方法能够被yield return在当前帧进行打断,到下一帧后能够继续从被打断的地方继续运行。线程

下面咱们看一个示例,场景中有一个空的GameObject对象,其绑定了下面的脚本:3d

复制代码
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class Test : MonoBehaviour
 5 {
 6     int frame = 0;
 7 
 8     void Start ()
 9     {
10         this.StartCoroutine(CountDown());
11     }
12     
13     void Update ()
14     {
15         Debug.Log("Now is frame: " + (++frame));
16     }
17 
18     IEnumerator CountDown()
19     {
20         Debug.Log("step - 1");
21         yield return null;
22         Debug.Log("step - 2");
23         yield return null;
24         Debug.Log("step - 3");
25         yield return null;
26         Debug.Log("step - 4");
27     }
28 }
复制代码

下面是执行的结果:code

下面咱们看看运行的逻辑是如何的:协程

当进入Start方法时开始启动协程,这时候协程开始运行,输出“step1”后遇到第一个yield return后暂停本帧的运行,接下来进入Update方法输出“frame1”,因为协程调用是在Update以后,因此第二帧开始后,先执行了第二个Update输出“frame2”,而后从协程的上次暂停处继续执行,输出“step2”后遇到第二个yield return后暂停本帧的运行,如此反复,当输出“step4”后发现方法已经执行完毕,协程结束。对象

 

下面看看yield break的效果,这个语句会当即中断协程的运行,代码以下:blog

复制代码
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class Test : MonoBehaviour
 5 {
 6     int frame = 0;
 7 
 8     void Start ()
 9     {
10         this.StartCoroutine(CountDown());
11     }
12     
13     void Update ()
14     {
15         Debug.Log("Now is frame: " + (++frame));
16     }
17 
18     IEnumerator CountDown()
19     {
20         Debug.Log("step - 1");
21         yield return null;
22         Debug.Log("step - 2");
23         yield return null;
24         Debug.Log("step - 3");
25         yield break;
26         Debug.Log("step - 4");
27     }
28 }
复制代码

下面是运行的结果:

咱们能够发现“step4”已经运行不到了。

 

yield的返回值,咱们能够返回null或者数字0,效果是一致的,同时还能够返回3个对象,分别以下:

yield return new WaitForFixedUpdate();

·等待直到下一个固定帧速率更新函数。

yield return new WaitForEndOfFrame();

·等待直到全部的摄像机和GUI被渲染完成后,在该帧显示在屏幕以前。

yield return new WaitForSeconds(1);

·在给定的秒数内,暂停协同程序的执行。

 

下面咱们来看一个例子,修改第一个例子的Test.cs:

复制代码
 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 
10     void Start ()
11     {
12         this.StartCoroutine(CountDown());
13         this.StartCoroutine(CountDown_WaitForFixedUpdate());
14         this.StartCoroutine(CountDown_WaitForEndOfFrame());
15         this.StartCoroutine(CountDown_WaitForSeconds());
16     }
17     
18     void Update ()
19     {
20         Debug.Log("Update is frame: " + (++frame1));
21     }
22 
23     void FixedUpdate ()
24     {
25         Debug.Log("FixedUpdate is frame: " + (++frame2));
26     }
27 
28     void LateUpdate ()
29     {
30         Debug.Log("LateUpdate is frame: " + (++frame3));
31     }
32 
33     IEnumerator CountDown()
34     {
35         Debug.Log("yield - step - 1");
36         yield return null;
37         Debug.Log("yield - step - 2");
38         yield return null;
39         Debug.Log("yield - step - 3");
40     }
41 
42     IEnumerator CountDown_WaitForFixedUpdate()
43     {
44         Debug.Log("yield WaitForFixedUpdate - step - 1");
45         yield return new WaitForFixedUpdate();
46         Debug.Log("yield WaitForFixedUpdate - step - 2");
47         yield return new WaitForFixedUpdate();
48         Debug.Log("yield WaitForFixedUpdate - step - 3");
49     }
50 
51     IEnumerator CountDown_WaitForEndOfFrame()
52     {
53         Debug.Log("yield WaitForEndOfFrame - step - 1");
54         yield return new WaitForEndOfFrame();
55         Debug.Log("yield WaitForEndOfFrame - step - 2");
56         yield return new WaitForEndOfFrame();
57         Debug.Log("yield WaitForEndOfFrame - step - 3");
58     }
59 
60     IEnumerator CountDown_WaitForSeconds()
61     {
62         Debug.Log("yield WaitForSeconds - step - 1");
63         yield return new WaitForSeconds(1 / 60 * 3);//大概是三帧的时间
64         Debug.Log("yield WaitForSeconds - step - 2");
65         yield return new WaitForSeconds(1 / 60 * 3);
66         Debug.Log("yield WaitForSeconds - step - 3");
67     }
68 }
复制代码

运行的结果以下,有点长,我就弄成两张图了:

 

经过输出咱们能够得出下面的结果:

  1. 当帧数波动时,FixedUpdate会进行屡次补帧处理,咱们能够发现两张图之间FixedUpdate从3一直补帧到15;
  2. WaitForFixedUpdate表示协程是跟在FixedUpdate以后执行的;
  3. WaitForEndOfFrame表示协程是跟在LateUpdate以后执行的;
  4. WaitForSeconds额。。。不用多说了,你指定多久后执行就多久后执行,固然因为是基于帧运算的,因此可能会不许确;

 

最后补一张开头博客的运行顺序图:

相关文章
相关标签/搜索