Unity API常用方法和类学习笔记1
------主要构架(Unity-Engine、GameObject、Component)
事件
一、事件执行顺序
二、测试代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class API01EventFunction : MonoBehaviour
{
void Reset(){
Debug.Log("Reset");//当脚本被附加到对象上时或被重置时
}
void Awake(){
Debug.Log("Awake");
}
void OnEnable() {
Debug.Log("OnEnable");//当脚本所在对象被重新可见时
}
void Start(){
Debug.Log("Start");
}
void FixedUpdate(){
Debug.Log("FixedUpdate");//每秒有固定的调用次数,所以运行时不受电脑性能的差别影响——适合用于控制物体的运动
}
void Update(){
Debug.Log("Update");//不固定,调用次数随电脑运行-画面加载快慢影响
}
void LateUpdate() {
Debug.Log("LateUpdate");//跟随Update(一致)
}
void OnApplicationPause() {
Debug.Log("OnApplicationPause");
}
void OnDisable() {
Debug.Log("OnDisable");//当物体不可见时
}
void OnApplicationQuit() {
Debug.Log("OnApplicationQuit");//退出时
}
void OnDestoy() {
Debug.Log("OnDestroy");
}
}
类Time
一、静态变量
void Update () {
Debug.Log("Time.deltaTime" + Time.deltaTime);//每一帧的时间间隔约等于1/60秒
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);//与Time.fixedTime差不多
Debug.Log("Time.timeScale" + Time.timeScale);//设置时间比例
Debug.Log("Time.timeSinceLevelLoad" + Time.timeSinceLevelLoad);//以场景为基准,加载新场景时重新开始计时
Debug.Log("Time.unscaledTime" + Time.unscaledTime);
}
二、利用Time.realtimeSinceStartup测试函数是否太耗费性能
void Start () {
//利用Time.realtimeSinceStartup测试函数是否太耗费性能
float time1 = Time.realtimeSinceStartup;
for (int i = 0; i < runCount; i++)
{
Method1();
}
float time2