Version: 6000.3
语言: 中文
游戏内时间和实时
模拟挂钩以进行测试

捕获帧速率

时间管理的一个特殊情况是您想要将游戏录制为视频。由于保存屏幕图像的任务需要相当长的时间,因此游戏的正常帧速率会降低,并且视频无法反映游戏的真实性能。

要改善视频的外观,请使用Time.captureFramerate财产。对于未录制的游戏,该属性的默认值为 0。将属性的值设置为零以外的任何值时,游戏时间会减慢,并且会以精确的定期间隔发出帧更新,以便进行录制。帧之间的间隔等于1 / Time.captureFramerate,因此,如果将值设置为 5.0,则每五分之一秒进行一次更新。

随着对帧速率的要求有效降低,您有时间在UpdateLateUpdate保存屏幕截图或执行其他作的功能。以下代码示例演示了这一点:

//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);
    }

    // Use LateUpdate to capture the screen after all other updates have been processed.
    void LateUpdate()
    {
        // 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.
        ScreenCapture.CaptureScreenshot(name);
    }
}

使用这种技术可以改善视频,但会使游戏更难玩。尝试不同的值Time.captureFramerate找到一个好的平衡点。

其他资源

游戏内时间和实时
模拟挂钩以进行测试