Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


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

hierarchy

Returns

Type

Description

TThe 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

Returns

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

hierarchyViewModel

The hierarchy view model.

The hierarchy node.

Returns

Type

Description

HierarchyNodeTypeHandlerThe 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
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:
  1. Save the script in a folder called
    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.
  2. Save the
    Enemy.cs
    script outside of an
    Editor
    folder, because MonoBehaviour scripts in an
    Editor
    folder can't be attached to GameObjects.
  3. In the Hierarchy window, right-click one or more GameObjects and open the Hierarchy Samples submenu. Select Turn into Enemy to add the
    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. }}

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

hierarchy

The Hierarchy to get the HierarchyNodeTypeHandler from.

nodeTypeName

The node type name to get the HierarchyNodeTypeHandler for.

Returns

Remarks

Use this method to retrieve a specific HierarchyNodeTypeHandler instance from a node type name.