T The HierarchyNodeTypeHandler.
Gets a HierarchyNodeTypeHandler instance from this hierarchy.
Use this method to retrieve a specific HierarchyNodeTypeHandler when you know the exact type at compile time.
| Parameter | Description |
|---|---|
| hierarchy | The Hierarchy to get the HierarchyNodeTypeHandler from. |
| node | The HierarchyNode to get the HierarchyNodeTypeHandler for. |
HierarchyNodeTypeHandler The HierarchyNodeTypeHandler.
Gets the HierarchyNodeTypeHandler instance for the specified node from this hierarchy.
Use this method to retrieve a specific HierarchyNodeTypeHandler instance from a specific HierarchyNode.
| Parameter | Description |
|---|---|
| hierarchyViewModel | The hierarchy view model. |
| node | The hierarchy node. |
HierarchyNodeTypeHandler The hierarchy node type handler.
Get the node type handler instance for the specified node from this hierarchy.
The following example adds context menu actions to the Hierarchy window to add or remove a specific component from a GameObject. The actions appear in the Hierarchy Samples submenu of the context menu. The example uses GetNodeTypeHandler to check whether each selected node is handled by a HierarchyGameObjectHandler and filters out nodes of other types.
The example requires a custom MonoBehaviour script called Enemy.cs.
To use this example:
Assets/Editor/CustomContextMenuAction. Scripts in an Editor folder can use the Hierarchy module API without additional setup. If you save the script outside of an Editor folder, you must enable the Hierarchy built-in module in the Package Manager window, which also adds the module to your Player builds.Enemy.cs script outside of an Editor folder, because MonoBehaviour scripts in an Editor folder can't be attached to GameObjects.Enemy component, or Remove Enemy component to remove it.using System.Collections.Generic; using Unity.Hierarchy; using Unity.Hierarchy.Editor; using UnityEditor; using UnityEngine; using UnityEngine.UIElements;
namespace Unity.HierarchySamples.Editor { class CustomContextMenuAction { [InitializeOnLoadMethod] static void Initialize() { HierarchyWindow.PopulateContextMenu += OnPopulateContextMenu; }
static void OnPopulateContextMenu(HierarchyWindow window, HierarchyView view, HierarchyViewItem item, DropdownMenu menu) { if (item == null) return;
if (view.Source.GetNodeTypeHandler<HierarchyGameObjectHandler>() == null) return;
List<GameObject> withEnemy = new List<GameObject>(); List<GameObject> withoutEnemy = new List<GameObject>();
foreach (HierarchyNode node in view.ViewModel.EnumerateNodesWithFlags(HierarchyNodeFlags.Selected)) { if (view.ViewModel.GetNodeTypeHandler(node) is not HierarchyGameObjectHandler handler) continue;
GameObject gameObject = handler.GetGameObject(node); if (gameObject.TryGetComponent<Enemy>(out _)) withEnemy.Add(gameObject); else withoutEnemy.Add(gameObject); }
if (withoutEnemy.Count > 0) { menu.AppendAction("Hierarchy Samples/Turn into Enemy", _ => { foreach (GameObject gameObject in withoutEnemy) Undo.AddComponent<Enemy>(gameObject); }); }
if (withEnemy.Count > 0) { menu.AppendAction("Hierarchy Samples/Remove Enemy component", _ => { foreach (GameObject gameObject in withEnemy) Undo.DestroyObjectImmediate(gameObject.GetComponent<Enemy>()); }); } } } }
The following example shows the Enemy component that the CustomContextMenuAction example uses.
using UnityEngine;
namespace Unity.HierarchySamples { public class Enemy : MonoBehaviour { // Marker component used by ChangeRowColor and CustomContextMenuAction samples. } }
| Parameter | Description |
|---|---|
| hierarchy | The Hierarchy to get the HierarchyNodeTypeHandler from. |
| nodeTypeName | The node type name to get the HierarchyNodeTypeHandler for. |
HierarchyNodeTypeHandler The HierarchyNodeTypeHandler.
Gets the HierarchyNodeTypeHandler instance for the specified node type name from this hierarchy.
Use this method to retrieve a specific HierarchyNodeTypeHandler instance from a node type name.