使用递归遍历方式查找是一个快捷的方式
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ForeachTransform : MonoBehaviour
{
//所有对象的Transfom
public List<Transform> obj_transforms = new List<Transform>();
private void Awake()
{
GetAllTransform(transform);
DetachAllChildren(transform);
}
//获取所有子对象
public void GetAllTransform(Transform parent)
{
for (int i = 0; i < parent.childCount; i++)
{
obj_transforms.Add(parent.GetChild(i));
if (parent.GetChild(i).childCount > 0)
GetAllTransform(parent.GetChild(i));
}
}
//拆分所有子对象
public void DetachAllChildren(Transform parent)
{
for (int i = 0; i < parent.childCount; i++)
{
if (parent.GetChild(i).childCount > 0)
{
DetachAllChildren(parent.GetChild(i));
parent.GetChild(i).DetachChildren();
}
}
}
}