Unity3d get a detailed description of the game object

Source: Internet
Author: User

Original address: http://www.xuanyusong.com/archives/2768

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, 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. Especially in the Update method do not go to the Find () Game Object!!

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.

1234 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.

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.

Gameobject root = Gameobject.find ("Gameobject");

Gameobject map = root.transform.Find ("map"). Gameobject;

Map. SetActive (TRUE);

3. Unity also provides several ways to get the game objects, but I personally feel that the use of the frequency is not high, here is also a simple two sentences.

Gameobject.findgameobjectswithtag ("tag")
Gameobject.findwithtag ("tag")

Based on a tag to get the game object, return one or an array, I personally think that this two method is not useful, because since the need to use the tag so compared to this game object is necessarily a very special one, so I will put it in memory.

Object.findobjectoftype
Object.findobjectsoftype
Resources.findobjectsoftypeall

Returns an object based on a type, such as Gameobject, Texture, Animation, or even a generic script that you write yourself. It is convenient to find and can return one or an array. I think these methods are not really useful in the game, but it is very often used in the editor, such as you want to do a batch inspection of the scene of the tool, to find out if there is a particular type of object in the scene. Or look at the memory footprint to see if the current in-memory texture are not released. Wait a minute.

There is also a method, if you know the index of the object, you can also use the following method to get, the parameter is index.
Transform. Getchild (0)

Unity3d get a detailed description of the game object

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.