Unity获取Hierarchy中所有物体

void FindAllGameObject() {
        List roots = new List();
        for (int i = 0; i < SceneManager.sceneCount; i++)
        {
            var scene = SceneManager.GetSceneAt(i);
            if (!scene.isLoaded) continue;

            roots.AddRange(scene.GetRootGameObjects());
        }

        for (int i = 0; i < roots.Count; i++) {
            print(roots[i].name);
        }
    }

你可能感兴趣的:(Unity获取Hierarchy中所有物体)