U3D Time類

Time.time 表示從遊戲開發到如今的時間,會隨着遊戲的暫停而中止計算。spa

 

Time.timeSinceLevelLoad 表示從當前Scene開始到目前爲止的時間,也會隨着暫停操做而中止。code

 

Time.deltaTime 表示從上一幀到當前幀時間,以秒爲單位。orm

 

Time.fixedTime 表示以秒計遊戲開始的時間,固定時間以按期間隔更新(至關於fixedDeltaTime)直到達到time屬性。blog

 

Time.fixedDeltaTime 表示以秒計間隔,在物理和其餘固定幀率進行更新,在Edit->ProjectSettings->Time的Fixed Timestep能夠自行設置。遊戲

 

Time.SmoothDeltaTime 表示一個平穩的deltaTime,根據前 N幀的時間加權平均的值。遊戲開發

 

Time.timeScale 時間縮放,默認值爲1,若設置<1,表示時間減慢,若設置>1,表示時間加快,能夠用來加速和減速遊戲,很是有用。開發

  記住下面兩句話:string

  1.「timeScale不會影響Update和LateUpdate的執行速度」it

  2.「FixedUpdate是根據時間來的,因此timeScale只會影響FixedUpdate的速度」。io

  官方的一句話:

  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);
    }
}

 

相關文章
相關標籤/搜索