The running order of event functions in Unity3d

Source: Internet
Author: User

The life cycle of a script in Unity3d is evolved in accordance with the running flow of pre-defined event functions, such as the following:

Editor mode reset:

When the script is first hung onto gameobject or when the user taps Resetbutton, reset is called to initialize the script properties, most often using the default values rendered in the Inspector View.


load the first scene:

When the scene starts, it runs once for each object in the scene, such as the following event function:

        • Awake: The game is initialized before it starts, regardless of variables and game state, and is only called once in the script life cycle. Cannot do the process, and is called before the start function and after the Prefab object is instantiated.

          The invocation of awake between gameobject is not in order, so it is possible to use awake to establish a reference between scripts. Then use start to pass the information back and forth. (For C #, Boo users, when the component serialization state is undefined when constructed, you should use awake instead of the constructor initialization).

        • onenable: only Gameobject is invoked when it is active, and typically occurs when an Monobehaviour instance is created.

before the first frame update beforeAssuming script instantiation is enabled, theStartThe function is called before the first frame is updated. Called only once in the script life cycle.

The awake is called regardless of whether the script is enabled or not, and if the script disabled during initialization, start will not be called at the same frame as awake. Awake and onenable must be called before start (the advantage is that object a instantiation code relies on the instantiation of Object B.) The instantiation of B is complete at the awake stage. A instantiation is complete at the start phase).
Between frames in between Frames:

If the pause is detected, the Onapplicationpause function is called after the current frame is run, and the call is valid between normal frame updates. After Onapplicationpause is called, an additional frame is added to display the image indicating a paused state. Onapplicationpause can be a co-process. If it runs as a co-thread. It is used two times during the initialization of the frame: the first time as a prior notification, and the second occurs in the normal process update step.


Update Order:

when you want to track game logic, animations, camera positions, and so on, you can run these tasks in the Update event function, in the order of fixedupdate, update, Lateupdate, and of course not all three of the scripts are included. The event functions of Unity3d are different from those of the three.

The coprocessor runs after all the update functions have completed. If a co-process is opened in the Lateupdate. It will also be called before the lateupdate is rendered.


Render Rendering:

  • Onprecull: called before the camera rejects the scene, culling determines which objects are visible in the camera.

    If you want to change the visual parameters of the camera (for example, Fov,transform, etc.), you should change it in this function. The visibility of the gameobject in the scene after that is dependent on the camera's number of references.

  • Onbecamevisible/onbecameinvisible: Called when the gameobject becomes visible/invisible in whatever camera. The message is sent to the script regardless of what hangs on the renderer.

    Using these two functions avoids unnecessary calculations, and the corresponding calculations are performed only when the gameobject is visible. They can be a co-process, simply using yield, and when run in the editor, the scene view camera will also cause them to be called.

  • Onwillrenderobject: Suppose the object is visible and the script is enabled. It will be called once for each camera before rendering every object after the culling operation.
  • OnPreRender: When the script is hung on the camera and enabled, it is called before the camera starts rendering the scene.

    If you change the visual parameters of the camera in this function (for example, the FOV). Then these parameters will only play a role in the next frame.

    The function can be a co-process. You can use yield only in a function.

  • Onrenderobject: Called after all fixed scene rendering. At this point you can use the GL class function or Graphics.drawmeshnow to draw your own defined geometry.

    This function is similar to Onpostrender. Except for one thing: whether the script hangs on the camera or not. Onrenderobject will be called on whatever Gameo.

  • Onpostrender: Called after the camera finishes rendering the scene. Unlike Onrenderobject, the function is called only if the script is hung on the camera and is enabled .

    If you want to do the work after all the cameras and GUI rendering, use the waitforendofframe process.

  • Onrenderimage (Pro only): Called after the scene has been rendered to post-process the image of the screen, which is sent to all scripts that are attached to the camera. The function agrees to use the shader-based filter to process the last image. The incoming source renders the texture. Terminates at the target render texture; When the camera has multiple image filters, the image order is processed, and the target rendering texture of the previous filter renders the texture as the source of the next filter.
  • Ongui: In response to GUI events, the function is called multiple times per frame, and each event is called once, and the runtime responds to the layout and redraw events, followed by the layout and keyboard/mouse events for each input event.
  • Ondrawgizmos: For visualization purposes, draw small icons in the scene view. The function uses the position of the mouse relative to the scene view, agreeing to high-speed selection of important gameobject in the scene, and of course, if the component is collapsed in inspector, the function will not be called.

co-process coroutines:Under normal circumstances, the association runs when the update function returns.

The function of the co-process is. Delay its operation (yield) until the given yieldinstruction is complete. Different uses of the co-process:

    • Yield: The co-process continues to run after all the update functions are called at the next frame.
    • yield waitforseconds: continues to run after a specified time delay, which is only available for yield statements in the process after all the update functions of the frame have been called.

    • Yield waitforfiexedupdate: After all fixedupdate are invoked on all scripts, they can be used to yield statements in the process.

    • Yield www: Continue to run after WWW loading is complete.
    • Yield startcoroutine: Wait for MyFunc to finish first. Used to link the co-process. The function always returns immediately, but yields the result so that it waits for the process to complete and then returns. Using JavaScript does not have to use this function because the compiler will do the work for you. If you use C #, you need to use this function.


when objects are destroyed when the object is destroyed:

OnDestroy This function is called after all frames have been updated. In the last frame in which the object exists (the object is destroyed to respond to Object.destroy or close a scene). The function cannot be a co-process.


when exiting when quitting:

The following functions are called for all active objects in the scene:

      • Onapplicationquit: The function will be called by all game objects before the app exits. In the editor, it will be called when the user stops playing; in Webplayer. Called when the page is closed.

      • Ondisable: When the behavior is not available or inactive. The function is called.

        When the object is destroyed. or the code for whatever cleanup work. The function can be called the same.

        When the script is loaded again after the compilation, the function can be called, immediately after the script is loaded and called onenable.


Script life cycle Flowchart:

in summary, the entire life cycle of the script can be seen in the Unity3d script life cycle flowchart .


Run order of event functions in Unity3d

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.