This note mainly talks about what you need to know about MonoBehavior, the script parent class created by Unity by default.
Life cycle
The picture below is from Unity official. In this picture, we can see the sequence of functions in MonoBehavior.
Initialization
In the Initialization phase, as shown in the figure above, the functions used are Awake(), Reset(), OnEnable() and Start().
Awake
Awake is called when loading a script instance. This is the first function called among all functions. It is used to initialize (usually local or independent of other scripts) objects and variables.
This function will be called after all objects have been initialized, but this function will also be called before the game object is Enabled, and even if the script component is not activated, it will is called.
Awake will only be called once.
Reset
Reset is called in the editor when the user adds a component for the first time or resets the script. Usually used to set default values.
This function will not be called while the game is running. In editor mode, this function will be called once when adding a component to a game object or clicking the component's Reset option (inside the three dots in the upper right corner).
OnEnable
Called when the script instance is activated.
The activation status can be seen in the editor mode. If the √ in the box to the left of the script name is clicked, it means Disabled. If is clicked, it is Enabled.
In C#, this script instance can be activated/disabled via .isEnabled = true/false.
This function is after Awake and before Start. Because this function will be called as long as the script instance is activated (changing from Disabled to Enabled is considered activation), this function can be called multiple times.
Start
Called once before the first Update of the script, this function is also usually used to set up initialization, especially the initialization that needs to rely on other objects and other scripts.
This function is called after Awake and OnEnable. If the script is not activated, this function will not be called.
Physics
In the Physics stage, we mainly discuss FixedUpdate, OnCollisionXXX, OnTriggerXXX. It will also involve some animation-related functions, such as OnAnimatorMove(), OnAnimatorIK(int layerIndex), OnStateMachineEnter/Exit, but the order in which they are called is not very important. Let’s look at the first three first.
FixedUpdate
FixedUpdate is used to process physics-related code, such as rigid body movement, rotation, physical calculations, etc. It is updated at a fixed time (called 50 times per second, i.e. in steps of 0.02 seconds), independent of frame rate, so the physics simulation remains consistent across different frame rates. The frequency with which it is called can be adjusted in Unity's TimeManager.
OnCollisionEnter/Stay/Exit (Collision collision)
OnCollisionXXX functions all handle collision events. They all take a Collision as parameter.
Enter: When the colliders of two objects touch for the first time, this function will be called once for each colliding object.
Stay: Called during each physics update when collision persists. The fixed step size of physics updates is the same as FixedUpdate, which is 0.02 seconds (or it can be said that FixedUpdate is called in every physics update).
Exit: This function is called once when the collision ends.
Note that the OnCollision series of functions require at least one of the two parties to collide to carry a Rigidbody component.
OnTriggerEnter/Stay/Exit (Collider other)
OnTriggerEnXXX functions all handle trigger events. They all take a Collider as a parameter. Similar to the OnCollision series, we will not go into details here.
OnControllerColliderHit(ColliderHit hit)
Specifically, we can use this function when dealing with Character Controller components and other collider bases.
Input and Game Logic
In the Input and Game Logic phases, the main functions we want to mention are OnMouseXXX, Update and LateUpdate.
OnMouseDown/Up/Drag/Over/Exit
The OnMouse series handles events that are triggered when the user uses the mouse to interact with the Collider of the game object.
Down: event triggered when the user clicks;
Up: Triggered when the user releases the mouse button pressed on the game object Collider;
Drag: Triggered when the user holds down the mouse button and moves on the game object Collider;
Over: Continuously called when the user mouses over the game object Collider;
Exit: Called when the user's mouse is no longer hovering over the game object Collider.
It should be noted that the game object must have a Collider.
Update
The Update function is called once every frame to handle regular game logic.
Since it is called every frame, and the frequency is the same as the frame rate, it is suitable for processing animations and smooth movement.
LateUpdate
The LateUpdate function is called after the Update function, and is executed after the Update execution of all GameObjects in the scene is completed. Therefore, this function is usually used to solve some updates that require order dependence.
This function is commonly used for adjusting cameras and following effects, as it ensures that all objects have completed their Update movement.
Rendering
In the Scene Rendering stage, this part of the function is mainly to provide the part of the data in the rendering pipeline that the CPU provides materials to the GPU. Note that this part of the function may behave differently in URP and Built-In because Unity provides different rendering pipelines.
OnPreCull
Called before the camera starts culling, it can be used to modify the properties of the camera or the state of the scene. ,
OnWillRenderObject
This function is called on each camera before rendering the object. Commonly used for pre-rendering settings at the object level, and can also be used for multi-camera view adjustments.
OnBecameVisible / OnBecameInvisible
This is a function that is automatically called when the object becomes visible or invisible.
OnPreRender / OnRenderObject / OnPostRender
Function called before the camera starts rendering the scene/when the scene is rendered/after the scene is rendered. should be used with caution as it may affect the performance of the rendering pipeline.
OnRenderImage
Has no effect in URP. Called before all images are ready to be displayed on the screen, typically used for image post-processing effects. It is easy to have a certain impact on performance.
Reference materials:
Unity Manual, https://docs.unity3d.com/Manual/ExecutionOrder. html
Comentarios