一、時間縮放比例
//時間停止
Time.timeScale = 0;
//恢復正常
Time.timeScale = 1;
//2倍速
Time.timeScale = 2;
二、幀間隔時間
幀間隔時間:最近的一幀用了多 長時間(單位 秒)
作用:主要是用來計算位移的 (路程 = 時間 * 速度)
根據需求選擇受scale影響的和不受scale影響的
如果希望游戲暫停時就不動,那就使用deltaTime
如果希望游戲暫停了 也繼續跑,那就使用unscaleDeltaTime
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson5 : MonoBehaviour
{
private void Update()
{
//為了體現兩個的區別,現在把時間縮放比例設定成:時間暫停
Time.timeScale = 0;
//1.受時間縮放比例(scale)影響的
print("幀間隔時間:" + Time.deltaTime);
//2.不受時間縮放比例(scale)影響的
print("不受scale影響的幀間隔時間:" + Time.unscaledDeltaTime);
}
}
運行:

三、游戲開始到現在的時間
作用:主要用來計時(一般用在單機游戲中)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson5 : MonoBehaviour
{
private void Update()
{
//為了體現兩個的區別,現在把時間縮放比例設定成:時間暫停
Time.timeScale = 0;
//1.受scale影響的
print("受scale影響的時間:" + Time.time);
//2.不受scale影響的
print("不受scale影響的時間:" + Time.unscaledTime);
}
}
運行:

四、物理間隔時間 FixedUpdate
注意:一般寫在生命周期函式FixedUpDate中
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson5 : MonoBehaviour
{
private void Update()
{
//為了體現兩個的區別,現在把時間縮放比例設定成:時間暫停
Time.timeScale = 0;
//1.受scale影響的
print("受scale影響的時間:" + Time.fixedDeltaTime);
//2.不受scale影響的
print("不受scale影響的時間:" + Time.fixedUnscaledDeltaTime);
}
}
運行:

五、游戲開始到現在跑了多少幀
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson5 : MonoBehaviour
{
private void Update()
{
print("游戲開始到現在跑的幀數是:" + Time.frameCount);
}
}
運行:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/499995.html
標籤:其他
