menu | 用于向其中添加所有特定类型的 MenuItem 的上下文菜单。 |
targets | 菜单项的目标对象。 |
type | 收集上下文菜单项时要搜索的对象类型。 |
submenu | 用于按结果筛选的子菜单类别的可选名称。 |
将特定类型的所有 MenuItem 添加到场景视图上下文菜单。
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; } }