转自:http://blog.csdn.net/lyh916/article/details/44133003
总结如下:
1.timeScale不影响Update和LateUpdate,会影响FixedUpdate
2.timeScale不影响Time.realtimeSinceStartup,会影响Time.timeSinceLevelLoad和Time.time
3.timeScale不影响Time.fixedDeltaTime和Time.unscaleDeltaTime,会影响Time.deltaTime
测试代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using UnityEngine; using System.Collections; public class TestTimeScale : MonoBehaviour { // Update is called once per frame void Update () { if (Input.GetKeyDown(KeyCode.Q)) Time.timeScale = 0; if (Time.timeScale == 0) { print("Update"); print("Time.time:" + Time.time); print("Time.timeSinceLevelLoad:" + Time.timeSinceLevelLoad); print("Time.realtimeSinceStartup:" + Time.realtimeSinceStartup); print("Time.deltaTime:" + Time.deltaTime); print("Time.fixedDeltaTime:" + Time.fixedDeltaTime); print("Time.unscaledDeltaTime:" + Time.unscaledDeltaTime); } //print("Time.timeSinceLevelLoad:" + Time.timeSinceLevelLoad); } void LateUpdate() { if (Time.timeScale == 0) print("LateUpdate"); } void FixedUpdate() { if (Time.timeScale == 0) print("FixedUpdate"); } } |
分别对部分代码进行注释,测试结果如下:
当timeScale等于0时:
1.Update和LateUpdate可以执行,FixedUpdate不可以执行
2.Time.realtimeSinceStartup依然在增加,Time.timeSinceLevelLoad和Time.time均不变
3.Time.fixedDeltaTime不变,Time.deltaTime变为0,Time.unscaleDeltaTime就像游戏正常速度运行下的Time.deltaTime
再次总结:
当想代码受timeScale控制时(如暂停、加速),可以把代码放在FixedUpdate中,又或者跟Time.time或Time.deltaTime扯上关系;否则,可以用Time.realtimeSinceStartup(类似正常状态下的Time.time)和Time.unscaleDeltaTime(类似正常状态下的Time.deltaTime)
- 本文固定链接: http://www.u3d8.com/?p=873
- 转载请注明: 网虫虫 在 u3d8.com 发表过