This article summarizes the methods for finding sub-objects in unity
First of all, here are three ways to find sub-objects:
A method for locating a sub-object of a fixed path, a method for finding a sub-object by its name depth, and a method for locating all sub-objects under the parent object.
First: A method for locating a sub-object of a fixed path
For known paths, you can find them directly using the Go.transform.FindChild method.
For example: In such a hierarchical path, we need to find the last plane object.
1 usingUnityengine;2 usingSystem.Collections;3 4 Public classFindchild:monobehaviour {5 6 //Use this for initialization7 voidStart () {8 9 }Ten One //Update is called once per frame A voidUpdate () { - if(Input.getmousebuttondown (1)) - { the //Find Object Methods -Gameobject go = Gameobject.find ("Cube"); - //Find sub-objects and convert the resulting objects into Gameobject -Gameobject objname= Go.transform.FindChild ("Sphere/cylinder/plane"). Gameobject; + -Debug.Log ("the name of the resulting sub-object is:"+objname.name); + } A } at}
Then the result of the execution:
==-------------------------------------------------------------------------------------------------------------
Second: The method of finding a sub-object by name depth
Note: There are two conditions that must be met to use this method: the first must have the name of the child object you are looking for, and the second must start from a parent object.
In the following code, check represents the name of the target sub-object you are looking for, starting with the parent object. such as return GetTransform (transform, "bone12");
The core code of the method:
And here's how to find it:
1Transform GetTransform (Transform check,stringname)2 {3Transform Forreturn =NULL;4 5 foreach(Transform tinchCheck. Getcomponentsinchildren<transform>())6 {7 if(T.name = =name)8 {9Debug.Log ("the name of the resulting sub-object is:"+t.name);TenForreturn =T; One returnT; A - } - the } - returnForreturn; -}
Look at the complete test code: Also use the previous example, for example, this time to find cylinder this object:
The Modified code:
1 usingUnityengine;2 usingSystem.Collections;3 4 Public classFindchild:monobehaviour {5 6 //Use this for initialization7 voidStart () {8 9 }Ten One //Update is called once per frame A voidUpdate () { - if(Input.getmousebuttondown (1)) - { the // //Find Object Methods -Gameobject go = Gameobject.find ("Cube"); - // //Find sub-objects and convert the resulting objects into Gameobject - //gameobject objname= go.transform.FindChild ("Sphere/cylinder/plane"). Gameobject; + - //Debug.Log ("Get the final sub-object name is:" + objname.name); + A atGetTransform (Go.transform,"Cylinder"); - - } - } - -Transform GetTransform (Transform check,stringname) in { -Transform Forreturn =NULL; to + foreach(Transform tinchCheck. Getcomponentsinchildren<transform>()) - { the if(T.name = =name) * { $Debug.Log ("the name of the resulting sub-object is:"+t.name);Panax NotoginsengForreturn =T; - returnT; the + } A the } + returnForreturn; - } $}
Test results:
-----------------------------------------------------------------------------------------------------
Third: Next we will acquire all the sub-objects under a parent object, and then destroy all the sub-objects beneath it.
Note: All sub-objects are sibling relationships, in the same layer.
Core approach:
1list<transform> LST =NewList<transform>();2 foreach(Transform Childinchtransform)3 {4 lst. ADD (child);5 Debug.Log (child.gameObject.name);6 }7 for(inti =0; I < LST. Count; i++)8 {9 Destroy (lst[i].gameobject);Ten}
The transform above is the transform of the parent object. Specific case code:
1 usingUnityengine;2 usingSystem.Collections;3 usingSystem.Collections.Generic;4 5 Public classFindchild:monobehaviour {6 7 //Use this for initialization8 voidStart () {9 Ten } One A //Update is called once per frame - voidUpdate () { - if(Input.getmousebuttondown (1)) the { - // //Find Object Methods -Gameobject go = Gameobject.find ("Cube"); -list<transform> LST =NewList<transform>(); + foreach(Transform Childinchgo.transform) - { + lst. ADD (child); A Debug.Log (child.gameObject.name); at } - for(inti =0; I < LST. Count; i++) - { -Debug.Log ("the objects destroyed are:"+lst[i].gameobject); - Destroy (lst[i].gameobject); - } in - } to } + - the}
The test results were completely destroyed:
The above is my summary of the three commonly used to find sub-object methods.
Unity Depth Find a sub-object and traverse all sub-object methods