Version: 2020.2
LanguageEnglish
  • C#

Time.realtimeSinceStartupAsDouble

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public static double realtimeSinceStartupAsDouble;

Description

The real time in seconds since the game started (Read Only). Double precision version of realtimeSinceStartup. This offers more precision than a float or single value, particularly over extended periods of real-world time. In almost all cases, choose the realtimeSinceStartupAsDouble equivalent over realtimeSinceStartup.

In almost all cases you can and should use Time.timeAsDouble instead.

realtimeSinceStartupAsDouble returns the time since startup, not affected by Time.timeScale. realtimeSinceStartupAsDouble also keeps increasing while the player is paused (in the background). Using realtimeSinceStartupAsDouble is useful when you want to pause the game by setting Time.timeScale to zero, but still want to be able to measure time somehow.

Note that realtimeSinceStartupAsDouble returns time as reported by system timer. Depending on the platform and the hardware, it may report the same time even in several consecutive frames. If you're dividing something by time difference, take this into account (time difference may 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 = 0; private float fps; void Start() { lastInterval = Time.realtimeSinceStartupAsDouble; frames = 0; }

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

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