关于时间管理的一个特殊案例是您希望将游戏过程录制为视频。由于保存屏幕图像的任务需要相当长的时间,因此游戏的正常帧率会降低,并且视频无法反映游戏的真实性能。
要改善视频的外观,请使用捕获帧率 (Capture Framerate) 属性。对于未录制的游戏过程,此属性的默认值为 0。将此属性的值设置为零以外的任何值时,游戏时间将减慢,而帧更新将以精确的定期时间间隔发出,以进行录制。帧之间的时间间隔等于 1 / Time.captureFramerate,因此如果该值设置为 5.0,则每五分之一秒更新一次。随着对帧率的要求有效降低,在更新 (Update) 函数中便有了时间来保存截屏或采取其他操作:
//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 的不同值以找到良好的平衡。