Unity协程(一):彻底了解yield return null 和 yield return new WaitForSeconds
2017-05-23 22:09
330 查看
unity的协程毫无疑问是个非常棒的设计,让我们能够非常方便的编写类似“异步”代码,提高开发效率。
在开发过程中用到最多的当属yield return null,yield return new WaitForSeconds 和 yield return new WaitForEndOfFrame了;
WaitForEndOfFrame,顾名思义是在等到本帧的帧末进行在进行处理,这个问题不大,比较不容易搞错。
yield return null表示暂缓一帧,在下一帧接着往下处理,也有人习惯写成yield return 0或者yield return 1,于是误区就随之而来了,很多同学误认为yield return后面的数字表示的是帧率,比如yield return 10,表示的是延缓10帧再处理,实则不然,yield return num;的写法其实后面的数字是不起作用的,不管为多少,表示都是在下一帧接着处理。
yield return new WaitForSeconds,这个要注意的是1·实际时间等于给定的时间乘以Time.timeScale的值。2·触发间隔一定大等于1中计算出的实际时间,而且误差的大小取决于帧率,因为它是在每帧处理协程的时候去计算时间间隔是否满足条件,如果满足则继续执行。例如,当帧率为5的情况下,一帧的时间为200ms,这时即使时间参数再小,最快也要200ms之后才能继续执行剩余部分。
接下来再来看一张图:
这是一张关于MonoBehaviour的执行顺序图关于协程的部分,由图可见,yield 是在yield WaitForSeconds之前处理的,再结合上段的分析可以得出一个结论:在同一帧里执行的两个协程,不论先后关系如何,不论WaitForSeconds给定的值为多少,yield return null所在的协程都要比yield return new WaitForSeconds的协程更先执行。同类型的协程则跟其开启的先后顺序相关:
void Start() { StartCoroutine(Coroutine3()); StartCoroutine(Coroutine2()); StartCoroutine(Coroutine1()); } IEnumerator Coroutine1() { yield return null; Debug.Log("Coroutine1 function"); } IEnumerator Coroutine2() { yield return null; Debug.Log("Coroutine2 function"); } IEnumerator Coroutine3() { yield return new WaitForSeconds(0.000001f); Debug.Log("Coroutine3 function"); }结果如下:
最后再提个点,yield return null和yield return new WaitForSeconds协程最好别一起混着用,特别是同时开启的这两个协程还有相互依赖的关系,因为帧率是不稳定的,所以有可能引起某些非必现的bug。
相关文章推荐
- unity, yield return new WaitForSeconds(waitTime) 在 Time.timeScale=0下卡死
- 关于unity中使用yield return newWaitForSeconds()后面语句不执行
- 【Unity】理解协程的原理1——实现一个自己的WaitForSeconds
- 了解WaitForSingleObject中WAIT_ABANDONED 返回值
- 关于unity中C#使用WaitForSeconds的方法
- unity3d之 C# WaitFOrSeconds()
- 191 You observed the following output for a user session: SQL > SELECT sid, event, seconds _in _wait
- PYQT TIP : QSpinBox signal waits for return key to be pressed
- 实现一个Unity3d的WaitForSeconds
- ognl.OgnlException: target is null for setProperty(null, "newsid", [Ljava.lan
- unity中invoke*和waitforseconds在停止携程后的表现不同
- Unity3d中协程的原理,你要的yield return new xxx的真正理解之道
- aop.AopInvocationException: null return value from advice does not match primitive return type for
- unity3d之 C# WaitFOrSeconds()
- android sip 开发中SipManager.newInstance()return null 解决方法
- unity waitforseconds
- C++ "new" does not return 0 ("NULL") on failure!
- return null 与return new List<HaierPageView>()的区别
- 了解WaitForSingleObject
- Spring之AOP奇葩报错:Null return value from advice does not match primitive return type for