协同程序(coroutine)

来源:互联网 发布:如何加入淘宝网 编辑:程序博客网 时间:2024/05/09 04:15
   什么是协同程序?
       协同程序,即在主程序运行时同时开启另一段逻辑处理,来协同当前程序的执行。换句话说,开启协同程序就是开启一个线程。


       1. Coroutines顾名思议是用来协助主要进程的,在Unity中感觉就是一个可动态添加和移除的Update()函数。它的调用在所有Update函数之后。
       2. yield就像是一个红绿灯,在满足紧跟在它后面的条件之前,这个协程会挂起,把执行权交给调用它的父函数,满足条件时就可以执行yield下面的代码。


       总结起来就是一句话:控制代码在特定的时机执行。






    yield 后面可以有的表达式:
 
       a) null - the coroutine executes the next time that it is eligible
       b) WaitForEndOfFrame - the coroutine executes on the frame, after all of the rendering and GUI is complete
       c) WaitForFixedUpdate - causes this coroutine to execute at the next physics step, after all physics is calculated
       d) WaitForSeconds - causes the coroutine not to execute for a given game time period
       e) WWW - waits for a web request to complete (resumes as if WaitForSeconds or null)
       f) Another coroutine - in which case the new coroutine will run to completion before the yielder is resumed
值得注意的是 WaitForSeconds()受Time.timeScale影响,当Time.timeScale = 0f 时,yield return new WaitForSecond(x) 将不会满足。
0 0
原创粉丝点击