Time.timeScale 对 协程WaitForSeconds的影响
2016-03-14 23:56
513 查看
Unity中的
yield return WaitForSeconds(1.0f);
大概等价于下面的这些语句:
float timer = Time.time + 1.0f;
while (Time.time < timer) {
yield return null;
}
这就是为什么下面的语句会有问题,游戏暂停后,过了3秒依然不会恢复
IEnumerator Resume ()
{
Time.timeScale = 0;
yield return new WaitForSeconds (3);
Time.timeScale = 1;
}
因为前面把Time.timeScale设置为0,时间已经不走了,所以WaitForSeconds函数中的Time.time一直都是同一个值,所以永远不会达到Time.time < timer的返回条件
有时间拿reflector反编译下unity代码看看WaitForSeconds的真实实现。
yield return WaitForSeconds(1.0f);
大概等价于下面的这些语句:
float timer = Time.time + 1.0f;
while (Time.time < timer) {
yield return null;
}
这就是为什么下面的语句会有问题,游戏暂停后,过了3秒依然不会恢复
IEnumerator Resume ()
{
Time.timeScale = 0;
yield return new WaitForSeconds (3);
Time.timeScale = 1;
}
因为前面把Time.timeScale设置为0,时间已经不走了,所以WaitForSeconds函数中的Time.time一直都是同一个值,所以永远不会达到Time.time < timer的返回条件
有时间拿reflector反编译下unity代码看看WaitForSeconds的真实实现。
相关文章推荐
- LeetCode #11 Container With Most Water 最大容器 解题小节
- #345 Div2 B. Beautiful Paintings
- 在 Windows 上遇到非常多 TIME_WAIT 連線時應如何處理
- 服务器TIME_WAIT和CLOSE_WAIT详解和解决办法
- 我的人工智能学习之路(1)
- CLOSE_WAIT状态的原因与解决方法
- HDU 2674 N!Again (数论)
- 深度学习之google deepmind的alphago AI人工智能算法技术演变历程
- What does 'if __name__=='__main__' do
- poj3250Bad Hair Day(单调栈)
- leetcode 70. Climbing Stairs
- HDU.1022 Train Problem I【栈的简单应用】(3.14)
- HDU4300 Clairewd’s message
- 【leetcode】第11题:Container问题
- 关灯游戏AI扩展
- git RPC failed;result=18
- Socket bind failed: [730048] ͨ³£ÿ¸öÌÓ×ֵØַ(ЭÒé/ÍøÂçµØַ/¶˿Ú)ֻÔÊÐíʹÓ
- EventMailer使用方法
- ERROR: Upgrade faifailed bin/mysqlcheck: Got error: 1045: Access denied for user 'root'@'localhost'
- 用TextPaint来绘制文字