다음 표준 컨트롤이 UIElement에 빌드됩니다.
컨텍스트 메뉴는 컨텍스트에 따라 다양한 선택 옵션과 액션을 사용자에게 표시합니다. 이 컨텍스트는 무엇이든 될 수 있지만, 대개는 현재 선택 항목입니다.
컨텍스트 메뉴를 활성화하려면 시각 요소에 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("Properties", DisplayProperties, DropdownMenu.MenuAction.AlwaysEnabled);
}
void DisplayProperties(DropdownMenu.MenuAction menuItem)
{
// ...
}
ContextualMenuManipulator
생성자에 부여된 콜백은 자식 요소가 메뉴를 채울 수 있도록 마지막에 호출됩니다.
내부적으로 조종기는 타겟 요소 계층 구조에 전파되는 ContextualMenuPopulateEvent
이벤트를 전파 경로(시각 트리의 루트에서 이벤트 타겟으로, 이후 다시 시각 트리를 따라 루트로 이동)를 따라 전송합니다. 전파 경로를 따라 ContextualMenuPopulateEvent
이벤트를 위한 콜백을 포함한 요소가 컨텍스트 메뉴에 항목을 추가하거나, 삭제하거나 수정할 수 있습니다.
ContextualMenuPopulateEvent
를 수신한 요소는 DropdownMenu.InsertAction()
이나 DropdownMenu.AppendAction()
을 호출하여 컨텍스트 메뉴에 메뉴 항목을 추가합니다.
이러한 함수는 각각 2개의 콜백을 파라미터로 사용합니다. 첫 번째 콜백은 사용자가 메뉴의 항목을 선택하면 실행됩니다. 두 번째 콜백은 메뉴를 표시하기 전에 실행되고 메뉴 항목의 활성화 여부도 확인합니다.
두 콜백 모두 MenuAction
을 파라미터로 수신합니다. MenuAction
은 메뉴 항목을 나타내며, 다음과 같은 기타 유용한 프로퍼티도 포함합니다.
MenuAction.userData
는 AppendAction()
또는 InsertAction()
과 함께 사용되었을 수 있는 사용자 데이터에 대한 레퍼런스를 포함합니다.MenuAction.eventInfo
는 컨텍스트 메뉴의 표시를 트리거한 이벤트에 관한 정보를 포함합니다. 이벤트에 상응하는 액션에서 MenuAction.eventInfo
를 사용하십시오. 예를 들어, 마우스 포지션을 사용하여 선택한 컨텍스트 메뉴 항목에 기반하여 오브젝트를 생성하고 배치할 수 있습니다.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.