The execution order and time point of the built-in event function in Unity3D

In the Unity3D script, several event functions that come with Unity3D are executed as scripts in a predetermined order. Its execution sequence is as follows:

Editor

  • Reset: The Reset function is called to initialize the script properties when the script is first attached to the object, and is also called when the Reset command is used.
    Editor's Note: Reset is called when the user clicks the Reset button on the Inspector panel or adds the component for the first time. Reset is most commonly used to give a default value in an insight panel.
    First Scene Load
    These functions are called at the start of a scene (only once for each object in the scene).

  • Awake: This function is always called after a preset is instantiated before any Start() functions. If a GameObject is inactive, the Awake function will not be called during startup until it is active ( active).

  • OnEnable: It will only be called when the object is in the active state. This function will only be called after the object is enabled (enable). This happens when a MonoBehaviour instance is created, such as when a level is loaded or a GameObject with a scripted component is instantiated.
    Note: When a scene is added to the scene, the Awake() and OnEable() functions on all scripts will be called before Start(), Update(), etc. any of them are called. Naturally, this cannot be enforced when an object is instantiated during gameplay.

Before the first frame update

  • Start: The Start() function will be called before the first frame of the Update() function as long as the script instance is enabled.
    For objects that are added to the scene, the Start() function on all scripts will be called before any of their Update() functions. Naturally, this cannot be used when an object is instantiated during gameplay. Mandatory.

In between frames

  • OnApplicationPause: This function will be called at the end of a frame between normal frame updates when a pause is detected valid. There will be an extra frame after OnApplicationPause is called to allow the game to display the display image that is in the paused state.

Update Order
There are several different event functions you can use when you are tracking game logic and state, animations, camera positions, etc. A common pattern is to perform most tasks in the Update() function, but there are other functions you can use.

  • FixedUpdate: The FixedUpdate function is often called more frequently than the Update function. It will be called multiple times per frame, if the frame rate is low it may not be called between frames, even if the frame rate is high. All graphics calculations and updates are performed immediately after FixedUpdate. When performing movement calculations in FixedUpdate, you do not need to multiply Time.deltaTime by your value, this is because FixedUpdate is called in real time, independent of frame rate.
  • Update: Update is called every frame, and it is the main load function for frame updates.
  • LateUpdate: LateUpdate will be called every frame after Update finishes, and any computations performed in Update end when LateUpdate starts. LateUpdate is commonly used for third-person camera follow.

Rendering

  • OnPreCull: Called before the camera culls the scene. Culling depends on which objects are visible to the camera, OnPreCull is only called before culling takes effect.
  • OnBecameVisible/OnBecameInvisible: Called when an object becomes visible/invisible to any camera.
  • OnPreRender: Called before the camera starts rendering the scene.
  • OnRenderObject: Called after the specified scene is rendered, you can use the GL class or Graphics.DrawMeshNow to draw custom geometry here.
  • OnPostRender: Called after the camera finishes rendering the scene.
  • OnRenderImage(Pro Only): Allows screen image post-processing to be called after the scene is completed.
  • OnGUI: In response to GUI events, it will be called multiple times per frame (usually at least twice). Layout and Repaint events are processed first, followed by each input event corresponding to Layout and keyboard/mouse events.
  • OnDrawGizmos: Used to visually draw some gizmos in the scene view.

Coroutines

Normal coroutine updates are run after the Update function returns. A coroutine can suspend execution (yield) until the given compliance instruction (YieldInstruction) is completed, written in different applications:

  • yield: The coroutine will continue to execute in the next frame after all Update functions have been called.
  • yield WaitForSeconds: Continue execution after a specified time delay, after all the Update functions have finished calling the frame.
  • yield WaitForFixedUpdate: Lasts after the FixedUpdate function on all scripts has been called.
  • yield WWW: lasts after the WWW download is complete.
  • yield StartCoroutine: Coroutine chain, will wait until MuFunc function coroutine execution completes first.

销毁(When the Object is Destroyed)

  • OnDestory: This function is called one frame before an object is destroyed, and is executed after all frames update the last frame where an object exists. Objects may be destroyed in response to Object.Destroy or when a scene is closed.
    When Quitting
    These functions will be called on all active objects in your scene:
  • OnApplicationQuit: This function is called on all game objects before the application exits, when the user stops PlayMode in Editor mode, and when the web view closes in the web player.
  • OnDisable: Called when the behavior becomes disabled or inactive.

Script Lifecycle Flowchart
write picture description here
write picture description here
write picture description here

Original text: https://blog.csdn.net/clyang92/article/details/51580527

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325205802&siteId=291194637