参考文档
生命周期

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class ScriptLifeCycle : MonoBehaviour
{
private void Reset()
{
EditorUtility.DisplayDialog($"Reset", $"当前游戏对象:{gameObject.name}\n描述:监听脚本挂载到对象上和编辑器上的Reset按钮点击", "确定");
}
private void Awake()
{
Debug.Log("Awake ---> 运行游戏时立即执行此初始化方法且只执行一次,是一个同步方法");
}
private void Start()
{
Debug.Log("Start ---> 在初始化方法Awake执行完成后的下一帧执行且也只执行一次");
}
private void OnEnable()
{
Debug.Log("OnEnable ---> 当脚本激活时执行");
}
private void OnDisable()
{
Debug.Log("OnDisable ---> 当脚本禁用时执行");
}
#region 脚本更新与协程任务
private void FixedUpdate()
{
Debug.Log("FixedUpdate ---> 固定更新\n" +
"默认情况下系统每0.02s调用一次;\n" +
"具体时间间隔可在 FixedTimestep 中配置,FixedTimestep:Editor->ProjectSetting->Time");
}
private void Update()
{
Debug.Log("Update ---> 监听每一帧执行");
}
private void LateUpdate()
{
Debug.Log("LateUpdate ---> 每一帧执行完成后调用");
}
#endregion
#region OnTriggerXXX 监听触发
private void OnTriggerEnter2D(Collider2D collision)
{
Debug.Log("OnTriggerEnter2D");
}
private void OnTriggerEnter(Collider other)
{
Debug.Log("OnTriggerEnter");
}
private void OnTriggerStay2D(Collider2D collision)
{
Debug.Log("OnTriggerStay2D");
}
private void OnTriggerStay(Collider other)
{
Debug.Log("OnTriggerStay");
}
private void OnTriggerExit2D(Collider2D collision)
{
Debug.Log("OnTriggerExit2D");
}
private void OnTriggerExit(Collider other)
{
Debug.Log("OnTriggerExit");
}
#endregion
#region OnCollisionXXX 监听碰撞
private void OnCollisionEnter2D(Collision2D collision)
{
Debug.Log("OnCollisionEnter2D");
}
private void OnCollisionEnter(Collision collision)
{
Debug.Log("OnCollisionEnter");
}
private void OnCollisionStay2D(Collision2D collision)
{
Debug.Log("OnCollisionStay2D");
}
private void OnCollisionStay(Collision collision)
{
GetComponent<Rigidbody>().velocity = Vector3.up * 10f;
Debug.Log("OnCollisionStay");
}
private void OnCollisionExit2D(Collision2D collision)
{
Debug.Log("OnCollisionExit2D");
}
private void OnCollisionExit(Collision collision)
{
Debug.Log("OnCollisionExit");
}
#endregion
#region OnMouseXXX 监听鼠标事件
private void OnMouseEnter()
{
Debug.Log("OnMouseEnter");
}
private void OnMouseOver()
{
Debug.Log("OnMouseOver");
}
private void OnMouseDown()
{
Debug.Log("OnMouseDown");
}
private void OnMouseDrag()
{
Debug.Log("OnMouseDrag");
}
private void OnMouseUpAsButton()
{
Debug.Log("OnMouseUpAsButton");
}
private void OnMouseUp()
{
Debug.Log("OnMouseUp");
}
private void OnMouseExit()
{
Debug.Log("OnMouseExit");
}
#endregion
private void OnWillRenderObject()
{
Debug.Log("OnWillRenderObject ---> 如果对象可见而不是 UI 元素,则会为每个相机调用 OnWillRenderObject");
}
private void OnPreCull()
{
Debug.Log("OnPreCull ---> Unity 在摄像机剔除场景之前调用的事件函数");
}
private void OnBecameVisible()
{
Debug.Log("OnBecameVisible ---> OnBecameVisible 在渲染器被任何相机可见时调用");
}
private void OnBecameInvisible()
{
Debug.Log("OnBecameInvisible ---> 当渲染器不再被任何相机可见时调用 OnBecameInvisible");
}
private void OnPreRender()
{
Debug.Log("OnPreRender ---> Unity 在相机渲染场景之前调用的事件函数");
}
private void OnRenderObject()
{
Debug.Log("OnRenderObject ---> OnRenderObject 在相机渲染场景后调用");
}
private void OnPostRender()
{
Debug.Log("OnPostRender ---> 在相机渲染场景后 Unity 调用的事件函数");
}
private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
Debug.Log("OnRenderImage ---> Unity 在相机完成渲染后调用的事件函数,允许您修改相机的最终图像");
}
private void OnDrawGizmos()
{
Debug.Log("OnDrawGizmos ---> 如果您想绘制也可以选择且始终绘制的 Gizmos,请实施 OnDrawGizmos");
}
private void OnGUI()
{
Debug.Log("OnGUI ---> 每一帧循环更新来绘制GUI组件");
GetFPS();
GUILayout.Label($"FPS: {_fps}");
CoroutineFun();
}
private void OnApplicationPause(bool pause)
{
Debug.Log("OnApplicationPause ---> 当应用程序暂停时发送到所有 GameObjects");
}
private void OnDestroy()
{
Debug.Log("OnDestroy ---> 游戏对象删除或者脚本被删除时执行销毁方法");
}
private void OnApplicationQuit()
{
Debug.Log("OnApplicationQuit ---> 应用程序推出时执行一次");
}
#region 获取游戏FPS
private float _time;
private int _count;
private int _fps;
void GetFPS()
{
_count++;
_time += Time.deltaTime;
if (_time > 1)
{
_fps = _count;
_time = _count = 0;
}
}
#endregion
#region 协程任务
private Coroutine _coroutine;
void CoroutineFun()
{
if (GUILayout.Button("Start Coroutine"))
{
_coroutine ??= StartCoroutine(CreateSphere(1f));
}
if (GUILayout.Button("Stop Coroutine"))
{
StopCoroutine(_coroutine);
_coroutine = null;
}
}
IEnumerator CreateSphere(float seconds)
{
for (int i = 0; i < 10; i++)
{
GameObject.CreatePrimitive(PrimitiveType.Sphere).transform.position = Vector3.up * i;
yield return new WaitForSeconds(seconds);
}
}
#endregion
}