版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wengpanfeng/article/details/80092254
这是一个基础的问题,时间长了不用就有点遗忘,在此记录下来。
递归查找,最简单,效率最好
直接贴代码:
public List<GameObject> objList =new List<GameObject>();
public static void RecursiveAllObjs(Transform parent)//参数:根节点物体的transform
{
for (int i = 0; i < parent.childCount; i++) //childCount的数量包括不显示的物体
{
Transform temp = parent.GetChild(i);
objList.Add(temp .gameobject);
/*
do something you want
*/
if (temp.childCount > 0)
{
RecursiveAllObjs(temp);
}
}
}
欢迎指正!