Contextual menu events
Events that occur when the user interacts with a contextual menu.
読み終わるまでの所要時間 3 分最終更新 1ヶ月前
Use the contextual menu events, and , to display a set of choices when a user performs certain actions, such as when a user right-clicks a label.
ContextualMenuManipulatorContextualMenuPopulateEventEnable contextual menus
To enable contextual menus, attach the to a visual element. This manipulator displays a contextual menu after either a right button mouse up event or a menu key up event. The manipulator also adds a callback that responds to a .
ContextualMenuManipulatorContextualMenuManipulatorContextualMenuPopulateEventThe following code example adds a contextual menu to a visual element. The menu has only one item.
void InstallManipulator(VisualElement element){ ContextualMenuManipulator m = new ContextualMenuManipulator(MyDelegate); m.target = element;}void MyDelegate(ContextualMenuPopulateEvent evt){ // Modify evt.menu evt.menu.AppendAction("My action", DisplayProperties, DropdownMenuMenuAction.AlwaysEnabled);}void DisplayProperties(DropdownMenu.MenuAction menuItem){ // ...}
The callback given to the constructor is invoked last so child elements can populate the menu.
ContextualMenuManipulatorThe manipulator sends a event propagated to the target element hierarchy. The event moves along the propagation path: from the root of the visual tree to the event target, and then back up the visual tree to the root. Along the propagation path, the elements with a callback for the event can add, remove, or update items in the contextual menu.
ContextualMenuPopulateEventContextualMenuPopulateEventRespond to the user selection
When an element receives a , it calls either or to add menu items to the contextual menu. and take two callbacks as parameters. The first callback executes when the user selects the item in the menu. The second callback executes before it displays the menu and also checks whether the menu item is enabled.
ContextualMenuPopulateEventDropdownMenu.InsertAction()DropdownMenu.AppendAction()DropdownMenu.InsertAction()DropdownMenu.AppendAction()Both callbacks receive a as a parameter. The represents the menu item and has the following properties:
MenuActionMenuAction- includes a reference to user data used with
MenuAction.userDataorAppendAction().InsertAction() - includes information about the event that displays the contextual menu. Use
MenuAction.eventInfoin the action that responds to the event. For example, you can use the mouse position to create and place an object based on the selected contextual menu item.MenuAction.eventInfo
Examples
The following example creates a custom Editor window with two labels and adds contextual menus for each label. The example demonstrates how to add, remove, and update a contextual menu.
-
Create a Unity project with any template.
-
In the Project window, create a folder named.
Editor -
In thewindow, create a C# script named
Editorwith the following content:ContextualMenuDemo.csusing UnityEditor;using UnityEngine;using UnityEngine.UIElements;public class ContextualMenuDemo : EditorWindow{ [MenuItem("Window/ContextualMenuDemo")] public static void ShowExample() { ContextualMenuDemo wnd = GetWindow<ContextualMenuDemo>(); wnd.titleContent = new GUIContent("ContextualMenuDemo"); } public void CreateGUI() { VisualElement root = rootVisualElement; VisualElement label = new Label("Right click me!"); root.Add(label); AddANewContextMenu(label); InsertIntoAnExistingMenu(label); VisualElement second = new Label("Click me also!"); root.Add(second); AddANewContextMenu(second); InsertIntoAnExistingMenu(second); // Override the default behavior by clearing the menu. ReplaceContextMenu(second); } void AddANewContextMenu(VisualElement element) { // The manipulator handles the right click and sends a ContextualMenuPopulateEvent to the target element. // The callback argument passed to the constructor is automatically registered on the target element. element.AddManipulator(new ContextualMenuManipulator((evt) => { evt.menu.AppendAction("First menu item", (x) => Debug.Log("First!!!!"), DropdownMenuAction.AlwaysEnabled); evt.menu.AppendAction("Second menu item", (x) => Debug.Log("Second!!!!"), DropdownMenuAction.AlwaysEnabled); })); } void InsertIntoAnExistingMenu(VisualElement element) { element.RegisterCallback<ContextualMenuPopulateEvent>((evt) => { evt.menu.AppendSeparator(); evt.menu.AppendAction("Another action", (x) => Debug.Log("Another Action!!!!"), DropdownMenuAction.AlwaysEnabled); }); } void ReplaceContextMenu(VisualElement element) { element.RegisterCallback<ContextualMenuPopulateEvent>((evt) => { evt.menu.ClearItems(); evt.menu.AppendAction("The only action", (x) => Debug.Log("The only action!"), DropdownMenuAction.AlwaysEnabled); }); }} -
To see the example live, from the menu, select Window > UI Toolkit > ContextualMenuDemo.