GameObject.Find (), Transform.Find find games objects
1. pre-conditions
Unity commonly used to find objects, not hidden, hidden, various methods of performance high and low, there are a variety of use conditions.
In this analysis of the performance and condition of the search. The main problem encountered when developing is to find hidden objects.
There is no perfect method to find, only to find the most appropriate method
Finally comes the test codes
2. Related API
GameObject.Find
Transform.Find
GameObject.FindWithTag
GameObject.FindGameObjectsWithTag
Resources.FindObjectsOfTypeAll
2.1 GameObject.Find
Find game objects by name or path.
GameObject.Find("GameObject"); GameObject.Find("GameObject/ChildGameObject);
Use:
1. not find hidden objects
hidden object search path includes a parent node of any hidden (active = false)
2. If the search is not the top, wise use of Pathfinder, the Pathfinder is a double-edged sword
Advantage 1: Find a problem to solve the same name that may arise.
Advantage 2: If there is a complete path, reduces the seek range to minimize seek time.
Disadvantages: path or restructuring, easily affect the Finder, and needs to be repositioned to find the path.
3. If any path to find a parent in active = false, the object will not look.
4. Easy to use inefficient but
this find is equivalent recursive traversal to find, easy to use but the efficiency is worrying though, is recommended () function to find objects and save the references Start, avoid dynamic look in the Update () in.
2.2 Transform.Find
1. You can find hidden objects
2. Support Pathfinder
3. Look for hidden object premise is the root transform is located must be visible, that is active = true
Root = GameObject.Find GameObject ( " root " ); root.SetActive ( false ); // root node is null // always lookup fails root.transform.Find ( " root / AnyChildObjectName " );
The actual development:
under the actual development will feature the preform into a visible GameObject directory, the directory as GameObject to find the root, all of the following objects (hidden, unhidden) can be found.
You can "map" a node on MapRoot active = true, whether it is close or display the code easy to write. If your map node is the top node, then once it is born acive = false, then you will not be able to get it to the object, but can not set its properties.
GameObject root = GameObject.Find("MapRoot"); GameObject map = root.transform.Find("map").gameObject; map.SetActive(true);
2.3 Other Finding
GameObject.FindWithTag
GameObject.FindGameObjectsWithTag
use minimal, with no eggs
Resources.FindObjectsOfTypeAll
returns the list of objects of the specified type. Mainly used in the editor, eg. Memory leak detection, batch search function, etc.
3 actual test
Directory structure is as follows, green represents reality, red represents Hide
void the Start () { // GameObject.Find { // root GameObject.Find ( " A11 " ); // to true // parent node (node affected parent) GameObject.Find ( " A21 " ); // to false the GameObject .find ( " A22 " ); // to true // grandson node (parent node by the impact) GameObject.Find ( " A31 " ); // false GameObject.Find ( " A32 "); // to false GameObject.Find ( " A33 " ); // to false GameObject.Find ( " A34 " ); // true GameObject.Find ( " A34 " ); // find the true relative path GameObject.Find ( " / A34 " ); // false absolute Pathfinder GameObject.Find ( " / A11 / A22 / A34 " ); // to true GameObject.Find ( "A11 / A22 / A34"); // true GameObject.Find("/A22/A34"); // false GameObject.Find("A22/A34"); // true } // Transform.find { // 根节点 Transform A11 = transform.Find("A11"); // false // 父亲节点 Transform A21 = transform.Find("A21"); // true The A22 = transform.Find the Transform ( " the A22 " ); // to true // grandchild node of the Transform transform.Find = A31 ( " A31 " ); // to false the Transform transform.Find = A32 ( " A32 " ); // to false the Transform = transform.Find A33 ( " A33 " ); // false the Transform A34 = transform.Find ( " A34 " ); // false // use the Find directory relative to the root node Transform AA31 = transform.Find("A21/A31"); // true Transform AA32 = transform.Find("A21/A32"); // true Transform AA33 = transform.Find("A22/A33"); // true Transform AA34 = transform.Find("A22/A34"); // true // 包含根节点的查找目录 Transform AA311 = transform.Find("A11/A21/A31"); // false Transform AA321 = transform.Find("A11/A21/A32"); // false Transform AA331 = transform.Find("A11/A22/A33"); // false Transform AA341 = transform.Find("A11/A22/A34"); // false // 绝对路径 Transform AA3111 = transform.Find("/A11/A21/A31"); // false Transform AA3211 = transform.Find("/A11/A21/A32"); // false Transform AA3311 = transform.Find("/A11/A22/A33"); // false Transform AA3411 = transform.Find("/A11/A22/A34"); // false } }
4 even though hidden root node gameObject also to find ways
Even I found a hidden root node gameObject can lookup method.
Code Preview:
GameObject[] pAllObjects = (GameObject[])Resources.FindObjectsOfTypeAll(typeof(GameObject)); foreach (GameObject pObject in pAllObjects) { 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); }