Good day.
Not so long ago I became interested in this engine. Fortunately, there are a lot of Russian materials on this topic, including on the habr.
However, I have never seen a description (in Russian) of the order of occurrence of various events in the games created, and this is one of the factors for the optimal placement of scripts in various functions.
Therefore I decided to translate the
Execution Order of Event Functions section from the Unity3D English-language help.
Hopefully something will come out of it. Who is interested - welcome under the cat =)
Translator's Preface
But, before the start of the official certificate, I want to note that the events in Unity3D are divided into three large groups:
- Events triggered by events
oil oily (loading scene, user exit)
This group of events is performed on an irregular basis. - Events triggered when drawing a frame
In this case, all used scripts are called in the screen drawing cycle, which means they will directly affect the FPS (frame rate per second). Therefore, it is necessary to work very carefully with functions that require a lot of processing time. - Events caused by the calculation of physics
And the last group. To calculate physics, a separate, independent thread is created, the events in which are triggered with a certain time interval. The size of this interval can be configured in the menu: Edit -> Project Settings -> Time -> Fixed Timestep.
Knowing this breakdown, you can already make decisions about where to place which code is best.
However, all calculations performed both in the calculation of physics and in drawing affect the responsiveness of the game. Therefore, when developing an application, it is desirable to make the most resource-intensive calculations in coroutines (Coroutine).
We now turn directly to the translation of the help section.
The order of the functions of events
In Unity3D, there are a number of events running in a specific order. We describe this order below:
First loading scene
These functions are called when the scene starts (once for each object in the frame).
- Awake : This function is always called before the start of any functions, as well as immediately after the prefab initialization.
- OnEnable : (called when the object is active): This function is called only after the object has been enabled.
Before the first frame update
- Start : is called before the first frame is drawn, only if the script is defined.
In between frames
- OnApplicationPause : This event is fired at the end of a frame when a pause is detected, actually between regular frame updates. After OnApplicationPause, one additional frame is drawn to show the window that is displayed during the pause.
Update order
To track game logic, interaction and animation of objects, camera positions, etc., there are several different events you can use. The general mechanism for performing most tasks is in the Update () function, but there are other functions.
- FixedUpdate : FixedUpdate () does not depend on Update (), and can be called more frequently or less frequently (usually it is called less often if the FPS is high enough). This event can be triggered several times in a frame, if the FPS is low and maybe not even triggered between frames, if the FPS is high. All physical calculations of the engine and update occur immediately after FixedUpdate (). When applying motion calculations inside FixedUpdate (), you do not need to multiply your value by Time.deltaTime. This is because FixedUpdate () is called from a timer, independent of the frame rate.
- Update : Update () is called once per frame. This is the main event for drawing a frame.
- LateUpdate : LateUpdate () is called once per frame after Update () is completed. Any calculations that are made in Update () will be completed when you call LateUpdate (). The main use of LateUpdate () is usually third-person camera tracking. If you move your character in the Update () event, then you can move the camera and calculate its location in the LateUpdate () event. This will ensure that the character has passed completely in front of the camera, and has fixed its position.
')
Scene rendering (Rendering)
- OnPreCull : Called before assembling the scene on the camera. The assembly determines which objects are visible to the camera. OnPreCull is called only if there will be a “cropping” of the scene from invisible objects.
- OnBecameVisible / OnBecameInvisible : Called when an object becomes visible / invisible to any camera.
- OnWillRenderObject : Called once for each camera if the object is visible.
- OnPreRender : Called before scene rendering starts on the camera.
- OnRenderObject : Called when all objects in the scene have been drawn. You can use the GL or Graphics.DrawMeshNow functions to create your drawings on this camera.
- OnPostRender : Called after the scene has been drawn on the camera.
- OnRenderImage (Pro version only): Called after the scene is drawn, to post-process the image on the screen.
- OnGUI : is called several times in the frame in response to interface events. Layout and fill events are processed first and then keyboard / mouse input events.
- OnDrawGizmos : Used to draw gizmo on stage.
Coroutines
Normally, a coroutine call is made after the Update () function returns. A coroutine is a function that can pause a performance until it is executed. Different uses of Coroutine:
- yield : the coroutine will continue after all the Update () functions that will be called in the next frame.
- yield WaitForSeconds (2) : Continue after the specified delay time, when all Update () functions have already been called in the frame
- yield WaitForFixedUpdate () : Continues when all FixedUpdate () functions have already been called
- yield WWW : Continues when WWW content loading is complete.
- yield StartCoroutine (MyFunc) : Coroutine associations , a coroutine call will wait for the MyFunc function to complete.
Destruction of objects
- OnDestroy : This function is called for the last frame of the object's existence (the object may be destroyed in response to Object.Destroy or when the scene is closed).
On exit
These functions are called for all active objects in the scene:
- OnApplicationQuit : This function is called for all game objects before closing the application. In the editor, this happens when the user terminates PlayMode. In a web player, this happens when the web player is closed.
- OnDisable : This function is called when the object is turned off or becomes inactive.
Thus, the following script execution order occurs:
- All Awake Events
- All events Start
- cycle (in increments of delta time)
- All FixedUpdate Functions
- working off the physics engine
- event triggers OnEnter / Exit / Stay
- collision events OnEnter / Exit / Stay
- Rigidbody transform according transform.position and rotation
- OnMouseDown / OnMouseUp other input events
- All events Update ()
- Animation, Blending and Transformation
- All LateUpdate events
- Rendering
Tips
If you run coroutines in LateUpdate, they will also be called after LateUpdate just before rendering.
Coroutines are executed after all Update () functions.
Translator's epilogue
This is my first article on Habré, and my first translation of technical documentation.
So I ask all suggestions and comments (constructive) in the comments.
I hope this post will be useful to someone =)
PS addition from user
Leopotam
Coroutine is just a piece of code executed in the main thread. This is very important to understand, because simply putting a heavy calculation into the coroutine and calculating that everything will be fine is fundamentally wrong, the calculations will simply score the flow exactly the same as if they were performed in Update or elsewhere in standard methods. It is necessary to break up the calculations into iterations so that if you repeat the iteration, the process continues. The whole point of coroutines is to automate the invocation of these iterations on each drawing cycle.
For example:
IEnumerator FindBozons() { var isFound = false; var colliderSectionID = 0; var colliderSectionCount = 10; while (!isFound) {
The coroutine mechanism will automatically save the state of the function execution context and return to the place of interruption (yield).