[Unity Study Notes] Life Cycle

Official documentation: Execution order of event functions


Script life cycle

As shown in the figure:
Insert image description here
the life cycle of a script mainly goes through the following stages:


initialization

Initialization phase, (including initializing Awake, OnEnable, and then Editor's Reset is interspersed between the initialization process, so we can override the Reset method in the script, which will be executed before the start of the game in the editor. Finally Start) , when we start the game, the initialization sequence is Awake, OnEnable, Reset (if defined and in the editor), Start

Awake: This function is called after instantiating the prefab. (If the game object is inactive during startup, Awake will not be called until after activation.)

OnEnable: (only called when the object is active) Call this function immediately after enabling the object.
This call is made when a MonoBehaviour instance is created , such as when a level is loaded or a game object with a script component is instantiated.

Start: Start is called before the first frame update only when the script instance is enabled.

The above moments are all executed before the first frame is updated. Generally speaking, the object must go through Awake and OnEnable before it enters Start. For example, if there are two objects AB, assuming A is executed before B, the execution order is A.Awake, A.OnEnable. ,B.Awake,B.OnEnable,A.Start,B.Start.


Update order

After the initialization is completed, the update phase is entered, in which the execution order of different update methods is different:

FixedUpdate: The update duration at this stage is fixed, about 0.02f once. It is mainly used for the physical system. The frequency of calling FixedUpdate often exceeds Update. If the frame rate is low, this function may be called multiple times per frame; if the frame rate is high, the function may not be called at all between frames. All physics calculations and updates will occur immediately after FixedUpdate. When applying motion calculations within a FixedUpdate, there is no need to multiply the value by Time.deltaTime. This is because FixedUpdate is called based on a reliable timer (independent of frame rate).

Update: Update is called once per frame. This is the main function used for frame updates. Update is affected by the frame rate, so some game control logic is usually placed here.

LateUpdate: Call LateUpdate__ once per frame (after Update__ completes). When LateUpdate begins, all calculations performed in Update are complete. A common use of LateUpdate is to follow a third-person camera. If you have your character moving and turning within Update, you can perform all camera movement and rotation calculations in LateUpdate. This ensures that the character has fully moved before the camera tracks its position.

As can be seen from the above life cycle diagram, the execution order of the three update methods in the update phase should be sequential. The first method is not affected by the frame rate, while Update and LateUpdate are affected by the frame rate, and LateUpdate is executed after Update. , so we can consider when to use what update method from the perspective of execution order and execution frequency.


animation update loop

The gray area in the figure is the order in which animation updates are executed. The processes of updating in FixedUpdate in the physical phase and in Update in the main logic phase of the game are similar.

First perform a fixed update of the state machine, then enter the Entry or Exit state of the state machine (if executed in the state machine), and then handle other animation events.

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

You can see that StateMachineBehaviour, that is, callbacks will be given after the state switching between state machines. You can define callback functions when these states switch.


Various events

In the life cycle, we can see that the execution order of events is roughly processed according to their categories:

OnState, OnAnimation and other animation events have the highest priority

OnTrigger, OnCollistion and other physical events, followed by animation events

OnMouse, OnKey (guess, not sure) and other input events, followed by physical events

Animation events such as OnState and OnAnimation are updated again during Update

OnRender... waits for some scene rendering events and updates after LateUpdate

OnDrawGizmos, Gizmos rendering, used to draw auxiliary icons in the scene view for visualization after the scene is rendered.

OnGUI, GUI rendering, after Gizmos rendering. Called multiple times per frame in response to GUI events. Layout and redraw events are handled first, then layout and keyboard/mouse events for each input event.

OnApplicationPause pauses the application, called after the frame in which the pause occurs, but emits another frame before the actual pause.

It can be seen that the main process of the entire intermediate event processing is: first handle the animation event, then handle the physical event, then the input event, then the animation is updated, then some rendering events, and the rendering of the GUI is after everything.


end stage

Call the following function on all active objects in the scene:

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

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

Object.Destroy This function is called after all frame updates have been completed for the last frame in which the object existed (the object may be destroyed at the request of Object.Destroy or when the scene is closed).

The above events such as OnDisable, Destroy, etc. can be executed in the game, while the OnApplicationQuit method can only be called when execution exits. The entire exit process is OnApplicationQuit, OnDisable, Destroy. The exit method should be Application.Quit.


stage analysis

The above process can be roughly divided into: initialization stage, physical stage, input detection stage, game logic stage, rendering stage, and end stage.

The initialization phase is performed once at the beginning of the game, and the end phase is only performed once at the end of the game. The remaining stages are sequentially cycled every frame.

The internal animation updates are interspersed between FixedUpdate in the physical phase and before physical events, as well as between Update in the game logic phase and LaterUpdate. The input detection phase is between the physical phase and the game logic phase, that is, at the end of FixedUpdate and before Update. The rendering phase is after the game logic phase and is at the end of the entire frame. When the rendering phase ends, it means the frame ends.

Coroutine returns

It is not difficult to find that in fact, the return time of the coroutine can be regarded as a mark of the starting point and end point of some stages. For example, yield WaitForFixedUpdatethe moment that marks the end of FixedUpdate, yield nulland some other subsequent returns after Update, yield WaitForEndOfFramerepresent the moment at the end of each frame. (Intermediate English translation: If some coroutines that are supposed to return are interrupted, and then resume execution, they must also execute yield in Update. The translation may be incorrect)


Summarize

Execution sequence of important moments in the entire life cycle:

  • initialization phase
  • Awake, OnEnable, Restart (editor), Start
  • physical stage
  • OnFixedUpdate
  • (internal animation execution)
  • yield return WaitForFixedUpdate
  • input stage
  • OnMouseXXX
  • game logic stage
  • Update
  • yield return null
  • yield return WaitForSeconds
  • yield return WWW
  • yield return StartCoroutine
  • (internal animation execution)
  • LaterUpdate
  • Rendering stage
  • SceneRender scene rendering
  • Gizmos rendering
  • GUIRender (GUI rendering last)
  • yield return WaitForEndOfFrame marks the end of the frame
  • (Pause, return to OnFixedUpdate at the end of the frame or end the game)
  • end stage
  • OnApplicationQuit,OnDisable,Destory

Guess you like

Origin blog.csdn.net/milu_ELK/article/details/132023730