Time類是Unity中獲取時(shí)間信息的接口類,只有靜態(tài)屬性。本博客介紹Time類的一些靜態(tài)屬性。
一、Time類靜態(tài)屬性
在Time類中,涉及的靜態(tài)屬性有realtimeSinceStartup、smoothDeltaTime和time屬性,在介紹time屬性時(shí)涉及了Time類的多個(gè)其他屬性的使用。
1、reltimeSinceStartup屬性:程序運(yùn)行實(shí)時(shí)時(shí)間
(1)基本語(yǔ)法
public static float realtimeScienceStartup { get; }
(2)功能說(shuō)明
此屬性用于返回從游戲啟動(dòng)到現(xiàn)在已運(yùn)行的實(shí)時(shí)時(shí)間(只讀),以秒為單位。此屬性通??捎?code>Time.time代替使用,但realtimeSinceStartup的返回值不受timeScale屬性變化的影響。
(3)代碼實(shí)現(xiàn)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RealtimeSinceStartup_test : MonoBehaviour
{
public Rigidbody rg;
void Start()
{
Debug.Log("Time.timeScale的默認(rèn)時(shí)間: " + Time.timeScale);
//觀察剛體在timeScale變化前后的移動(dòng)速度
rg.velocity = Vector3.forward * 2.0f;
Time.timeScale = 0.5f;
}
void Update()
{
Debug.Log("Time.timeScale的當(dāng)前值: " + Time.timeScale);
Debug.Log("Time.time:" + Time.time);
Debug.Log("Time.realtimeSinceStartup:" + Time.realtimeSinceStartup);
}
void OnGUI()
{
if (GUI.Button(new Rect(10.0f, 10.0f,200.0f, 45.0f), "Time.timeScale = 0.5f"))
{
Time.timeScale = 0.5f;
}
if (GUI.Button(new Rect(10.0f,60.0f,200.0f,45.0f),"Time.timeScale = 1.0f"))
{
Time.timeScale = 1.0f;
}
}
}
在這段代碼中,首先聲明了一個(gè)Rigidbody變量rg,并在Start方法中給剛體rg一個(gè)出事速度,然后再方法OnGUI中定義了兩個(gè)Button用來(lái)控制Time.timeScale
的值,最后再Update方法中分別打印出了Time.timeScale
、Time.timeScale
、Time.time
和Time.realtimeSinceStartup
的值
2、reltimeSinceStartup屬性:程序運(yùn)行實(shí)時(shí)時(shí)間
(1)基本語(yǔ)法
public static float smoothDeltaTime { get; }
(2)基本語(yǔ)法
此屬性用于返回Time.deltaTime的平滑輸出值(只讀)。Time.smoothDeltaTime
比Time.deltaTime
的波幅震蕩更平滑,通常Time.smoothDeltaTime
的累加和比Time.deltaTime
的累加稍微大些。Time.smoothDeltaTime
主要用于在于在非FixedUpdate方法中需要平滑過(guò)渡的計(jì)算文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-467584.html
(3)代碼實(shí)現(xiàn)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SmoothDeltaTime_test : MonoBehaviour
{
float a = 0, b= 0;
// Update is called once per frame
void Update()
{
float t1, t2;
t1 = Time.deltaTime;
t2 = Time.smoothDeltaTime;
Debug.Log("Time.deltaTime:" + t1);
Debug.Log("Time.deltaTime:" + t2);
a += t1;
b += t2;
Debug.Log("Time.deltaTime的累加和:" + a + "smoothDeltaTime的累加和:" + b);
}
}
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-467584.html
到了這里,關(guān)于Unity API詳解——Time類的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!