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

 

相关文章
相关标签/搜索