Unity Life Cycle

Source: Internet
Author: User

1. Static constructors

When the assembly is loaded, it is called, and if your unity is in the editing state, when you save a script (forcing recompilation), the static constructor is immediately called because unity loads the DLL. And it will not run again, it will only be executed once, and the unity runtime will not be executed again! On a deployed game, this constructor will be called early in the unity loading process!

Static Constructors article: Http://www.cnblogs.com/MrZivChu/p/BaseKnowledge_staticConstructor.html

2, non-static constructors

Unity will invoke the default constructor of an object at a seemingly random time. When you edit a game, the constructor will be called immediately after you save a script (forcing recompilation)!

The program is randomly called multiple times when it is run, and is also called when the program ends (self test found)

So instead of using the constructor to initialize the value of the field, Unity's awake is designed specifically for this purpose.

3, Awake

will only be called once, before the start method is called! Primarily used for initialization of field values, disabling scripts, creating game objects, or resources.load (Prefab) objects

4. Start

Executes only once, after the execution of the Awake method executes, but before the Update method executes, primarily for initialization of the program UI, such as acquiring a game object or component

5. Update

Every frame of execution, listen to user input, play animation, when the machine busy or poor performance, he will stop execution, will create a sense of pause, such as a person originally in the position of 1 meters, suddenly to 5 meters in the position, the jump frame is generated, and the following Fixedupdate method is the opposite! will be 1.1-meter meters to execute! (Self-debugging found that the update is performed prior to Ongui and executes two times after the update is performed Ongui)

6, Fixedupdate

Regardless of the current machine is busy, will be guaranteed every frame to execute once! Avoid skipping frames! Fixed updates. Fixed updates are commonly used for operations such as moving models.

7, Lateupdate

Perform update first, then execute lateupdate (the Update method executes, must then execute Lateupdate, and the update and Fixedupdate methods are executed in an indeterminate order, and sometimes fixedupdate executes multiple frames, Update only executes one frame, which is due to the jump frame (depending on your machine performance)! ), if there are now 100 scripts, each with 100 update () functions, with only one lateupdate, then this lateupdate () is performed only after 100 update () is executed in the same frame.

8, Ongui

This is where the GUI is drawn, and the GUI is erased every frame of repainting! Just draw! No concept of life cycle! All the code for drawing the GUI should be written directly or indirectly into the Ongui Method!

9,OnDestroy

Called when the current script is destroyed

10,onenable

The script is called when it is available and will not be called if the script is unavailable!

If there are three objects, A1 > A2 > a3 (Parent-child relationship), three script s1,s2,s3 are hung, and three scripts have awake,start,onenable,ondisable,update methods, unity executes in the following order:

Awake1,onenable1,awake2,onenable2,awake3,onenable3,start1,start2,start3,update1,update2,update3

If the script S1 is not available in the Awake method of script S2 (s1.enabled=false), the script executes as follows:

Awake1,onenable1,ondisable1,awake2,onenable2,awake3,onenable3,start2,start3,update2,update3

If the script S3 is not available in the Awake method of script S2 (s3.enabled=false), the script executes as follows:

Awake1,onenable1,awake2,onenable2,awake3,start1,start2,update1,update2

If the script S3 is not available (S3.enabled=false) in the Start method of the Script S2, the script executes as follows:

Awake1,onenable1,awake2,onenable2,awake3,onenable3,start1,ondisable3,start2,update1,update2

Summary: The key is to see where the settings script is not available:

A (1) if the script that sets the child in the parent's awake method is not available

then only the awake method in the child is executed, when the child is activated (enabled=true), the onenable is executed first, then the Start method is executed, and the update frame sequence method starts executing

(2) if the script that sets the child in the parent's Start method is not available

Then the awake,onenable and Ondisable methods in the subset are executed, and when the child is activated (enabled=true), the onenable is executed first, then the Start method is executed, and the update frame sequence method will also start executing

B (1) if the script that sets the parent in the awake method of the child is not available

Then the awake,onenable and Ondisable methods in the parent are executed, and when the parent is activated (enabled=true), the onenable is executed first, then the Start method is executed, and the update frame sequence method starts executing

(2) if the script that sets the parent in the child's Start method is not available

Then the Awake,onenable,start and Ondisable methods inside the parent set are executed, and when the parent is activated (enabled=true), the onenable,update and other frame sequence methods are executed first.

If the activated script does not call the Start method before, the Start method is called once the script is activated! Specifically, look at the first picture of the life cycle, behind the article!

11, Ondisable

If the script is set to be unavailable it will be executed and will be executed once at the end of the program!

Onenable and ondisable are only affected by the available state of the script (enabled)

The onbecamevisible and onbecameinvisible are affected by whether the object is visible (see below)! Even if the script is set to not be available, onbecamevisible and onbecameinvisible will be executed, mainly to see if the object is displayed in the scene!

onbecamevisible and onbecameinvisible:

(1) When you start loading an object:

Game show Scene shows onbecameinvisible does not perform onbecamevisible execution

Game does not show Scene does not show onbecameinvisible do not perform onbecamevisible do not execute

Game does not show Scene display onbecameinvisible does not perform onbecamevisible execution

Game show Scene does not show onbecameinvisible do not perform onbecamevisible execution

Summary: As long as there is a display in the game and scene, Onbecamevisible will execute! And Onbecameinvisible has never been executed.

(2) When moving an object:

The game and scene objects must disappear at the same time in two scenes onbecameinvisible to execute

Scene and game as long as one party enters the scene, Onbecamevisible executes.

Summary of script execution sequence:

If there are now three Gameobject objects: A1 > A2 > a3 (A1 is the parent of A2, A2 is theparent of A1, Unity executes the script from top to bottom, that is, the script on the parent node is executed before the child node script is executed. If there is more than one script on the child node, then it is executed in the top-down order , the three objects each have a script: S1,s2,s3, and the three script code are the same, all have awake,start,update,lateupdate,fixupdate, Then when running the program, the program will be grouped, that is, the awake method in the S1,S2,S3 group, the Start method to form a group, the Update method to form a group, the Lateupdate method composed of a group, the Fixupdate method composed of a group, Finally follow the order of Awake,start,fixupdate,update,lateupdate (Fixupdate and Update order indeterminate)! That is, the awake group inside the awake method to complete the execution, and then to execute the code inside the Start,fixupdate,update,lateupdate group: The execution sequence is as follows:

AWAK1 Awak2 Awak3 Start1 Start2 Start3 FixUpdate1 FixUpdate2 FixUpdate3 Update1 Update2 Update3 LateUpdate1 LateUpdate2 La TeUpdate3

Reference: Http://wiki.unity3d.com/index.php?title=Life_cycle#0_:_Start

Unity Life Cycle

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.