이벤트 로직을 UI 코드에서 분리하려면 매니퓰레이터를 사용하여 이벤트를 처리합니다. 매니퓰레이터는 UI 요소와의 사용자 상호작용을 처리하는 상태 머신입니다. 매니퓰레이터는 이벤트 콜백을 저장, 등록 및 등록 취소합니다. 매니퓰레이터는 사용자 상호작용 설정을 간소화하므로 각 콜백을 일일이 처리하지 않아도 됩니다. 이벤트를 처리하려면 UI 툴킷이 지원하는 매니퓰레이터 중 하나를 사용하거나 상속합니다.
매니퓰레이터를 생성 및 사용하려면 다음을 수행하십시오.
다음 표에는 지원되는 매니퓰레이터 클래스가 나열되어 있습니다.
매니퓰레이터 | 다음에서 상속 | 설명 |
---|---|---|
Manipulator |
제공된 모든 매니퓰레이터의 기본 클래스입니다. | |
KeyboardNavigationManipulator |
Manipulator |
기기별 입력 이벤트를 키보드를 사용하여 상위 레벨의 탐색 작업으로 변환합니다. |
MouseManipulator |
Manipulator |
마우스 입력을 처리합니다.활성화 필터 리스트가 있습니다. |
ContextualMenuManipulator |
MouseManipulator |
사용자가 오른쪽 클릭하거나 키보드의 메뉴 키를 누르면 컨텍스트 메뉴가 표시됩니다. |
PointerManipulator |
MouseManipulator |
포인터 입력을 처리합니다.활성화 필터 리스트가 있습니다. |
Clickable |
PointerManipulator |
요소에서 마우스 이벤트를 추적하고, 동일한 요소에서 포인터를 누르고 놓는 등의 클릭이 발생하는 시점을 식별합니다. |
다음 예는 매니퓰레이터를 사용하여 이벤트를 처리하는 방법을 보여 줍니다. 다음 작업을 수행합니다.
PointerManipulators
클래스에서 매니퓰레이터를 상속합니다.activators
목록을 사용하여 매니퓰레이터를 활성화하는 조건을 지정합니다. 예를 들어 사용자가 마우스 버튼을 클릭할 때 매니퓰레이터를 활성화하려면 button
필드가 MouseButton.LeftMouse
로 설정된 상태에서
ManipulatorActivationFilter
를 activators
목록에 추가합니다.target
프로퍼티를 사용하여 매니퓰레이터가 연결된 요소에 액세스합니다.RegisterCallbacksOnTarget
및 UnregisterCallbacksFromTarget
메서드를 오버라이드하여 이벤트 콜백을 등록 및 등록 해제합니다.다음 예시에서는 클릭 및 드래그 시 요소를 옮기는 매니퓰레이터를 생성합니다.
[!code-cs[(Modules/UIElements/Tests/UIElementsExamples/Assets/ui-toolkit-manual-code-examples/manipulator/ExampleDragger.cs)]
다음 예시에서는 드래그 시 요소 크기를 조정하는 매니퓰레이터를 생성합니다.
[!code-cs[(Modules/UIElements/Tests/UIElementsExamples/Assets/ui-toolkit-manual-code-examples/manipulator/ExampleResizer.cs)]
매니퓰레이터를 요소에 추가하려면 AddManipulator
메서드를 사용합니다. 요소에서 매니퓰레이터를 제거하려면 RemoveManipulator
메서드를 사용합니다.
다음 예시에서는 ExampleDragger
를 VisualElement
에 추가 및 제거합니다.
// Create a VisualElement.
var myElement = new VisualElement();
// Add manipulators to the VisualElement.
myElement.AddManipulator(new ExampleDragger());
// Remove manipulators from the VisualElement.
myElement.RemoveManipulator<ExampleDragger>();
다음 예시에서는 ExampleResizer
를 VisualElement
에 추가합니다.
var box = new VisualElement()
{
style =
{
left = 100,
top = 100,
width = 100,
height = 100,
backgroundColor = Color.red
},
pickingMode = PickingMode.Position,
};
box.AddManipulator(new ExampleResizer());
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.