包含此页的版本:
不含此页的版本:
使用上下文菜单事件,ContextualMenuManipulator和ContextualMenuPopulateEvent,以在用户执行某些作时(例如,当用户右键单击标签时)显示一组选项。
要启用上下文菜单,请将ContextualMenuManipulator设置为视觉元素实例化或派生自 C# 的可视化树的节点VisualElement类。您可以设置外观样式、定义行为并将其作为 UI 的一部分显示在屏幕上。更多信息
请参阅术语表.此纵器在右键鼠标向上事件或菜单键向上事件之后显示上下文菜单。这ContextualMenuManipulator纵器还添加了一个回调,该回调响应ContextualMenuPopulateEvent.
以下代码示例将上下文菜单添加到视觉元素。菜单只有一项。
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)
{
// ...
}
给出的回调ContextualMenuManipulator构造函数最后调用,以便子元素可以填充菜单。
机械手发送一个ContextualMenuPopulateEvent事件传播到目标元素层次结构。事件沿着传播路径移动:从可视化树 由轻量级节点组成的对象图,用于保存窗口或面板中的所有元素。它定义了使用 UI 工具包构建的每个 UI。
请参阅术语表到事件目标,然后将可视化树备份到根。沿着传播路径,具有ContextualMenuPopulateEvent事件可以添加、删除或更新上下文菜单中的项目。
当元素收到ContextualMenuPopulateEvent,它调用DropdownMenu.InsertAction()或DropdownMenu.AppendAction()将菜单项添加到上下文菜单。DropdownMenu.InsertAction()和DropdownMenu.AppendAction()将两个回调作为参数。当用户选择菜单中的项目时,将执行第一个回调。第二个回调在显示菜单之前执行,并检查菜单项是否已启用。
两个回调都会收到一个MenuAction作为参数。这MenuAction表示菜单项,并具有以下属性:
MenuAction.userData包括对与AppendAction()或InsertAction().MenuAction.eventInfo包括有关显示上下文菜单的事件的信息。用MenuAction.eventInfo在响应事件的作中。例如,您可以使用鼠标位置根据选定的上下文菜单项创建和放置对象。以下示例创建一个具有两个标签的自定义编辑器窗口,并为每个标签添加上下文菜单。该示例演示了如何添加、删除和更新上下文菜单。
使用任何模板创建 Unity 项目。
在Editor窗口中,创建一个名为ContextualMenuDemo.cs内容如下:
using 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);
});
}
}
若要实时查看示例,请从菜单中选择“窗口> UI 工具包”>“ContextualMenuDemo”。