GetNodeTypeHandler
Gets a HierarchyNodeTypeHandler instance from this hierarchy.
Read time 3 minutesLast updated 12 days ago
Definition
- Type: Method
- Namespace: Unity.Hierarchy
- Assembly: UnityEngine.HierarchyModule
GetNodeTypeHandler<T>(Hierarchy)
Gets a HierarchyNodeTypeHandler instance from this hierarchy.
public static T GetNodeTypeHandler<T>(this Hierarchy hierarchy) where T : HierarchyNodeTypeHandler
Parameters
Returns
Type | Description |
|---|---|
| T | The HierarchyNodeTypeHandler. |
Remarks
Use this method to retrieve a specific HierarchyNodeTypeHandler when you know the exact type at compile time.
GetNodeTypeHandler(Hierarchy, HierarchyNode)
Gets the HierarchyNodeTypeHandler instance for the specified node from this hierarchy.
public static HierarchyNodeTypeHandler GetNodeTypeHandler(this Hierarchy hierarchy, in HierarchyNode node)
Parameters
The Hierarchy to get the HierarchyNodeTypeHandler from.
The HierarchyNode to get the HierarchyNodeTypeHandler for.
Returns
Type | Description |
|---|---|
| HierarchyNodeTypeHandler | The HierarchyNodeTypeHandler. |
Remarks
Use this method to retrieve a specific HierarchyNodeTypeHandler instance from a specific HierarchyNode.
GetNodeTypeHandler(HierarchyViewModel, HierarchyNode)
Get the node type handler instance for the specified node from this hierarchy.
public static HierarchyNodeTypeHandler GetNodeTypeHandler(this HierarchyViewModel hierarchyViewModel, in HierarchyNode node)
Parameters
Returns
Type | Description |
|---|---|
| HierarchyNodeTypeHandler | The hierarchy node type handler. |
Remarks
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 to check whether each selected node is handled by a and filters out nodes of other types.
GetNodeTypeHandlerHierarchyGameObjectHandlerThe example requires a custom MonoBehaviour script called .
Enemy.csTo use this example:
- Save the script in a folder called . Scripts in an
Assets/Editor/CustomContextMenuActionfolder can use the Hierarchy module API without additional setup. If you save the script outside of anEditorfolder, you must enable the Hierarchy built-in module in the Package Manager window, which also adds the module to your Player builds.Editor - Save the script outside of an
Enemy.csfolder, because MonoBehaviour scripts in anEditorfolder can't be attached to GameObjects.Editor - In the Hierarchy window, right-click one or more GameObjects and open the Hierarchy Samples submenu. Select Turn into Enemy to add the component, or Remove Enemy component to remove it.
Enemy
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 component that the CustomContextMenuAction example uses.
Enemyusing UnityEngine;namespace Unity.HierarchySamples{ public class Enemy : MonoBehaviour { // Marker component used by ChangeRowColor and CustomContextMenuAction samples. }}
GetNodeTypeHandler(Hierarchy, string)
Gets the HierarchyNodeTypeHandler instance for the specified node type name from this hierarchy.
public static HierarchyNodeTypeHandler GetNodeTypeHandler(this Hierarchy hierarchy, string nodeTypeName)
Parameters
The Hierarchy to get the HierarchyNodeTypeHandler from.
The node type name to get the HierarchyNodeTypeHandler for.
Returns
Type | Description |
|---|---|
| HierarchyNodeTypeHandler | The HierarchyNodeTypeHandler. |
Remarks
Use this method to retrieve a specific HierarchyNodeTypeHandler instance from a node type name.