Version: Unity 6.6 (6000.6)
LanguageEnglish
  • C#

HierarchyExtensions.GetNodeTypeHandler

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public static T GetNodeTypeHandler(Hierarchy hierarchy);

Returns

T The HierarchyNodeTypeHandler.

Description

Gets a HierarchyNodeTypeHandler instance from this hierarchy.

Use this method to retrieve a specific HierarchyNodeTypeHandler when you know the exact type at compile time.


Declaration

public static HierarchyNodeTypeHandler GetNodeTypeHandler(Hierarchy hierarchy, ref HierarchyNode node);

Parameters

Parameter Description
hierarchy The Hierarchy to get the HierarchyNodeTypeHandler from.
node The HierarchyNode to get the HierarchyNodeTypeHandler for.

Returns

HierarchyNodeTypeHandler The HierarchyNodeTypeHandler.

Description

Gets the HierarchyNodeTypeHandler instance for the specified node from this hierarchy.

Use this method to retrieve a specific HierarchyNodeTypeHandler instance from a specific HierarchyNode.


Declaration

public static HierarchyNodeTypeHandler GetNodeTypeHandler(HierarchyViewModel hierarchyViewModel, ref HierarchyNode node);

Parameters

Parameter Description
hierarchyViewModel The hierarchy view model.
node The hierarchy node.

Returns

HierarchyNodeTypeHandler The hierarchy node type handler.

Description

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:

  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. } }

Declaration

public static HierarchyNodeTypeHandler GetNodeTypeHandler(Hierarchy hierarchy, string nodeTypeName);

Parameters

Parameter Description
hierarchy The Hierarchy to get the HierarchyNodeTypeHandler from.
nodeTypeName The node type name to get the HierarchyNodeTypeHandler for.

Returns

HierarchyNodeTypeHandler The HierarchyNodeTypeHandler.

Description

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.