下面是获取所有子物体的方法:
using UnityEngine;
using System.Collections;
public class FindObject : MonoBehaviour
{
public Transform[] objs;
void Start()
{
objs = GetComponentsInChildren<Transform>();
print(objs.Length);
foreach (Transform child in objs)
{
print(child.name);
}
}
}
但是,这样是不能获取隐藏的物体的,接下来需要修改 objs = GetComponentsInChildren<Transform>(); 为 objs = GetComponentsInChildren<Transform>(true);