#unity/日常积累

Time.realtimeSinceStartup

public static float realtimeSinceStartup ;

描述

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

在几乎所有情况下,您都可以并且应该使用 Time.time

realtimeSinceStartup 返回自启动以来的时间,不受 Time.timeScale 的影响。 在播放器暂停期间(在后台),realtimeSinceStartup 仍会不断增加。 当您需要通过将 Time.timeScale 设置为 0 来暂停游戏,但仍希望能够以某种方式测量时间时, realtimeSinceStartup 非常有用。

注意,realtimeSinceStartup 返回系统计时器报告的时间。 根据平台和硬件的不同,它可能会在数个连续帧中报告相同的时间。 如果您需要用某个值除以时间差,一定要考虑到这一点 (时间差可能会变为 0!)。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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.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;
        }
    }
}