Unity3D script execution sequence and compilation sequence, unity3d script

Source: Internet
Author: User

Unity3D script execution sequence and compilation sequence, unity3d script

Execution sequence and compilation sequence of scripts in Unity3D


Dog planing Learning Network]

In Unity, you can create many scripts at the same time and bind them to different game objects, each of which runs in its own lifecycle. This article describes how to compile and execute scripts in Unity.


Execution sequence of event Functions

Let's talk about the execution sequence first. The execution sequence of event functions in the script officially provided is as follows:






We can do a small experiment to test it:
Create three game objects in the Hierarchy view and three scripts in the Project view, as shown in. Then, bind the scripts to the corresponding game objects in sequence:





The codes of the three scripts are exactly the same, but they are differentiated by name:

Using UnityEngine;
Using System. Collections;
Public class Scring0: 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 check the execution sequence. As shown in, Awake, Update, and LateUpdate have the same execution sequence no matter how many games are running.






Next, let's perform another test and comment out the Update method of Script0 !!

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;
}
}
}




Run the game again and check its results. The execution sequence of the script is exactly the same as before. Even if Script0 deletes the Update method, it does not directly execute the LateUpdate method. Instead, after the Update Methods in Script1 and Script2 are executed, then execute all LateUpdate methods.



 




Through these two examples, we can clearly determine how the Unity background executes the script. The Awake, Start, Update, LateUpdate, FixedUpdate, and so on of each script. All the methods are summarized in the background:


Background Awake ()
{
// Temporarily follow the script execution sequence in. The subsequent steps will show that the sequence can be customized.
Awake () in script 2 ();
Awake () in script 1 ();
Awake () in script 0 ();
}


Background methods such as Awake, Update, and LateUpdate are all in sequence. After Awake is executed in the script on all game objects, Start, Update, LateUpdate, and other methods are executed.


Background Update ()
{
// Temporarily follow the script execution sequence in. The subsequent steps will show that the sequence can be customized.
Update () in script 2 ();
Update () in script 1 ();
Update () in script 0 ();
}



Then let's look at the execution sequence of the script: Create a cube object in the Awake method of script 0, and then obtain the cube object in the Awake method of script 2. The Code is as follows:

// Script0.cs
Using UnityEngine;
Using System. Collections;
Public class Script0: MonoBehaviour
{
Void Awake ()
{
GameObject. CreatePrimitive (PrimitiveType. Cube );
}
}

// Script2.cs
Using UnityEngine;
Using System. Collections;
Public class Script2: MonoBehaviour
{
Void Awake ()
{
GameObject go = GameObject. Find ("Cube ");
Debug. Log (go. name );
}
}




If the script execution sequence is to execute Script0 first, and then execute Script2, Awake in Script2 can get the cube object correctly. However, if the script execution sequence is to execute Script2 first, then Script0, then Script2 will certainly report a null pointer error.


There will be a lot of scripts in the actual project, and no one knows the sequence of their execution (some say they are executed according to the stack structure, that is, the script bound to the game object is executed first. This can be obtained from the above example, but it is not officially mentioned, and further research is required ). However, we recommend that you create a game object or


Resources. Load (Prefab) object, and then get the game object or component in the Start method, because the execution sequence of the event function is fixed, this can ensure that nothing is lost.
In addition, Unity provides a method to set the Script Execution sequence. in the Edit-> Project Settings-> Script Execution Order menu, you can see the following in the Inspector panel:




Click "+" in the lower right corner to bring up the drop-down window, including all the scripts in the game. After the script is added, you can drag the script to sort the script. The smaller the number after the Script Name is, the closer the script is to be executed. The Default Time indicates the execution sequence of those scripts that do not set the execution sequence of the script.





Based on the settings shown in the preceding figure, let's take a look at the output result of the console to check whether our settings work (Note: uncomment the Update method in the Script0 script ):





Script compilation Sequence

The script compilation sequence is a headache. The official statement is a bit vague. Please refer to the official explanation:




Because the script compilation sequence involves special folders, such as the plug-ins, Editor, Standard Assets, and other Standard resource folders mentioned above, the location of the script is very important. The following example illustrates the script compilation sequence in different folders:





In fact, if you are careful, you will find that if you create a folder hierarchy as shown in your project, after the project is compiled, some project files with the words Editor and firstpass are generated in the project folder. For example, according to the folder structure, open the project folder to see what the project file is like?





Next we will discuss in detail what these words mean? What are their relationships with the script compilation sequence?


1. First of all, from the perspective of the script language type, Unity3d supports three types of script languages and will be compiled into CLI DLL

If the project contains a C # script, Unity3d generates a project prefixed with Assembly-CSharp. The project whose name contains "vs" is used by Vistual Studio, which does not contain "vs" is generated for MonoDevelop.


Script Language in the project Project prefix Engineering suffix
C # Assembly-CSharp Csproj
UnityScript Assembly-UnityScript Unityproj
Boo Assembly-Boo Booproj

If all three scripts exist in the project, Unity will generate three prefix types of projects.


2. For each scripting language, place the script according to the location (in fact, part of it depends on the role of the script, such as the Editor extension script, which must be placed in the Editor folder ), unity generates a project with the suffix 4. The firstpass indicates that the script is compiled first, and the Editor indicates that the script is placed in the Editor folder.

In the preceding example, we obtain two sets of project files: Virtual Studio and MonoDevelop (the suffix package does not contain vs). For simplicity, we only analyze the vs project. The file list is as follows:


Assembly-CSharp-filepass-vs.csproj
Assembly-CSharp-Editor-filepass-vs.csproj
Assembly-CSharp-vs.csproj
Assembly-CSharp-Editor-vs.csproj


According to the official explanation, their compilation sequence is as follows:


(1) All the scripts in the Standard Assets, Pro Standard Assets, or Plugins folder will generate a Assembly-CSharp-filepass-vs.csproj file and compile it first;


(2) All the scripts in the Standard Assets/Editor, Pro Standard Assets/Editor or Plugins/Editor folder generate the Assembly-CSharp-Editor-filepass-vs.csproj project file and then compile;


(3) All scripts that are out of Assets/Editor and are not in (1) and (2) (generally, these scripts are self-written non-Editor extension scripts) will generate Assembly-CSharp-vs.csproj project files, compiled;


(4) All scripts in Assets/Editor generate a Assembly-CSharp-Editor-vs.csproj project file and are compiled.


The reason for establishing a project and compiling it in this order is also determined by the dependencies between the DLL.

So far, we can easily determine the script compilation sequence in the above example (in fact, I have written the sequence in the script file name)


More highlights: www.gopedu.com

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.