Execution order of event Functions, the sequence of execution of the Unity 3d events function

Source: Internet
Author: User

Learning Unity3d, it is important to feel the order of events. Please translate the official documents.

Execution Order of Event Functions

Order of execution of event functions

In Unity scripting, there is a number of event functions that get executed in a predetermined order as a script executes. This execution order is described below:

In a Unity script, there are a number of events that are executed as scripts in a predetermined order.

Editor

    • Reset: Reset was called to initialize the script's properties when it was first attached to the object and also when the reset< /c2> command is used.
    • When attaching to an object or executing the reset command for the first time, the reset event is called to initialize the properties of the script. (member variable)

First Scene Load

These functions get called when a scene starts (once for each object in the scene).

    • Awake:  this function is all called before any Start functions and also just after a pre Fab is instantiated. (If a gameobject is inactive during start up awake are not called until it is made active)
    • The
    • awake function is executed before all start functions and after prefab initialization (if Gameobject is not activated, this gameobject awake function is not performed for this gameobject,awake event stage)
    • onenable:   (only called if the Object was active): This function was called just after the O Bject is enabled. This happens when a Monobehaviour instance was created, such as when a level was loaded or a gameobject with the script comp Onent is instantiated.
    • in the Gameobject is active state, when Gameobject is instantiated, the special Compenent:monobehaviour in Gameobject is initialized, and after that, the script is executed onenable Event.
    • onlevelwasloaded:  this function is executed to inform the game this a new level has been Loaded.
    •  

Note that for objects added to the scene, the awake and onenable functions for All scripts would be called before Start, Update, etc is called for any of them. Naturally, this cannot was enforced when a object is instantiated during gameplay.

For all objects in the scene, the awake and onenable functions in the script are called before functions such as start,update and so on, which is essentially non-enforceable during object initialization.

Before the first frame update

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

For objects added to the scene, the Start function is called on all scripts before Update, etc is called for any of them. Naturally, this cannot was enforced when a object is instantiated during gameplay.

Start is called before the first frame is updated. Of course the script must be enable. Because it is called before the first frame, this means that it will only be executed once.

In between frames

Between each frame

    • Onapplicationpause: This was called at the end of the frame where the pause is detected, effectively between the normal frame updates. One extra frame'll be issued after Onapplicationpause are called to allow the game to show graphics that Indicat E The paused state.

Update Order

Update Order

When your ' re keeping track of game logic and interactions, animations, camera positions, etc, there is a few different Ev Ents can use. The common pattern is to perform most of the tasks inside the Update function, but there was also other functions you CA n use.

There are a number of different events that can be used when dealing with game logic, interactions, animations, camera positions, and so on.

Most will be handled in the Update method, which is a common way. Of course there are some other functions that can be used.

    • fixedupdate: Fixedupdate is often called more frequently than Update. It can be called multiple times per frame, if the frame rate was low and it could not be called between frames at all if the Frame rate was high. All physics calculations and updates occur immediately after fixedupdate. When applying movement calculations inside fixedupdate, don't need to multiply your values by Time.delta time. This is because fixedupdate are called on a reliable timer, independent of the frame rate.

The fixedupdate:fixedupdate is usually shorter than the update call cycle, and if the frame rate is low, it may be called multiple times between frames, and if the frame rate is higher then there may be no calls between frames. When Fixedupdate is called, all physical calculations and updates are immediately updated.

When calculating motion in fixedupdate, you do not need to use time.deltatime to multiply your values, because fixedupdate is independent of the frame rate and relies on a reliable timer to invoke.

(The following conjecture: So it is common to put some physical characteristics, interaction and other changes in the timing of the fixupdate, because the engine will be fixed call physical and interactive updates, so to avoid poor graphics performance of the target machine, resulting in low frame rate, The appropriate way is to put physical properties and interactions into the fixupdate, so that although the display is slow, but the whole game of internal logic operation is normal)

    • Update: Update is called once per frame. It is the main workhorse function for frame updates.
    • lateupdate: Lateupdate is called once per frame, after Update has finished. Any calculations that is performed in Update would has completed when lateupdatebegins. A common use for lateupdate would is a following Third-person camera. If you do your character move and turn inside Update, you can perform all camera movement and rotation calculat Ions in lateupdate. This would ensure that the character have moved completely before the camera tracks its position.

Rendering

  • Onprecull: Called before the camera culls the scene. Culling determines which objects is visible to the camera. Onprecull is called just before culling takes place.
  • onbecamevisible/onbecameinvisible: Called when a object becomes 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 regular scene rendering are done. You can use the GL class or Graphics.drawmeshnow to draw the custom geometry at this point.
  • Onpostrender: Called after a camera finishes rendering the scene.
  • Onrenderimage: Called after scene rendering are complete to allow post-processing of the image, see post-processing Effects.
  • Ongui: Called multiple times per frame in response to GUI events. The layout and Repaint events is processed first, followed by a Layout and Keyboard/mouse event for each input event.
  • Ondrawgizmos Used for drawing gizmos in the scene view for visualisation purposes.

Coroutines

Normal coroutine Updates is run after the Update function returns. A coroutine is a function which can suspend its execution (yield) until the given yieldinstruction finishes. Different Uses of coroutines:

    • yield The coroutine would continue after all Update functions has been called on the next frame.
    • yield Waitforseconds Continue after a specified time delay, after all Update functions has been called for the frame
    • yield waitforfixedupdate Continue after all fixedupdate have been called on all scripts
    • yield WWW . Continue after a WWW download have completed.
    • yield Startcoroutine Chains the Coroutine, and would wait for the MyFunc coroutine to complete first.

When the Object is destroyed

    • OnDestroy: This function was called after all frame updates for the last frame of the object's existence (the object might be Destroye D in response to Object.destroy or at the closure of a scene).

When quitting

These functions get called on all the active objects in your scene:

    • Onapplicationquit: This function was called on all game objects before the application is quit. In the editor it's called when the user stops PlayMode.
    • ondisable: This function was called when the behaviour becomes disabled or inactive.

Script Lifecycle Flowchart

The following diagram summarises the ordering and repetition of event functions during a script ' s lifetime.

Execution order of event Functions, the sequence of execution of the Unity 3d events function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.