Time and frame rate management
Time类: Time script reference page.
一些常见的属性有:
- Time.time 返回从游戏开始经历的时间.
- Time.deltaTime 返回从上帧结束到现在经历的时间,和帧率成反比
- Time.timeScale 控制时间流逝的因子
- Time.fixedDeltaTime 控制fixed update 更新的间隔时间
- Time.maximumDeltaTime 控制最大的deltatime,如果超过这个时间,也会执行一次update
Variable and Fixed time steps
Unity有两个系统分别跟踪 可变的时间间隔 和 不可变的时间间隔
可变的就是update,和帧率有关,不可变的和帧率无关
Variable frame rate management
下边的代码,是每帧移动一个距离,它就会收到帧率的影响,从而每个手机上表现不一致
//C# script example
using UnityEngine;
using System.Collections;public class ExampleScript : MonoBehaviour {public float distancePerFrame;void Update() {transform.Translate(0, 0, distancePerFrame); // this is incorrect}
}
通过和 Time.deltaTime 属性相乘,这样就是每秒执行多长时间,因为帧率再怎么变化,总的时间是不变的:
Fixed Timestep
不像main frame update, Unity’s physics system根据fixed timestep更新, 在每帧的开始,unity会尽可能的多次执行 fixed updates 来赶上当前的时间
fixedDeltaTime 控制的是1秒内执行的步骤数,比如 0.01 表示每个固定的时间步长是持续时间的百分之一秒, 所以每秒有100个固定的时间步长
Unity’s Time Logic
下面的逻辑很重要:

Controlling and handling variations in time
如上所述,每帧之间的时间间隔可能会有所变化。
The properties explained in this section are:
- Time.time
- Time.unscaledTime
- Time.deltaTime
- Time.unscaledDeltaTime
- Time.smoothDeltaTime
- Time.timeScale
- Time.maximumDeltaTime
Time.time 表示玩家开始游戏后经过的时间,所以通常是连续稳定地上升.
Time.deltaTime 表示自上一帧以来经过的时间量,因此理想情况下保持相当恒定
Time.timeScale 为0会暂停游戏,Update 方法依然执行,不过 Time.time 不会增加了,且Time.deltaTime 为0.
Time.unscaledTime、Time.unscaledDeltaTime不收缩放因子的影响,这对UI动画有用 
The table below shows an example of 16 frames elapsing one after another, with one large delay occuring half-way through, on a single frame. These figures illustrate how the various Time class properties report and respond to this large variation in frame rate.
| Frame | unscaledTime | time | unscaledDeltaTime | deltaTime | smoothDeltaTime | 
|---|---|---|---|---|---|
| 1 | 0.000 | 0.000 | 0.018 | 0.018 | 0.018 | 
| 2 | 0.018 | 0.018 | 0.018 | 0.018 | 0.018 | 
| 3 | 0.036 | 0.036 | 0.018 | 0.018 | 0.018 | 
| 4 | 0.054 | 0.054 | 0.018 | 0.018 | 0.018 | 
| 5 | 0.071 | 0.071 | 0.017 | 0.017 | 0.018 | 
| 6 | 0.089 | 0.089 | 0.018 | 0.018 | 0.018 | 
| 7 | 0.107 | 0.107 | 0.018 | 0.018 | 0.018 | 
| 8 (a) | 1.123 (b) | 0.440 (c) | 1.016 (d) | 0.333 (e) | 0.081 (f) | 
| 9 | 1.140 | 0.457 | 0.017 | 0.017 | 0.066 | 
| 10 | 1.157 | 0.474 | 0.017 | 0.017 | 0.056 | 
| 11 | 1.175 | 0.492 | 0.018 | 0.018 | 0.049 | 
| 12 | 1.193 | 0.510 | 0.018 | 0.018 | 0.042 | 
| 13 | 1.211 | 0.528 | 0.018 | 0.018 | 0.038 | 
| 14 | 1.229 | 0.546 | 0.018 | 0.018 | 0.034 | 
| 15 | 1.247 | 0.564 | 0.018 | 0.018 | 0.031 | 
| 16 | 1.265 | 0.582 | 0.018 | 0.018 | 0.028 | 
如果不存在任何限制,那么由deltaTime缩放的物体就能够在帧速率高峰期间穿过游戏中的墙壁,因为从理论上讲,物体从一帧移动到下一帧的距离是没有限制的,所以它可以在一帧内从障碍物的一边跳到另一边而不会与之相交。因为还没有到on collider ***的检测

可以在上面的第8帧中看到,unscaledDeltaTime (d)和deltaTime (e)经过时间不同。尽管在第7帧和第8帧之间经过了整整一秒的实际时间,但deltaTime报告的时间仅为0.333秒。这是因为deltaTime被限制为maximumDeltaTime值。

类似地,unscaledTime (b)增加了大约整整一秒,因为添加了真实的(未缩放)值,而time (c)只增加了较小的偏移值。时间值没有赶上实际的时间,而是表现得好像延迟的持续时间只有maximumDeltaTime。

Time.smoothDeltaTime 属性表示最近deltaTime值的近似值,并根据算法平滑所有变化。 这是另一种技术,可以避免不必要的大步幅或移动波动或其他基于时间的计算。 In particular, those which fall below the threshold set by maximumDeltaTime. 平滑算法不能预测未来的变化,但它逐渐调整其值来平滑最近经过的增量时间值的变化,从而使平均报告时间保持与实际经过的时间量大致相等。
Time variation and the physics system
maximumDeltaTime也会影响 physics system.就像之前图片中提到的,如果它特别大的话,可能每帧之间需要执行的fixed update 比较多,会带来额外的压力
如果一个帧更新花费的时间超过了maximumDeltaTime,物理引擎
 不会尝试模拟超过maximumDeltaTime的任何时间,而是让帧处理赶上。一旦帧更新完成,物理就会恢复,就好像它停止后没有时间过去一样。这样做的结果是,物理对象不会像通常那样完美地实时移动,而是会稍微放慢速度。然而,物理“时钟”仍然会跟踪它们,就好像它们在正常移动一样。物理时间的变慢通常是不明显的,通常是可以接受的