当层级视图中的对象或对象组发生更改时引发的事件。
Actions that trigger this event include creating, renaming, reparenting, or destroying objects in the current hierarchy, as well as loading, unloading, renaming, or reordering loaded Scenes. Note that the event is not raised immediately in response to these actions, but rather during the next update of the editor application.
对已设置 HideFlags.HideInHierarchy 的对象执行的操作不会引发此事件,但更改 Object.hideFlags 会引发此事件。
以下示例脚本会在层级视图发生更改时将对象数量记录到控制台。将该数据复制到名为 HierarchyMonitor.cs 的文件中,然后将文件放在名为 Editor 的文件夹中。
using System.Linq; using UnityEditor; using UnityEngine;
[InitializeOnLoadAttribute] public static class HierarchyMonitor { static HierarchyMonitor() { EditorApplication.hierarchyChanged += OnHierarchyChanged; }
static void OnHierarchyChanged() { var all = Resources.FindObjectsOfTypeAll(typeof(GameObject)); var numberVisible = all.Where(obj => (obj.hideFlags & HideFlags.HideInHierarchy) != HideFlags.HideInHierarchy).Count(); Debug.LogFormat("There are currently {0} GameObjects visible in the hierarchy.", numberVisible); } }