Version: 2021.3

Time.realtimeSinceStartup

切换到手册
public static float realtimeSinceStartup ;

描述

游戏开始以来的实际时间(只读)。

This is the time in seconds since the start of the application, and is not constant if called multiple times in a frame. Time.timeScale does not affect this property.

In almost all cases you should use Time.time or unscaledTime instead.

Using realtimeSinceStartup is useful if you want to set Time.timeScale to zero to pause your application, but still want to be able to measure time somehow. In Editor scripts, you can also use realtimeSinceStartup to measure time while the Editor is paused.

realtimeSinceStartup returns time as reported by the system timer. Depending on the platform and the hardware, it might report the same time even in several consecutive frames. If you're dividing something by time difference, take this into account (for example, time difference might become zero).

using UnityEngine;
using System.Collections;

// An FPS counter. // It calculates frames/second over each updateInterval, // so the display does not keep changing wildly. public class ExampleClass : MonoBehaviour { public float updateInterval = 0.5F; private double lastInterval; private int frames; private float fps; void Start() { lastInterval = Time.realtimeSinceStartup; frames = 0; }

void OnGUI() { GUILayout.Label("" + fps.ToString("f2")); }

void Update() { ++frames; float timeNow = Time.realtimeSinceStartup; if (timeNow > lastInterval + updateInterval) { fps = (float)(frames / (timeNow - lastInterval)); frames = 0; lastInterval = timeNow; } } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961