一起看看MonoBehavior内部事件执行顺序

原文链接: http://blog.shuiguzi.com/2014/11/10/MonoBehavior%20event%20functions/


写在最前面

或许对于大部分Unity3D程序员来说,第一次接触脚本编写的时候都是从MonoBehavior开始的。MonoBehavior是Uniy3d脚本编写核心的类之一, 它预先定义好了很多事件,并且这些事件按照预先定义好顺序执行。了解MonoBehavior提供的这些事件的执行顺序,是我们进一步提高脚步编程和了解Unity3D内部逻辑的必要一步。我们先从MonoBehavior提供的事件说起。

MonoBehavior都提供了什么事件

MonoBehavior提供的事件从编辑器到游戏结束都有涉及。下面列举一些常用的,更详列表可以参照 这个页面

与编辑相关
名称 注释
Reset Reset to default values.
Update相关
名称 注释
FixedUpdate This function is called every fixed framerate frame, if the MonoBehaviour is enabled.
Update Update is called every frame, if the MonoBehaviour is enabled.
LateUpdate LateUpdate is called every frame, if the Behaviour is enabled.
生命周期相关
名称 注释
Awake Awake is called when the script instance is being loaded.
OnEnable This function is called when the object becomes enabled and active.
OnDisable This function is called when the behaviour becomes disabled () or inactive.
OnDestroy This function is called when the MonoBehaviour will be destroyed.
OnApplicationFocus Sent to all game objects when the player gets or loses focus.
OnApplicationPause Sent to all game objects when the player pauses.
OnApplicationQuit Sent to all game objects before the application is quit.
Start Start is called on the frame when a script is enabled just before any of the Update methods is called the first time.
物理系统相关
名称 注释
OnCollisionEnter OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider.
OnCollisionStay OnCollisionStay is called once per frame for every collider/rigidbody that is touching rigidbody/collider.
OnCollisionExit OnCollisionExit is called when this collider/rigidbody has stopped touching another rigidbody/collider.
OnCollisionEnter2D Sent when an incoming collider makes contact with this object’s collider (2D physics only).
OnCollisionStay2D Sent each frame where a collider on another object is touching this object’s collider (2D physics only).
OnCollisionExit2D Sent when a collider on another object stops touching this object’s collider (2D physics only).
OnTriggerEnter OnTriggerEnter is called when the Collider other enters the trigger.
OnTriggerStay OnTriggerStay is called once per frame for every Collider other that is touching the trigger.
OnTriggerExit OnTriggerExit is called when the Collider other has stopped touching the trigger.
OnTriggerEnter2D Sent when another object enters a trigger collider attached to this object (2D physics only).
OnTriggerStay2D Sent each frame where another object is within a trigger collider attached to this object (2D physics only).
OnTriggerExit2D Sent when another object leaves a trigger collider attached to this object (2D physics only).
输入系统相关
名称 注释
OnMouseDown OnMouseDown is called when the user has pressed the mouse button while over the GUIElement or Collider.
OnMouseOver OnMouseOver is called every frame while the mouse is over the GUIElement or Collider.
OnMouseUp OnMouseUp is called when the user has released the mouse button.
OnMouseDrag OnMouseDrag is called when the user has clicked on a GUIElement or Collider and is still holding down the mouse.
OnMouseEnter OnMouseEnter is called when the mouse entered the GUIElement or Collider.
OnMouseExit OnMouseExit is called when the mouse is not any longer over the GUIElement or Collider.
OnMouseUpAsButton OnMouseUpAsButton is only called when the mouse is released over the same GUIElement or Collider as it was pressed.
渲染相关
名称 注释
OnPreCull OnPreCull is called before a camera culls the scene.
OnBecameVisible OnBecameVisible is called when the renderer became visible by any camera.
OnBecameInvisible OnBecameInvisible is called when the renderer is no longer visible by any camera.
OnWillRenderObject OnWillRenderObject is called once for each camera if the object is visible.
OnPreRender OnPreRender is called before a camera starts rendering the scene.
OnRenderObject OnRenderObject is called after camera has rendered the scene.
OnPostRender OnPostRender is called after a camera finished rendering the scene.
OnRenderImage OnRenderImage is called after all rendering is complete to render image.
OnGUI OnGUI is called for rendering and handling GUI events.
OnDrawGizmos Implement OnDrawGizmos if you want to draw gizmos that are also pickable and always drawn.

用图来表示MonoBehavior事件执行顺序

monobehavior

总结:

  • 首次加载场景时执行Awake()
  • Start()只在第一帧才执行, Start()Awake()之后执行
  • Update的执行顺序是: FixedUpdate() -> Update() -> LateUpdate()
  • 以每一帧的Update()事件作分界线:
    • Update()之前物理系统输入系统相关事件先执行,如OnTriggerXXXOnMouseXXX事件。此处XXX是占位符,如OnTriggerXXX可以代表OnTriggerEnter或者OnTriggerExit
    • Update()之后场景渲染协程,如OnRenderImage()yield WWW语句
  • 协程中,除了WaitForFixedUpdate是在FixedUpdate之后,Update之前执行,其他的都是在Update之后,场景渲染前执行
  • GUI事件OnGUI在场景渲染完之后执行
  • 当对象被销毁时执行OnDestory()事件
  • 当游戏退出时执行OnApplicationQuit()
  • OnEnable()OnDisable()
    • OnEnable()只有在Object是Active的状态下才能用,一般是Object被初始化或者Object从disable到active过程中被调用
    • OnDisable()只有到Object从active到disable状态才被调用

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值