一、事件函數
Help->Unity Manual/Scripting Reference(查看Unity文檔和API手冊)
Manual-> Scripting-> Scripting Overview->Execution Order of Event Functions(事件函數執行順序)
(1)Rest:Editor模式下觸發
(2)Awake:喚醒事件,在腳本實例被創建時調用,只執行一次
(3)OnEnable:啓用事件,在每次激活腳本時使用
(4)Start:開始事件,用於遊戲對象或遊戲場景的初始化,在場景被加載時被調用,值執行一次
(5)FixedUpdate:固定更新事件
(6)Update:更新事件,每幀執行一次
(7)LateUpdate:每幀執行一次在Update事件之後執行
(8)OnTriggerXXX:觸發觸發器
(9)OnCollisionXXX:觸發碰撞器
(10)yield WaitForFixedUpdate:在FixedUpdate調用yield進行等待
(11)OnMouseXXX:輸入事件
(12)OnDrawGizmos:繪製Gizmos
(13)OnGUI:用來繪製用戶交互界面,每一幀會調用多次
(14)OnApplicationPause:在點擊暫停按鈕時調用
(15)OnApplicationQuit:退出遊戲,遊戲中的所有物體會被銷燬
(16)OnDestroy: 當前腳本被銷燬時調用一次
測試代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class API01EventFunction : MonoBehaviour {
void Awake()
{
Debug.Log("Awake");
}
void OnEnable()
{
Debug.Log("Enable");
}
void Start () {
Debug.Log("Start");
}
void FixedUpdate()
{
Debug.Log("FixedUpdate");
}
void Update()
{
Debug.Log("Update");
}
void LateUpdate()
{
Debug.Log("LateUpdate");
}
void OnApplicationPause()
{
Debug.Log("OnApplicationPause");
}
void OnDisable()
{
Debug.Log("Disable");
}
void OnApplicatoinQuit()
{
Debug.Log("OnApplicationQuit");
}
void Reset()
{
Debug.Log("Reset");
}
void OnDestroy()
{
Debug.Log("OnDestroy");
}
}
二、Time類(靜態變量)
(1)captureFramerate:表示設置每秒的幀率,可以用於屏幕截圖。
(2)deltaTime:表示從上一幀到當前幀時間(秒)。
(3)fixedDeltaTime:最近FixedUpdate已開始的時間。該時間從遊戲開始計算,在Edit->ProjectSettings->Time的Fixed Timestep可以進行設置。
(4)fixedTime:表示以秒計遊戲開始的時間,固定時間以定期間隔更新(相當於fixedDeltaTime)直到達到time屬性。
(5)timeScale:可以用來製作慢動作特效。(默認爲1,<1,減慢, >1,加快)
(6)time:遊戲從開始到現在經歷的時間(秒)。
(7)frameCount:總幀數(從遊戲開始到現在一共運行了多少幀)。
(8)realtimeSinceStartup:遊戲開始後的總時間,暫停也會繼續增加。(性能測試 )
測試代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class API02Time : MonoBehaviour {
public Transform cube;
public int runCount = 10000;
void Start()
{
float time00 = Time.realtimeSinceStartup;
for (int i = 0; i < runCount; i++)
{
Method0();
}
float time0 = Time.realtimeSinceStartup;
Debug.Log(time0 - time00);
float time1 = Time.realtimeSinceStartup;
for (int i = 0; i < runCount; i++)
{
Method1();
}
float time2 = Time.realtimeSinceStartup;
Debug.Log(time2 - time1);
float time3 = Time.realtimeSinceStartup;
for (int i = 0; i < runCount; i++)
{
Method2();
}
float time4 = Time.realtimeSinceStartup;
Debug.Log(time4 - time3);
}
void Update () {
//Debug.Log("Time.deltaTime:" + Time.deltaTime);
//Debug.Log("Time.fixedDeltaTime:" + Time.fixedDeltaTime);
//Debug.Log("Time.fixedTime:" + Time.fixedTime);
//Debug.Log("Time.frameCount:" + Time.frameCount);
//Debug.Log("Time.realtimeSinceStartup:" + Time.realtimeSinceStartup);
//Debug.Log("Time.smoothDeltaTime:" + Time.smoothDeltaTime);
//Debug.Log("Time.time:" + Time.time);
//Debug.Log("Time.timeScale:" + Time.timeScale);
//Debug.Log("Time.timeSinceLevelLoad:" + Time.timeSinceLevelLoad);
//Debug.Log("Time.unscaledTime:" + Time.unscaledTime);
//cube.Translate(Vector3.forward *Time.deltaTime*3 );
//Time.timeScale = 3f;
}
void Method0() { }
void Method1()
{
int i = 5;
i += 5;
i += 5;
}
void Method2()
{
int i = 5;
i *= 5;
i *= 5;
}
}