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

posted on 2015-07-01 02:36  wantnon  阅读(293)  评论(0编辑  收藏  举报

导航