unity, iterate immediate children and iterate all children

遍历所有直接子节点(immediate children):

foreach (Transform child in transform)
{
    // do whatever you want with child transform object here
}

int childCount = transform.childCount;

for (int i=0; i<childCount; i++) {
    Transform child=transform.GetChild (i);
}

注:transform.childCount返回的是直接子节点个数。

遍历所有子节点(包括根节点本身及所有子孙节点(grandchildren)):

Transform[] allChildren = GetComponentsInChildren<Transform>();
foreach (Transform child in allChildren) {
    // do whatever with child transform here
}

参考:http://answers.unity3d.com/questions/10417/how-can-i-access-the-children-of-a-transform.html

原文地址:https://www.cnblogs.com/wantnon/p/4612222.html