Execution order of Unity event functions

Execution order of event functions


Script life cycle flowchart

insert image description here

When the scene loads

这些函数在场景开始时被调用(场景中的每个对象一次)
  • Awake: This function is always called before any Start function and after the prefab is instantiated (if the GameObject is inactive during startup, Awake will not be called until it is activated.)

  • OnEnable: (Called only if the object is active): This function is called immediately after the object is enabled. This happens when a MonoBehaviour instance is created, such as when a level is loaded or when a GameObject's script component is instantiated.


Editor

  • Reset: Reset is called to initialize the script's properties when the script is first attached to the object and when the Reset command is used.

  • OnValidate: OnValidate is called whenever a script's properties are set, including when deserializing objects, which can happen at different times, such as when opening a scene in the editor and after a domain reload.


Before the first frame update

  • Start: Start is called before the first frame update, only if script instance is enabled.

Update Order

  • FixedUpdate: FixedUpdate is usually called more frequently than Update. If the frame rate is low, it may be called multiple times per frame, and if the frame rate is high, it may not be called between frames at all. All physics calculations and updates happen immediately after FixedUpdate. You don't need to multiply your value by Time.deltaTime when applying movement calculations in FixedUpdate. This is because FixedUpdate is called on a reliable timer, independent of frame rate.

  • Update: Called once per frame.

  • LateUpdate: After Update, LateUpdate is called every frame. Any calculations performed in Update will be complete when LateUpdate starts. A common use of LateUpdate is to follow a third-person camera. If you let your character move and turn in Update, you can do all camera movement and rotation calculations in LateUpdate. This will ensure that the character has fully moved before the camera tracks its position.


Animation update loop

  • OnStateMachineEnter: During the state machine update step, this callback is called on the first update frame when the controller's state machine makes a transition through the Entry state. It is not called to transition to a StateMachine substate.

  • OnStateMachineExit: During a state machine update step, this callback is called on the last update frame when the controller's state machine makes a transition that flows through an exit state. It is not called to transition to a StateMachine substate.

  • Fire Animation Events: Calls all animation events from all clips sampled between the last update time and the current update time.

  • StateMachineBehaviour (OnStateEnter/OnStateUpdate/OnStateExit): A layer can have up to 3 active states: current state, interrupted state, and next state. This function is called for each active state using a StateMachineBehaviour component that defines an OnStateEnter, OnStateUpdate, or OnStateExit callback.

  • OnAnimatorMove: Every update frame, called once for each Animator component, modifying Root Motion

  • StateMachineBehaviour(OnStateMove): Called on each active state with the StateMachineBehaviour that defines this callback.

  • OnAnimatorIK: Set animation IK. Called once for each Animator Controller layer that has an IK channel enabled.

  • StateMachineBehaviour(OnStateIK): The StateMachineBehaviour component that defines this callback on layers with IK channels enabled is invoked on each active state.

  • WriteProperties: Writes all other animation properties to the scene from the main thread.


Rendering

  • OnPreCull: Called before the camera culls the scene. Culling determines which objects are visible to the camera. OnPreCull is called before culling occurs.

  • OnBecameVisible/OnBecameInvisible: Called when the object is visible/invisible to any camera.

  • OnWillRenderObject: Called once for each camera if the object is visible.

  • OnPreRender: Called before the camera starts rendering the scene.

  • OnRenderObject: Called after all normal scene rendering is complete. At this point you can use the GL classes or Graphics.DrawMeshNow to draw custom geometry.

  • OnPostRender: Called after the camera has finished rendering the scene.

  • OnRenderImage: Called after the scene is rendered, allowing post-processing of the image.

  • OnGUI: Called multiple times per frame in response to GUI events. Layout and Repaint events are handled first, followed by Layout and keyboard/mouse events for each input event.

  • OnDrawGizmos is used to draw Gizmos in the scene view for visualization purposes.

Note: These callbacks only apply to the built-in rendering pipeline.


Coroutines

正常的协程更新在 Update 函数返回后运行。 协程是一个可以暂停执行(yield)直到给定的 YieldInstruction 完成的函数。
  • yield The coroutine will continue after calling all Update functions on the next frame.
  • yield WaitForSeconds Continues after the specified time delay after all update functions have been called for the frame.
  • yield WaitForFixedUpdate continues after calling all FixedUpdates on all scripts. If the coroutine was spawned before FixedUpdate, it resumes after the current frame's FixedUpdate.
  • yield WWW Continue after WWW download is complete.
  • yield StartCoroutine chains coroutines and waits for MyFunc coroutine to finish first.

When the Object is destroyed

  • OnDestroy: This function is called after all frame updates for the last frame the object existed (the object may respond to Object.Destroy or be destroyed when the scene is closed).

When quitting

这些函数会在场景中的所有活动对象上调用:

OnApplicationQuit: This function is called on all game objects before the application quits. In the editor, this is called when the user stops play mode.

OnDisable: This function is called when the behavior is disabled or inactive.

Guess you like

Origin blog.csdn.net/a_codecat/article/details/128153801