ContextMenuUtility.AddMenuItemsForType
Declaration
public static void AddMenuItemsForType(UIElements.DropdownMenu menu, IEnumerable<T> targets);public static void AddMenuItemsForType(UIElements.DropdownMenu menu, Type type, IEnumerable<Object> targets, string submenu);Параметры
| Параметр | Описание |
|---|---|
| меню | Контекстное меню для добавления всех MenuItem для определенного типа. |
| целевые показатели | Целевые объекты для пунктов меню. |
| тип | Тип объекта для поиска при сборе элементов контекстного меню. |
| подменю | Необязательное имя категории подменю для фильтрации результатов. |
Описание
Добавление всех MenuItem определенного типа в контекстное меню представления Scene.
using UnityEditor; using UnityEditor.Actions; using UnityEditor.Overlays; using UnityEngine; using UnityEngine.UIElements; [Overlay(typeof(SceneView), "Context Menu Example", defaultDisplay = true)] class ContextMenuExample : Overlay { [MenuItem("CONTEXT/Transform/Print Selected")] static void Init(MenuCommand cmd) { Debug.Log($"Selected transforms: {cmd.context}"); } static void PopulateMenuItems(ContextualMenuPopulateEvent evt) { ContextMenuUtility.AddMenuItemsForType(evt.menu, typeof(Transform), Selection.transforms); } public override VisualElement CreatePanelContent() { var root = new VisualElement(); ContextualMenuManipulator manipulator = new ContextualMenuManipulator(PopulateMenuItems); manipulator.target = root; const string text = "Context click here to show the context menu items for the selected Transform components."; root.Add(new Label(text)); root.style.width = 256; root.style.height = 128; return root; } }