Default function call order in Unity3d (Monobehaviour)

Source: Internet
Author: User

The first thing to make clear is that Monobehaviour is the base class for each script. Each JavaScript script automatically inherits Monobehaviour. When you use C # or Boo, you need to explicitly inherit monobehaviour.

Unity does not support multithreading, that is, we have to operate it in the main thread, but unity can create a lot of scripts at the same time, and can be bound to different game objects, each of them in the implementation of their own life cycle feel like multi-threading, parallel execution of the script, how is it executed?

Let's do a little experiment to verify it. As shown, create three game objects in the hierarchy view, create three scripts in Project view, and then bind the script to the corresponding game object in order.

Three script code exactly the same, just do a bit of the name of the distinction, the code to write the ugly we just as a test!!

12345678910111213141516171819202122232425262728293031 using unityengine; using System. Collections; public class Script0 : monobehaviour { void awake () { Debug. Log("Script0 ========= Awake"); } bool isupdate = false; void Update () { if(! Isupdate) { Debug. Log("Script0 ========= Update"); isupdate = true; } } bool islateupdate = false; void lateupdate() { if(! Islateupdate) { Debug. Log("Script0 ========= lateupdate"); islateupdate = true; } }}

Play the game and see their order of execution. As shown, awake, Update, lateupdate, and no matter how many times the game is played, the order in which they are executed is exactly the same.

Then we're doing a test to comment out the Script0 Update Method!!

12345678910111213141516171819202122232425262728293031 using unityengine; using System. Collections; public class Script0 : monobehaviour { void awake () { Debug. Log("Script0 ========= Awake"); }//bool isupdate = false;//Void Update ()// {//if (!isupdate)// {//Debug.Log ("Script0 ========= Update");//Isupdate = true;// }// } bool islateupdate = false; void lateupdate() { if(! Islateupdate) { Debug. Log("Script0 ========= lateupdate"); islateupdate = true; } }}

Play the game and look at the results of it. The script executes in exactly the same order as before, Script0 even if the update method is removed, but it does not execute the Lateupdate method directly, but waits until both SCRIPT1 and Script2 update methods are executed. is going to execute all the lateupdate methods.

With these two examples, we can clearly determine how the unity daemon executes the script. Each script awake, Update, Lateupdate, Fixedupdate, and so on, the method in the background has a summary.

Awake in the background ()

{

Awake () in script 0;

Awake () in script 1;

Awake () in script 2;

}

Backstage methods Awake, update, Lateupdate, Fixedupdate, and so on are in order, and so on all sub-scripts in the awake after the execution of the Start, update, Lateupdate and so on. So here's an explanation of unity's lack of multi-threading concepts.

Update in the background ()

{

Update() in script 0;

Update() in script 1;

Update() in script 2;

}

Unity also provides a set of collaborative tasks, in fact, its principle is exactly the same as above, they are fake multithreading. Did we go back to the order in which unity scripts were executed? We're looking at two scripts!

Create a cube object in the Awake method of script 2.

12345678910 using unityengine; using System. Collections; public class Script2 : monobehaviour { void awake () { gameobject. Createprimitive(primitivetype. Cube); }}

To get this cube object in the Awake method of script 0

12345678910111213 using unityengine; using System. Collections; public class Script0 : monobehaviour { void awake () { gameobject go = gameobject. Find("Cube"); Debug. Log(go. Name); }}

If the execution order of the script is to execute SCRIPT2 first and then awake in the execution Script0 then Script0 can get to the cube object, but if the script is executed first Script0 then Script2, Then the SCRIPT0 will certainly report a null pointer error.

So the script in the actual project will be very, very much, in their order we don't know anyone. So my advice generally creates a game object or a Resources.load (Prefab) object in the Awake method. Go to the Start method to get the game object, or the game component, so you can be sure of the foolproof.

If you want to control the execution order of the script, it's not totally impossible! Unity can set the order in which the scripts are executed. As shown, select any script in the Inspector View and click Execution Order. Button.

As shown, clicking on the "+" in the lower right corner will bring up the drop-down window, including all the scripts in the game. When you finish adding a script, the smaller the number below the default time, the earlier the script will take the lead, if the script that is not set will be executed in the default order.

According to my this setting, the program will first execute SCRIPT0 then Script1 finally Script2, welcome to discuss together!! Wow, ka-ka.

Default function call order in Unity3d (Monobehaviour)

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.