#你好Unity3D #unity Get a detailed description of the game object

Source: Internet
Author: User

I think Unity's transform and gameobject are like two twin brothers, these two buddies are very good, I can find you directly, you can directly find me. I think a lot of people like to save Gameobject objects in the class. Resolves an issue in which gameobject.find () could not get inborn Activie = False.
private Gameobject root;

I think you had better not save Gameobject, but to save transform, because transform under the method is more than Gameobject, use frequency is much higher.

private Transform root;

In fact, I have a question in mind, why unity does not combine gameobject and transform into one object.

1.gameobject.find ()

get the game object directly from the name or path in the scene.
Gameobject root = Gameobject.find ("Gameobject");

I think if the game object is not the top, then the best way to use the path, because it is possible that your game object will have the name of the case, the path is separated by the "/" symbol.
Gameobject root = Gameobject.find ("Gameobject/cube");

Gameobject.find () is convenient to use, but it has a flaw as shown in the figure below, if your gameobject is born acive = False. Then you use Gameobject.find () is never able to get its object. If the object is not obtained, then the object on the body of the script AH component ah what is not acquired, become meaningless objects.

 

I've looked at a lot of information on this issue, and it ended up being fruitless. But I used another ingenious way to solve it. Or you can save all the game objects in memory in advance.

The Gameobject.find () method is used very often in games. But it also consumes performance, you can think of its principle is certainly also in a similar recursive form to do, then we should try to call the Gameobject.find () method Less, can get the game object, saved in memory, this is the best choice. In particular, do not go to the Find () game object in the Update method.

2. Transform.find ()
Remember the above I said with Gameobject can't get natural acive = False Game object, if you use Transform.find () can be very good to get, in addition Unity also provides a transform.findchind () method , this method will be abandoned by unity in the future, it is best not to use, with Transform.find () can be replaced.

in the following code, we first get the top-level object root. Then use Find () to find its child node "XXXX" object, regardless of whether the "XXXX" object is active = True can be directly found object.

C #

1 2 3 4 Gameobject root = Gameobject.                   Find ("Gameobject"); gameobject xxxx = root. Transform. Find ("xxxx").            Gameobject; Xxxx. SetActive (TRUE);

The Find () method can only go directly to the child nodes, if you want to find the Sun node, then you can use the "/" symbol to separate the hierarchical relationship, it is convenient to find. Similarly, whether the "XXXX" object is active = True, the object can be found directly. C #

1 Gameobject cube = root. Transform. Find ("Xxxx/cube"). Gameobject;

It is important to note that unity specifies that the parent node active = True and that the child node's active = True is sufficient to be fully displayed. The use of Transform.find () can be very convenient to get the game object, because of the game object, then its body of the script AH components ah what can be easily obtained.

but Transform.find () must ensure that the activity of your top-level Parent Object = True. For example, you made a scene with some maps that you pre-Activie = False in the scene, and you want to turn them all on at some point in the game setActive (true)

you can put the "map" node on an active = True gameobject, which is convenient to write in either the off or display code. If your map node is a top-level node, once it is inherently acive = False, then you will not be able to get its object, and you cannot set its properties.

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.