컨텍스트 메뉴 이벤트 ContextualMenuManipulator
와 ContextualMenuPopulateEvent
를 사용하여 사용자가 레이블을 오른쪽 클릭하는 등의 특정 작업을 수행할 때 일련의 선택 항목을 표시합니다.
컨텍스트 메뉴를 활성화하려면 시각적 요소에 ContextualMenuManipulator
를 첨부합니다.이 매니퓰레이터는 마우스 오른쪽 버튼 눌림 해제 이벤트 또는 메뉴 키 눌림 해제 이벤트 후에 컨텍스트 메뉴를 표시합니다.ContextualMenuManipulator
매니퓰레이터는 또한 ContextualMenuPopulateEvent
에 응답하는 콜백을 추가합니다.
다음 코드 예제는 시각적 요소에 컨텍스트 메뉴를 추가하는 예제입니다.메뉴에는 하나의 항목만 있습니다.
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
생성자에 주어진 콜백은 자식 요소가 메뉴를 채울 수 있도록 마지막에 호출됩니다.
매니퓰레이터는 타겟 요소 계층 구조에 전파된 ContextualMenuPopulateEvent
이벤트를 전송합니다.이벤트는 시각적 트리의 루트에서 이벤트 타겟으로 이동한 다음 시각적 트리를 다시 루트로 백업하는 전파 경로를 따라 이동합니다.전파 경로를 따라 ContextualMenuPopulateEvent
이벤트에 대한 콜백이 있는 요소는 컨텍스트 메뉴에서 항목을 추가, 제거 또는 업데이트할 수 있습니다.
요소가 ContextualMenuPopulateEvent
를 수신하면, [DropdownMenu.InsertAction()
](../ScriptReference/UIElements.DropdownMenu.InsertAction] 또는 DropdownMenu.AppendAction(.html)
을 호출하여 컨텍스트 메뉴에 메뉴 항목을 추가합니다.
[DropdownMenu.InsertAction()
](../ScriptReference/UIElements.DropdownMenu.InsertAction] 및 DropdownMenu.AppendAction(.html)
은 두 콜백을 파라미터로 받습니다.첫 번째 콜백은 사용자가 메뉴에서 항목을 선택하면 실행됩니다.두 번째 콜백은 메뉴를 표시하기 전에 실행되며 메뉴 항목이 활성화되어 있는지 여부도 확인합니다.
두 콜백 모두 파라미터로 MenuAction
을 받습니다.’MenuAction’은 메뉴 항목을 나타내며 다음과 같은 프로퍼티를 가집니다.
MenuAction.userData
에는 AppendAction()
또는 InsertAction()
과 함께 사용되는 사용자 데이터에 대한 레퍼런스가 포함됩니다.MenuAction.eventInfo
에는 컨텍스트 메뉴를 표시하는 이벤트에 대한 정보가 포함되어 있습니다.이벤트에 응답하는 액션에 MenuAction.eventInfo
를 사용하십시오.예를 들어 마우스 위치를 사용하여 선택한 컨텍스트 메뉴 항목에 따라 오브젝트를 생성하고 배치할 수 있습니다.다음 예제에서는 두 개의 레이블이 있는 커스텀 에디터 창을 만들고 각 레이블에 대한 컨텍스트 메뉴를 추가합니다.이 예는 컨텍스트 메뉴를 추가, 제거 및 업데이트하는 방법을 보여줍니다.
임의의 템플릿을 사용하여 Unity 프로젝트를 생성합니다.
프로젝트(Project) 창에서 Editor
라는 폴더를 만듭니다.
Editor
창에서 ContextualMenuDemo
라는 이름의 C# 스크립트를 생성하고 해당 콘텐츠를 다음과 같이 바꿉니다.
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);
});
}
}
예제를 실시간으로 보려면 메뉴에서 Window > UI Toolkit > ContextualMenuDemo를 선택합니다.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.