Version: 2021.1
ScriptableObject
重要的类 - Mathf

Important Classes - Time and frame rate management

Time 类

Unity 的 Time 类提供许多重要的基本属性,允许您在项目中使用与时间相关的值。

时间脚本参考页面每个成员的描述是不言自明的,这是了解它们的最佳场所。

这里列出了一小部分示例,以提供常见和典型用途的概念:

Time.time 返回自项目开始播放以来的时间量。

Time.deltaTime 返回自上一帧完成以来经过的时间量。

Time.timeScale 表示时间流逝的速率。您可以读取此值,或将其设置为控制时间流逝的速度,从而创建慢动作效果。

有关时间相关属性的完整列表,请参阅时间脚本参考页面

Frame rate management

The Update function allows you to monitor inputs and other events regularly from a script and take appropriate action. For example, you might move a character when the “forward” key is pressed. An important thing to remember when handling time-based actions like this is that the game’s frame rate is not constant and neither is the length of time between Update function calls.

举例来说,假设在一项任务中需要逐步向前移动某个对象,一次一帧。起初看起来好像可以在每帧将对象移动一个固定距离:

//C# 脚本示例
using UnityEngine;
using System.Collections;

public class ExampleScript : MonoBehaviour {
    public float distancePerFrame;
    
    void Update() {
        transform.Translate(0, 0, distancePerFrame);
    }
}

但是,如果帧时间不是恒定的,那么对象看起来会以不规则的速度移动。如果帧时间为 10 毫秒,那么对象将以 distancePerFrame 的距离每秒前进一百次。但如果帧时间增加到 25 毫秒(比如由于 CPU 负载的原因),那么对象每秒只会前进四十次,因此移动的总距离更短。解决方案是通过可从 Time.deltaTime 属性读取的帧时间来缩放移动距离大小:

//C# 脚本示例
using UnityEngine;
using System.Collections;

public class ExampleScript : MonoBehaviour {
    public float distancePerSecond;
    
    void Update() {
        transform.Translate(0, 0, distancePerSecond * Time.deltaTime);
    }
}

Note that the movement is now given as distancePerSecond rather than distancePerFrame. As the frame rate changes, the size of the movement step will change accordingly and so the object’s speed will be constant.

Depending on your target platform, use either Application.targetFrameRate or QualitySettings.vSyncCount to set the frame rate of your application. For more information, see the Application.targetFrameRate API documentation.

固定时间步长

与主帧更新不同,Unity 的物理系统_会_工作到固定的时间步长,这对于模拟的准确性和一致性很重要。在物理更新开始时,Unity 通过将固定的时间步长值添加到上次物理更新结束的时间来设置“警报”时间。然后,物理系统将执行计算,直到警报响起。

可从 Time 窗口中更改固定时间步长的大小,并可使用 Time.fixedDeltaTime 属性从脚本中读取该值。请注意,较低的时间步长值将产生更频繁的物理更新和更精确的模拟,但代价是更大的 CPU 负载。除非要对物理引擎提出很高的要求,否则可能不需要更改默认的固定时间步长。

Maximum Allowed Timestep

The fixed timestep keeps the physical simulation accurate in real time but it can cause problems in cases where the game makes heavy use of physics and the gameplay frame rate has also become low (due to a large number of objects in play, say). The main frame update processing has to be “squeezed” in between the regular physics updates and if there is a lot of processing to do then several physics updates can take place during a single frame. Since the frame time, positions of objects and other properties are frozen at the start of the frame, the graphics can get out of sync with the more frequently updated physics.

当然,只有这么多的 CPU 处理能力,但 Unity 可以选择让您有效地减慢物理时间,让帧处理能够赶上来。Maximum Allowed Timestep 设置(位于 Time 窗口中)可限制 Unity 在指定的帧更新期间处理物理系统和 FixedUpdate 调用所花费的时间。如果帧更新花费的时间超过 Maximum Allowed Timestep 设置,则物理引擎将“让时间停止”并让帧处理赶上。一旦帧更新完成,物理引擎将恢复,就好像让时间停止后没有流逝一样。这种情况下的结果是刚体不会像正常情况下那样实时完美移动,而会稍微减慢。然而,物理“时钟”仍将跟踪它们,就像它们正常移动一样。物理时间的减慢通常是不明显的,并且是针对游戏性能的合理折衷。

时间尺度

对于特殊效果,例如“子弹时间”,有时减慢游戏时间的流逝会很有用,能够使动画和脚本响应以较低的速率发生。此外,有时可能希望完全冻结游戏时间,就像游戏暂停时一样。Unity 有一个 Time Scale 属性可以控制游戏时间相对于实时时间的进展速度。如果该标度设置为 1.0,则游戏时间与实时时间匹配。值为 2.0 会使 Unity 中的时间流逝速度加倍(即,动作将加速),而值为 0.5 则会将游戏速度减半。值为零将使时间完全“停止”。请注意,时间标度实际上并不会降低执行速度,而只是更改了通过 Time.deltaTimeTime.fixedDeltaTime 报告给 Update 和 FixedUpdate 函数的时间步长。当游戏时间减慢时,调用 Update 函数的频率可能高于平常,但每帧报告的 deltaTime 步长将会缩短。其他脚本函数不受时间标度的影响,因此您可以在游戏暂停时显示具有正常交互的 GUI。

Time 窗口有一个属性可用于全局设置时间标度,但使用 Time.timeScale 属性从脚本设置该值通常更有用:

//C# 脚本示例
using UnityEngine;
using System.Collections;

public class ExampleScript : MonoBehaviour {
    void Pause() {
        Time.timeScale = 0;
    }
    
    void Resume() {
        Time.timeScale = 1;
    }
}

Capture Framerate

A very special case of time management is where you want to record gameplay as a video. Since the task of saving screen images takes considerable time, the usual frame rate of the game will be drastically reduced if you attempt to do this during normal gameplay. This will result in a video that doesn’t reflect the true performance of the game.

Fortunately, Unity provides a Capture Framerate property that lets you get around this problem. When the property’s value is set to anything other than zero, game time will be slowed and the frame updates will be issued at precise regular intervals. The interval between frames is equal to 1 / Time.captureFramerate, so if the value is set to 5.0 then updates occur every fifth of a second. With the demands on frame rate effectively reduced, you have time in the Update function to save screenshots or take other actions:

//C# script example
using UnityEngine;
using System.Collections;

public class ExampleScript : MonoBehaviour {
    // Capture frames as a screenshot sequence. Images are
    // stored as PNG files in a folder - these can be combined into
    // a movie using image utility software (eg, QuickTime Pro).
    // The folder to contain our screenshots.
    // If the folder exists we will append numbers to create an empty folder.
    string folder = "ScreenshotFolder";
    int frameRate = 25;
        
    void Start () {
        // Set the playback frame rate (real time will not relate to game time after this).
        Time.captureFramerate = frameRate;
        
        // Create the folder
        System.IO.Directory.CreateDirectory(folder);
    }
    
    void Update () {
        // Append filename to folder name (format is '0005 shot.png"')
        string name = string.Format("{0}/{1:D04} shot.png", folder, Time.frameCount );
        
        // Capture the screenshot to the specified file.
        Application.CaptureScreenshot(name);
    }
}

尽管使用这种技术录制的视频通常看起来非常好,但是当速度减慢时,游戏很难运行。您可能需要尝试使用 Time.captureFramerate 的值来确保充足的录制时间,但不会过度复杂化测试播放器的任务。

ScriptableObject
重要的类 - Mathf