之前对Time的各个属性认识还有点模糊,抽了点时间把Time的API又完整的拔了一遍,添加了一点我个人的理解,在这里做个记录,如有问题欢迎各位指摘和探讨。
以下直接放代码了,所有注释都在代码里,如果想直接下载文章末尾有链接。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.PlayerLoop;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
/// <summary>
/// Time API
/// Date:2018/10/13
/// Author:大虾小二
/// </summary>
public class TimeStudy : MonoBehaviour {
#region time
/*
/// ********************************************************************
/// Time.time
/// 自动增量,从程序开始的事件-一个不断增加的量,一直在增加,暂停停止计算
/// ********************************************************************
void Update()
{
if (Input.GetKey(KeyCode.A))
{
Debug.Log("time:"+Time.time);
}
}
*/
#endregion
#region Time.captureFramerate
/*
/// ********************************************************************
/// 设定每秒固定帧率
/// 以下是官方案例,设置每秒25帧,每帧截一次图
/// ********************************************************************
public string folder = "ScreenshotFolder";
public int frameRate = 25;
void Start()
{
// Set the playback framerate (real time will not relate to game time after this).
Time.captureFramerate = frameRate;
// Create the folder
System.IO.Directory.CreateDirectory(folder);
}
void Update()
{
print(Time.time);
// Append filename to folder name (format is '0005 shot.png"')
string name = string.Format("{0}/{1:D04} shot.png", folder, Time.frameCount);
// Capture the screenshot to the specified file.
ScreenCapture.CaptureScreenshot(name); //--------每秒截图25张
}
*/
#endregion
#region Time.deltaTime
/*
/// ********************************************************************
/// Time.deltaTime
/// 完成上一帧所花的时间只读,每一帧时间是不固定的
/// 常用来控制移动和动画,通过✖️Time.deltaTime来进行一个时间的补偿,控制平滑移动
/// ********************************************************************
float time = 0;
void Update()
{
Debug.Log("完成上一帧所花的时间:"+Time.deltaTime);
time+=Time.deltaTime;
Debug.Log("完成所有帧所花的时间"+time);
transform.Translate(0, 0, Time.deltaTime * 10);//控制移动
}
*/
#endregion
#region Time.fixedDeltaTime
/*
/// ********************************************************************
/// Time.fixedDeltaTime
/// 表示以秒计间隔,在物理和其他固定帧率进行更新,在Edit->ProjectSettings->Time的Fixed Timestep可以自行设置,默认设置是0.02f。
/// Update每帧调用一次,FixedUpdate是每Time.fixedDeltaTime调用一次
/// ********************************************************************
void Start()
{
Time.fixedDeltaTime = 0.1f;
}
void FixedUpdate()
{
print("刷新速率");//----0.1f每秒刷新10次,0.001f每秒刷新100次
}
*/
#endregion
#region Time.fixedTime
/*
/// ********************************************************************
/// Time.fixedTime
/// 表示FixedUpdate内已经用过的时间
/// ********************************************************************
void Update()
{
if (Input.GetKey(KeyCode.A))
{
Debug.Log("在Update:" + Time.fixedTime);
}
}
*/
#endregion
#region Time.fixedUnscaledDeltaTime
/*
/// ********************************************************************
/// Time.fixedUnscaledDeltaTime
/// 和fixedDeltaTime对应,不受暂停的影响
/// ********************************************************************
private float timeNum = 0;
private void Update()
{
if (Input.GetKey(KeyCode.S))
{
Time.timeScale = 1;
}
}
void FixedUpdate()
{
if (Input.GetKey(KeyCode.A))
{
Time.timeScale = 0;
}
timeNum += Time.fixedUnscaledDeltaTime;
Debug.Log("fixedUnscaledDeltaTime:"+timeNum+"....."+"fixedTime:"+Time.fixedTime);
}
*/
#endregion
#region Time.fixedUnscaledTime
/*
/// ********************************************************************
/// Time.fixedUnscaledTime
/// 和fixedTime对应,不受暂停的影响
/// ********************************************************************
void Update()
{
if (Input.GetKey(KeyCode.A))
{
Time.timeScale = 0;
}
if (Input.GetKey(KeyCode.S))
{
Time.timeScale = 1;
}
Debug.Log("fixedUnscaledTime:"+Time.fixedUnscaledTime);
}
*/
#endregion
#region Time.frameCount
// ********************************************************************
// Time.frameCount 已通过帧的总数
// 你可以通过该属性在某帧执行一些特定的方法
// ********************************************************************
#endregion
#region Time.inFixedTimeStep
/*
/// ********************************************************************
/// Time.inFixedTimeStep
/// 如果在固定时间步骤回调中调用,则返回true
/// ********************************************************************
void Update()
{
print("在Update内:"+Time.inFixedTimeStep); //返回false
}
private void FixedUpdate()
{
print("在FixedUpdate内:"+Time.inFixedTimeStep); //返回true
}
*/
#endregion
#region Time.maximumDeltaTime
/*
/// ********************************************************************
/// Time.maximumDeltaTime 一帧所花费的最大时间
/// 在Edit->ProjectSettings->Time的Maximum Particle Timestep可以自行设置。
/// 如果帧花费的时间超过了指定的值,那么将执行更少的物理和其他固定帧率更新
/// ********************************************************************
void Update()
{
Debug.Log(Time.maximumDeltaTime);
}
*/
#endregion
#region Time.maximumParticleDeltaTime
// ********************************************************************
// Time.maximumParticleDeltaTime 一个帧在粒子更新上所能花费的最大时间
// 在Edit->ProjectSettings->Time的Maximum Allowed Timestep可以自行设置。
// 如果帧花费的时间超过了指定的值,更新会被分解
// ********************************************************************
#endregion
#region Time.realtimeSinceStartup
/*
/// ********************************************************************
/// Time.realtimeSinceStartup
/// 表示自游戏开始后的总时间,即使暂停也会不断的增加。
/// 官方案例,FPS的显示
/// ********************************************************************
public float updateInterval = 0.5F;
private double lastInterval;
private int frames = 0;
private float fps;
void Start()
{
lastInterval = Time.realtimeSinceStartup;
frames = 0;
}
void OnGUI()
{
GUILayout.Label("" + fps.ToString("f2"));
}
void Update()
{
++frames;
float timeNow = Time.realtimeSinceStartup;
if (timeNow > lastInterval + updateInterval)
{
fps = (float)(frames / (timeNow - lastInterval));
frames = 0;
lastInterval = timeNow;
}
}
*/
#endregion
#region Time.smoothDeltaTime
/*
/// ********************************************************************
/// Time.SmoothDeltaTime
/// 表示一个平稳的deltaTime,根据前 N帧的时间加权平均的值
/// ********************************************************************
void Update()
{
Debug.Log(Time.smoothDeltaTime);
}
*/
#endregion
#region Time.timeScale
/*
/// ********************************************************************
/// Time.timeScale
/// 表示时间的缓慢尺度
/// ********************************************************************
void Update()
{
if (Input.GetKey(KeyCode.A))
{
Time.timeScale = 0; //--时间暂停
}
if (Input.GetKey(KeyCode.S))
{
Time.timeScale = 1; //--时间恢复正常速度
}
Debug.Log("暂停了就不输出了");
}
*/
#endregion
#region Time.timeSinceLevelLoad
// ********************************************************************
// Time.timeSinceLevelLoad
// 表示从第一个场景加载开始的总时间,也会随着暂停操作而停止。
// ********************************************************************
#endregion
#region Time.unscaledTime
// ********************************************************************
// Time.unscaledTime
// 不考虑timescale时候与time相同,若timescale被设置,则无效。
// ********************************************************************
#endregion
#region Time.unscaledDeltaTime
// ********************************************************************
// Time.unscaledDeltaTime
// 不考虑timescale时候与deltaTime相同,若timescale被设置,则无效。
// ********************************************************************
#endregion
}
附上源码,有需要的可以下载
https://download.csdn.net/download/mr_sun88/10718505
作者:大虾小二
CSDN:Mr_Sun88
转载请注明出处!