⬆️ ⬇️

Order of events in Unity3D

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:

  1. Events triggered by events oil oily (loading scene, user exit)

    This group of events is performed on an irregular basis.
  2. 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.
  3. 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).

Before the first frame update



In between frames



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.

')

Scene rendering (Rendering)



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:

Destruction of objects



On exit

These functions are called for all active objects in the scene:

Thus, the following script execution order occurs:



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) { //      ,    isFound = ProcessDataFromSection(colliderSectionID); colliderSectionID = (colliderSectionID ++) % colliderSectionCount; yield return null; } //   /  //   } void Start() { StartCoroutine(FindBozons()); } 




The coroutine mechanism will automatically save the state of the function execution context and return to the place of interruption (yield).

Source: https://habr.com/ru/post/147315/



All Articles