Unity3D :上下文菜单事件
推荐:将NSDT场景编辑器加入你的3D工具链
3D工具集:NSDT简石数字孪生
上下文菜单事件
使用上下文菜单事件(ContextualMenuManipulator 和 ContextualMenuPopulateEvent
)可以在用户执行某些操作(例如当用户右键单击标签时)显示一组选项。
启用上下文菜单
若要启用上下文菜单,请将上下文菜单操纵器
附加到可视元素。此操纵器在鼠标右键向上事件或菜单键向上事件之后显示上下文菜单。操纵器还添加一个响应 ContextualMenuPopulateEvent
的回调。ContextualMenuManipulator
下面的代码示例将上下文菜单添加到可视元素。菜单只有一个项目。
void InstallManipulator(VisualElement element)
{
ContextualMenuManipulator m = new ContextualMenuManipulator(MyDelegate);
m.target = element;
}
void MyDelegate(ContextualMenuPopulateEvent event)
{
// Modify event.menu
event.menu.AppendAction("My action", DisplayProperties, DropdownMenu.MenuAction.AlwaysEnabled);
}
void DisplayProperties(DropdownMenu.MenuAction menuItem)
{
// ...
}
最后调用提供给构造函数的回调,以便子元素可以填充菜单。ContextualMenuManipulator
操纵器发送传播到目标元素层次结构的事件。事件沿传播路径移动:从可视化树的根到事件目标,然后将可视化树备份到根。沿着传播路径,具有事件回调的元素可以添加、删除或更新上下文菜单中的项。ContextualMenuPopulateEventContextualMenuPopulateEvent
响应用户选择
当一个元素收到一个 时,它会调用 [](../ScriptReference/UIElements.DropdownMenu.InsertAction] 或 DropdownMenu.AppendAction(.html)
将菜单项添加到上下文菜单中。 [](../ScriptReference/UIElements.DropdownMenu.InsertAction] 和 DropdownMenu.AppendAction(.html)
将两个回调作为参数。当用户选择菜单中的项时,将执行第一个回调。第二个回调在显示菜单之前执行,并检查菜单项是否已启用。ContextualMenuPopulateEventDropdownMenu.InsertAction()DropdownMenu.InsertAction()
两个回调都接收 a 作为参数。表示菜单项,并具有以下属性:MenuActionMenuAction
MenuAction.userData
包括对与 或 一起使用的用户数据的引用。AppendAction()InsertAction()
MenuAction.eventInfo
包括有关显示上下文菜单的事件的信息。在响应事件的动作中使用。例如,可以使用鼠标位置基于选定的上下文菜单项创建和放置对象。MenuAction.eventInfo
示例
下面的示例创建一个具有两个标签的自定义编辑器窗口,并为每个标签添加上下文菜单。该示例演示如何添加、删除和更新上下文菜单。
- 使用任何模板创建 Unity 项目。
- 在“项目”窗口中,创建一个名为 的文件夹。
Editor
在窗口中,创建一个名为的 C# 脚本,并将其内容替换为以下内容:EditorContextualMenuDemo
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);
});
}
}
4. 若要实时查看示例,请从菜单中选择“窗口> UI 工具包”>“上下文菜单演示”。
由3D建模学习工作室整理翻译,转载请注明出处!