Unity3D_01_ Various methods for finding GameObject

1.GameObject.Find():

Find the game object whose activie in the Hierarchy panel is not false; the 
path is written as the official example: 
public class ExampleClass : MonoBehaviour { 
public GameObject hand; 
void Example() { 
hand = GameObject.Find(“Hand”); 
hand = GameObject.Find ("/Hand"); 
hand = GameObject.Find("/Monster/Arm/Hand"); 
hand = GameObject.Find("Monster/Arm/Hand"); 

}

Note: 
GameObject.Find() is very convenient to use, that is, if your GameObject is born with acive = false. Then you can never get its object with GameObject.Find(). If the object cannot be obtained, then the script, components, etc. on the object cannot be obtained, and it becomes a meaningless object. 
The GameObject.Find() method is frequently used in games. But it also consumes a lot of performance. You can think that its principle must be done in a form similar to recursion, then we should call the GameObject.Find() method as little as possible, and save the acquired game object in the In memory, this is a better choice. Especially in the Update method don't go to Find() the game object! !

2.Transform.Find()

The official explanation is to get a child object by name (Finds a child by name and returns it), this method can get the hidden (inactive) GameObject, 
you can get the parent object first (active must be true), and then by looking for the child Transform.Find()

1 GameObject root = GameObject.Find("GameObject");      
2 
3 GameObject xxxx = root.transform.Find("xxxx").gameObject;
4 
5 xxxx.SetActive(true);

Note: 
Remember that I said above that you can't use GameObject to get the game object that is born with acive = false. If you use Transform.Find(), you can get it very well. In addition, Unity also provides a Transform.FindChind() method, This method will be abandoned by Unity in the future, so you'd better not use it and use Transform.Find() instead. 
In the following code, we first get the top-level object root. Then use Find() to find the object of its child node "xxxx", whether the "xxxx" object is active = true or not, the object can be found directly.

The Find() method can only find the child nodes directly. If you want to find the grandchild nodes, you can use the "/" symbol to separate the hierarchical relationship, which is very convenient to find. Similarly, whether the "xxxx" object is active = true or not, the object can be found directly.

   GameObject cube = root.transform.Find("xxxx/Cube").gameObject;

In addition, it should be noted that Unity stipulates that, for example, the parent node active = true and the child node's active = true can be fully displayed. Using Transform.Find() can easily get the game object, because with the game object, the scripts, components, etc. on it can be easily obtained.

But Transform.Find() must ensure that your top-level parent's activity = true.

For example, you made a scene with some maps and you pre-activeie = false in the scene, you want to turn them all on at some point in the game setActive(true)

You can put the "map" node on a GameObject with active = true, and it's convenient to write in code whether it is closed or displayed. If your map node is the top-level node, once it is born with acive = false, you will not be able to get its object, let alone set its properties.

1 GameObject root = GameObject.Find(“GameObject”);        
2 
3 GameObject map =  root.transform.Find(“map”).gameObject;       
4 
5 map.SetActive(true);

3. Unity also provides several methods to get game objects

GameObject.FindGameObjectsWithTag(“tag”)
GameObject.FindWithTag(“tag”)

Get the game object based on a tag and return an array or an array. I personally think these two methods are useless, because since the tag needs to be used, this game object must be a very special one, so I will keep it there in memory.

Object.FindObjectOfType 
Object.FindObjectsOfType 
Resources.FindObjectsOfTypeAll

Returns Object according to a type, such as GameObject, Texture, Animation, or even a script you wrote yourself. It's easy to find and can return one or an array. I think these methods are actually useless in the game, but they are used very frequently in the editor. For example, if you want to check the scene in batches, you can find out whether a special type of object is used in the scene. Or check the memory usage to see if those Textures in the current memory have not been released. etc.

There is also a method. If you know the index of the self object, you can also use the following method to get it. The parameter is the index of the index.

transform.GetChild(index);index为索引,0,1,2,3,4代表第几个child.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325028762&siteId=291194637