[Go] about the order of Unity3d object and script instance invocation

Source: Internet
Author: User

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
    1. <span style="FONT-SIZE:18PX;" > void Start () {
    2. Print ("-----" + this.transform.name);
    3. }</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
  1. <span style="FONT-SIZE:18PX;" >public class Groupdata:monobehaviour
  2. {
  3. void Awake ()
  4. {
  5. Print ("--" + this.transform.name+ "-------Awake-----------------");
  6. }
  7. // Use this for initialization
  8. void Start ()
  9. {
  10. Print ("----start-" + this.transform.name);
  11. }
  12. //Update is called once per frame
  13. void Update ()
  14. {
  15. }
  16. void OnDestroy ()
  17. {
  18. Print ("*******************" + This.transform.name + "********destroy!");
  19. }
  20. }</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
  1. <span style="FONT-SIZE:18PX;" >// use the for initialization
  2. Voidstart ()
  3. {
  4. Print ("----start-" + This.transform.name + ", class:groupdata");
  5. }
  6. //Update is called once per frame
  7. void Update ()
  8. {
  9. }
  10. void OnDestroy ()
  11. {
  12. Print ("*******************" + This.transform.name + "********destroy!" + ", class:groupdata");
  13. }</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

Related Article

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.