协同程序(coroutine)

   什么是协同程序?
       协同程序,即在主程序运行时同时开启另一段逻辑处理,来协同当前程序的执行。换句话说,开启协同程序就是开启一个线程。


       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) 将不会满足。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值