- Gameobjectfindtransformfind Finding Game objects
- Front-facing conditions
- Related APIs
- 1 Gameobjectfind
- 2 Transformfind
- 3 other Searches
- Actual test
- A way to find out even if you hide the root node gameobject
Gameobject.find (), Transform.find find Game object 1. Preconditions
Unity is commonly used to find objects, non-hidden, hidden, various methods of performance are high and low, and the use of a variety of constraints.
The performance and conditions for the lookup are analyzed here. The main problem with development is finding hidden objects.
There is no perfect way to find, only the most appropriate way to find
Finally comes with the test code
2. Related APIs
Gameobject.find
Transform.find
Gameobject.findwithtag
Gameobject.findgameobjectswithtag
Resources.findobjectsoftypeall
2.1 Gameobject.find
Find the game object by name or path.
GameObject.Find("GameObject");GameObject.Find("GameObject/ChildGameObject);
Usage Specification:
1. Unable to find hidden objects
Hidden objects include a lookup path for any parent node hidden (active=false)
2. If the search is not on the top, it is recommended to use path lookup reasonably, and the path lookup is a double-edged sword
Advantage 1: resolve the duplicate name problems that may occur in the lookup.
Advantage 2: If there is a complete path, reduce the look-up range and reduce the lookup time.
disadvantage: after the path or structure adjustment, it is easy to affect the search in the program, need to relocate the lookup path.
3. If any one of the parent nodes in the path lookup is Active=false, the object will not be found.
4. Easy to use but inefficient
This lookup is equivalent to recursive traversal lookups, which are easy to use but inefficient, and it is recommended that you find the object in the start () function and save the reference without dynamically locating it in Update ().
2.2 Transform.find
1. Can find hidden objects
2. Support Path Lookup
3. The prerequisite for finding hidden objects is that the root node where the transform resides must be visible, that is, active=true
GameObject root = GameObject.Find("root");root.SetActive(false); // 根节点为空// 总是查找失败root.transform.Find("root/AnyChildObjectName");
Actual development:
In the actual development, the functional preform will be placed in a visible gameobject directory, the Gameobject directory as a lookup root node, all the following objects (hidden, not hidden) can be found.
You can put the "map" node on an active = True maproot, 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("MapRoot");GameObject map = root.transform.Find("map").gameObject; map.SetActive(true);
2.3 Other Searches
Gameobject.findwithtag
Gameobject.findgameobjectswithtag
Rarely used, and no egg
Resources.findobjectsoftypeall
Returns a list of objects of the specified type. Mainly used in the editor, eg. Detects memory leaks, functions for batch lookups, and more
3 Actual test
The directory structure is as follows, green stands for reality, red represents hidden
void Start () {//Gameobject.find{//root nodeGameobject.find ("A11");//True //parent node (affected by parent node)Gameobject.find ("A21");//FalseGameobject.find ("A22");//True //Grandson node (affected by parent node)Gameobject.find ("A31");//FalseGameobject.find ("A32");//FalseGameobject.find ("A33");//FalseGameobject.find ("A34");//TrueGameobject.find ("A34");//True relative path lookupGameobject.find ("/a34");//False absolute path lookupGameobject.find ("/a11/a22/a34");//TrueGameobject.find ("A11/a22/a34");//TrueGameobject.find ("/a22/a34");//FalseGameobject.find ("A22/a34");//True}//Transform.find{//root nodeTransform A11 =Transform. Find ("A11");//False //Father nodeTransform A21 =Transform. Find ("A21");//TrueTransform A22 =Transform. Find ("A22");//True //Grandson NodeTransform A31 =Transform. Find ("A31");//FalseTransform A32 =Transform. Find ("A32");//FalseTransform A33 =Transform. Find ("A33");//FalseTransform A34 =Transform. Find ("A34");//False //Use a lookup directory relative to the root nodeTransform AA31 =Transform. Find ("A21/a31");//TrueTransform AA32 =Transform. Find ("A21/a32");//TrueTransform AA33 =Transform. Find ("A22/a33");//TrueTransform AA34 =Transform. Find ("A22/a34");//True //Find directory containing the root nodeTransform AA311 =Transform. Find ("A11/a21/a31");//FalseTransform AA321 =Transform. Find ("A11/a21/a32");//FalseTransform AA331 =Transform. Find ("A11/a22/a33");//FalseTransform AA341 =Transform. Find ("A11/a22/a34");//False //absolute pathTransform AA3111 =Transform. Find ("/a11/a21/a31");//FalseTransform AA3211 =Transform. Find ("/a11/a21/a32");//FalseTransform AA3311 =Transform. Find ("/a11/a22/a33");//FalseTransform AA3411 =Transform. Find ("/a11/a22/a34");//False}}
4 How to find even if you hide the root node gameobject
Found a way to find a gameobject even if the root node is hidden.
Http://answers.unity3d.com/questions/52560/gameobjectfind-work-on-inactive-objects.html
Code preview:
Gameobject[] pallobjects = (gameobject[]) Resources. Findobjectsoftypeall(typeof (Gameobject));foreach (Gameobject pobjectinchPallobjects) {if (pobject. Transform. Parent! = null) {Continue;} if (pobject. Hideflags= = Hideflags. Noteditable|| Pobject. Hideflags= = Hideflags. Hideanddontsave) {Continue;} if (Application. Iseditor) {String Sassetpath = Assetdatabase. Getassetpath(Pobject. Transform. Root. Gameobject);if (!string. IsNullOrEmpty(Sassetpath)) {Continue;}} Debug. Log(Pobject. Name);}
Reference article: Glaze Unity Get a detailed description of the game object
http://www.xuanyusong.com/archives/2768
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Unity game Development Four" Gameobject.find (), Transform.find find hidden objects