(2) traversing descendant objects
This article will record the basics of traversing sub-objects of an object or traversing sub-objects and grandchild objects, or filtering traversal
(This method traverses child objects, does not involve grandchild objects)
foreach (Transform child in transform)
{
Debug.Log("sub-object name:"+child.name);
}
for (int i = 0; i < transform.childCount; i++)
{
var child = transform.GetChild(i);
Debug.Log("sub-object name:"+child.name);
}
(This method can iterate through all child and grandchild objects)
for (int i = 0; i < GetComponentsInChildren<Transform>(true).Length; i++)
{
Debug.Log("Sub-object name:"+GetComponentsInChildren<Transform>(true)[i].name);
}
foreach(Transform child in GetComponentsInChildren<Transform>(true))
{
Debug.Log("sub-object name:"+child.name);
}
(This method can obtain objects with specific properties in descendant objects)
for (int i = 0; i < GetComponentsInChildren<Button>(true).Length; i++)
{
Debug.Log("Sub-object name:"+GetComponentsInChildren<Button>(true)[i].name);
}
Note: Output all Buttons in descendants
foreach(Transform child in GetComponentsInChildren<Text>(true))
{
Debug.Log("sub-object name:"+child.name);
}
Note: Output all the Text in the descendant object
Nothing to push. . .
This record is used, and the blogger's ability is limited. If there is any error in the text, you are welcome to comment on the accusation.
QQ exchange group: 806091680 (Chinar)
This group is created by CSDN blogger Chinar, recommend it! I am also in the group!
This article belongs to the original article, reproduced by the famous author source and top! ! ! ! !
Unity3D Removes the child object under a component (list update) When updating the ScrollView list content, you usually need to empty the list, read data, and the empty list is actually destroying the...
Regarding the dissolution of the father-son relationship, first look at the official explanation code. The official gives a function to remove the father-son relationship. Use the officially given fun...
In real game scenes that can be played, many scripts are executed on uncertain game objects, so they will consider writing scripts on parent or child objects. At this time, you may need to find game o...
Preface In the process of using unity3D for scene design and object control, multi-layer object nesting is a structure that is often used. In multi-level objects, objects at each level may have child ...
var arr = [18,20,26]; var obj = {name:"xiaohong",sex:"f",age:"18"}; for Can be used for traversing arrays for in/for of Traversing Note: for...of is suitable for traversi...
Irregular: Regular (json array): You can also use this: Traversing the json object: There are the following json objects: Var obj ={"name": "Feng Juan", "password": "...