您的位置:首页 > 移动开发 > Unity3D

unity, iterate immediate children and iterate all children

2015-07-01 02:36 791 查看
遍历所有直接子节点(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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: