Editor 编辑器
Reset()
当脚本绑定到物体上时,则调用这个函数
private void Reset() {
Debug.Log("Script added!");
}
可以利用这个函数自动找到一些组件,在函数外创建一个变量
public Transform player;
在函数内找到并赋值
player = GameObject.FindGameObjectWithTag("player").transform;
Initialization 初始化
Awake()
最早的执行函数,可以用来初始化一些东西,执行顺序Awake() →OnEnable() → start()
private void Awake() {
Debug.Log("Awake");
}
OnEnable()
当前物体被启用的时候执行的周期函数
private void OnEnable() {
Debug.Log("OnEnable");
}
Start()
在游戏第一帧执行
private void Start() {
Debug.Log("Start");
}
Physics 物理
FixedUpdate()
固定更新频率,可以是在项目设置中更改时间间隔 Project Settings - Time - Fixed Timestep,默认是0.02,一秒50次
private void FixedUpdate() {
Debug.Log("FixedUpdate");
}
OnTriggerXxx()
OnTriggerEnter()当物体穿过时执行(在 Collider 中 勾选 Is Trigger)
private void OnTriggerEnter(Collider other) {
Debug.Log("OnTriggerEnter");
}
OnCollisionXxx()
OnCollisionEnter()当物体碰撞时执行
private void OnCollisionEnter(Collision collision) {
Debug.Log("OnCollisionEnter");
}
Input 输入
OnMouseXxx()
OnMouseOver() 当鼠标移动至物品上则执行(只针对3D对象,Main Camera 需要是3D视角 Projection - Perspective)
private void OnMouseOver() {
Debug.Log("OnMouseOver");
}
Game Logic 游戏逻辑
Update()
每帧执行一次
private void Update() {
Debug.Log("Update");
}
( Update() 和 LateUpdate() 中间是一些 coroutines 和一些 animation updates)
LateUpdate()
在Update()之后执行,避免和其他脚本的一些逻辑冲突
private void LateUpdate() {
Debug.Log("LateUpdate");
}
Rendering 渲染
OnBecameVisible()
当对象可见时则执行
private void OnBecameVisible() {
Debug.Log("OnBecameVisible");
}
OnBecameInvisible()
当对象不可见时则执行
private void OnBecameInvisible() {
Debug.Log("OnBecameInvisible");
}
OnRenderImage()
private void OnRenderImage(RenderTexture src, RenderTexture dest) {
Debug.Log("OnRenderImage");
}
Gizmo 图标
OnDrawGizmos()
可以绘制一些图标,比如是画一个圈,且游戏中不可见
private void OnDrawGizmos() {
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position, 0.5f);
}
GUI 图形界面
OnGUI()
可以直接用代码写GUI
private void OnGUI() {
GUI.Label(new Rect(100, 100, 600, 300), "Hello!");
}
Pause 暂停
OnApplicationPause()
离开暂停
private void OnApplicationPause(bool pause) {
Debug.Log("OnApplicationPause");
}
Exiting 退出
OnDisable()
当前物体被关闭的时候执行的周期函数
private void OnDisable() {
Debug.Log("OnDisable");
}
OnApplicationQuit()
当程序结束运行时执行
private void OnApplicationQuit() {
Debug.Log("OnApplicationQuit");
}
OnDestroy()
销毁时执行,会先调用一次OnDisable()
private void OnDestroy() {
Debug.Log("OnDestroy");
}







Comments | NOTHING