roots | 将从这里开始搜索的对象数组。 |
Object[] 与该 search 数组以层次结构附着的对象数组。
收集基于各给定对象的层次结构中的所有对象。
这对于对整个 GameObject 层次结构及其所有组件进行线性化非常有用。
请注意,遍历不会包括层次结构内部引用的资源。例如,层次结构中拥有 MeshFilter 组件并不会导致引用的网格包含在结果列表中。
using UnityEngine; using UnityEditor;
public class CollectHierarchyExample : MonoBehaviour { void Start() { // Create two GameObjects GameObject parent = new GameObject(); GameObject child = new GameObject(); Object[] roots = new Object[] { parent };
// Name them parent.name = "Parent"; child.name = "Child";
// Make one a child of the other. child.transform.parent = parent.transform;
// Collect entire hierarchy Object[] result = EditorUtility.CollectDeepHierarchy(roots);
// Dump results. Will log four objects to the console; // two GameObjects ("Parent" and "Child") and two Transform // components (one belonging to "Parent" and one belonging to // "Child") foreach (Object obj in result) Debug.Log(obj); } }