http://blog.csdn.net/liangzg_2011/article/details/8150844 about the order of Unity3d objects and script instance calls
Let's take a look at some small experiments that are interesting in unity instance order. There is a picture of the truth!!
Note: The above printed code statements are as follows:
[CSharp]View Plaincopy
- <span style="FONT-SIZE:18PX;" > void Start () {
- Print ("-----" + this.transform.name);
- }</span>
From the above print we can at least produce the results of the experiment!
Summary: Unity instantiates objects from the bottom-most depths of the tree and then up-to-down instances!
Let's experiment with a little experiment on the life cycle of the script:
About the life cycle of scripts there are a lot of resources on the web and in the government, and here the main experiment is about the order of instances of objects and the order in which they are attached to scripts!
Script code:
[CSharp]View Plaincopy
- <span style="FONT-SIZE:18PX;" >public class Groupdata:monobehaviour
- {
- void Awake ()
- {
- Print ("--" + this.transform.name+ "-------Awake-----------------");
- }
- // Use this for initialization
- void Start ()
- {
- Print ("----start-" + this.transform.name);
- }
- //Update is called once per frame
- void Update ()
- {
- }
- void OnDestroy ()
- {
- Print ("*******************" + This.transform.name + "********destroy!");
- }
- }</span>
Run:
State of initialization
As you can see from the above print, on the initial object and the invocation of the script, the instance of unity always loads the call from the bottom of the tree structure and then goes up and down!
Termination Status:
What a wonderful thing happened! When the object is destroyed, unity is the first object to destroy the upper layer, and then its sub-objects (from top to bottom) are destroyed!
Finally, we focus on the output of an object when it is associated with multiple scripts , and here we also focus on initializing and destroying two behaviors!
Script binding form for each object:
Three scripts are printed in such a way as:
[CSharp]View Plaincopy
- <span style="FONT-SIZE:18PX;" >// use the for initialization
- Voidstart ()
- {
- Print ("----start-" + This.transform.name + ", class:groupdata");
- }
- //Update is called once per frame
- void Update ()
- {
- }
- void OnDestroy ()
- {
- Print ("*******************" + This.transform.name + "********destroy!" + ", class:groupdata");
- }</span>
Let's take a look at the execution of the script first:
Let's swap the order of the next three scripts to see what the print is:
Summarize:
Initialization: Binding multiple scripts for the same object, the initialization load will always start from the bottom of the directory, and then step up!
Destroy: Perhaps because of the sibling directory, so the order of destruction according to the directory layer, from top to bottom execution!
Finally, let's look at the execution of multi-layered multiple scripts:
Summarize:
For the object view, it still starts from the bottom, and then to the script view of the single object, and starts at the bottom!
Personally, there's a little accident here!! haha ~ ~
The following are the destruction:
The order of destruction is not much different from the experimental results above us!
Suppose: We reduce the GROUP20 and Group210 scripts by one, just place a script to see how the script executes!
The following are destroyed:
We tried to write the code, and I guess it might be:
1. Find the script bindings for the object, record the maximum number of script binding layers
2. Find the number of distribution layers of an object and establish a tree level
3. For the loading process, start at the bottom of the script layer to traverse the tree layer of the entire object distribution
4. For the destruction process, it is in turn the object view of the tree layer started, still destroy the object's script, and the first to destroy the lowest parent node, and then up!
Just to record some questions in the work, there is the wrong place, welcome to correct, Exchange! Thanks for understanding!
[Go] about the order of Unity3d object and script instance invocation