U3D Time类

Time.time 表示从游戏开发到现在的时间,会随着游戏的暂停而停止计算。

Time.timeSinceLevelLoad 表示从当前Scene开始到目前为止的时间,也会随着暂停操作而停止。

Time.deltaTime 表示从上一帧到当前帧时间,以秒为单位。

Time.fixedTime 表示以秒计游戏开始的时间,固定时间以定期间隔更新(相当于fixedDeltaTime)直到达到time属性。

Time.fixedDeltaTime 表示以秒计间隔,在物理和其他固定帧率进行更新,在Edit->ProjectSettings->Time的Fixed Timestep可以自行设置。

Time.SmoothDeltaTime 表示一个平稳的deltaTime,根据前 N帧的时间加权平均的值。

Time.timeScale 时间缩放,默认值为1,若设置<1,表示时间减慢,若设置>1,表示时间加快,可以用来加速和减速游戏,非常有用。

  记住下面两句话:

  1.“timeScale不会影响Update和LateUpdate的执行速度”

  2.“FixedUpdate是根据时间来的,所以timeScale只会影响FixedUpdate的速度”。

  官方的一句话:

  Except for realtimeSinceStartup, timeScale affects all the time and delta time measuring variables of the Time class.

  除了realtimesincestartup,timeScale影响Time类中所有时间和时间增量测量相关的变量。

Time.frameCount 总帧数

Time.realtimeSinceStartup 表示自游戏开始后的总时间,即使暂停也会不断的增加。

Time.captureFramerate 表示设置每秒的帧率,然后不考虑真实时间。

Time.unscaledDeltaTime 不考虑timescale时候与deltaTime相同,若timescale被设置,则无效。

Time.unscaledTime 不考虑timescale时候与time相同,若timescale被设置,则无效。

下面示例怎么怎么创建一个实时现实的FPS

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Show_FPS : MonoBehaviour
{
    public float updateInterval = 0.5f;
    private float accum = 0;
    private int frame = 0;
    private float timeLeft = 0;
    private string stringFPS = string.Empty;

    void Start()
    {
        timeLeft = updateInterval;
    }

    void Update()
    {
        timeLeft -= Time.deltaTime;
        accum += Time.timeScale / Time.deltaTime;
        frame++;
        if(timeLeft <= 0)
        {
            float fps = accum / frame;
            string format = string.Format("{0:F2} FPS", fps);
            stringFPS = format;
            timeLeft = updateInterval;
            accum = 0.0F;
            frame = 0;
        }
    }

    private void OnGUI()
    {
        GUIStyle gUIStyle = GUIStyle.none;
        gUIStyle.fontSize = 30;
        gUIStyle.normal.textColor = Color.blue;
        gUIStyle.alignment = TextAnchor.UpperLeft;
        Rect rt = new Rect(40, 0, 100, 100);
        GUI.Label(rt, stringFPS, gUIStyle);
    }
}

原文地址:https://www.cnblogs.com/forever-Ys/p/10520544.html