Version: Unity 6.6 (6000.6)
LanguageEnglish
  • C#

HierarchyWindow.PopulateContextMenu

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

Description

Raised when a right click is handled on a node or on the background of the HierarchyView.

This callback receives the HierarchyViewItem to create the context menu for and the DropdownMenu to populate. If the user right-clicks in empty space, the callback receives null for the view item.

The following example creates a context menu item that collapses all nodes in the Hierarchy window except the paths to the selected items. The action appears in the Hierarchy Samples submenu of the context menu. It uses PopulateContextMenu to add the context menu action.

using System;
using Unity.Hierarchy;
using Unity.Hierarchy.Editor;
using UnityEditor;
using UnityEngine.UIElements;

namespace Unity.HierarchySamples.Editor { class CollapseOthers { [InitializeOnLoadMethod] static void Initialize() { HierarchyWindow.PopulateContextMenu += OnPopulateContextMenu; }

static void OnPopulateContextMenu(HierarchyWindow window, HierarchyView view, HierarchyViewItem item, DropdownMenu menu) { menu.AppendAction("Hierarchy Samples/Collapse Others", a => { // Capture selected and expanded nodes. int selectedAndExpandedCount = view.ViewModel.HasFlagsCount(HierarchyNodeFlags.Selected | HierarchyNodeFlags.Expanded); Span<HierarchyNode> expandedAndSelectedNodes = selectedAndExpandedCount < 16 ? stackalloc HierarchyNode[selectedAndExpandedCount] : new HierarchyNode[selectedAndExpandedCount]; view.ViewModel.GetNodesWithFlags(HierarchyNodeFlags.Selected | HierarchyNodeFlags.Expanded, expandedAndSelectedNodes);

// Create a flags change scope. This is an optimization to avoid triggering an update if no flags // actually changed after the operation. using (_ = new HierarchyViewModelFlagsChangeScope(view.ViewModel)) { // Collapse all nodes. view.ViewModel.ClearFlags(HierarchyNodeFlags.Expanded);

// Expand parents of all selected nodes. foreach (ref readonly var node in view.ViewModel.EnumerateNodesWithFlags(HierarchyNodeFlags.Selected)) { var parent = view.ViewModel.GetParent(node); if (parent == HierarchyNode.Null || parent == view.Source.Root) continue; view.ViewModel.SetFlagsRecursive(parent, HierarchyNodeFlags.Expanded, HierarchyTraversalDirection.Parents); }

// Expand all nodes that were previously expanded and selected. view.Expand(expandedAndSelectedNodes); }

}, _ => item == null || (view.ViewModel.HasFlagsCount(HierarchyNodeFlags.Selected) == 0) ? DropdownMenuAction.Status.Disabled : DropdownMenuAction.Status.Normal); } } }