커스텀 컨트롤을 구현하는 경우, 다음과 같은 방법으로 UI 툴킷 이벤트에 응답할 수 있습니다.
이벤트에 대한 응답은 상황에 따라 다를 수 있습니다. 콜백과 가상 메서드 오버라이드의 차이는 다음과 같습니다.
가상 메서드 오버라이드는 클래스의 모든 인스턴스에 적용됩니다. HandleEventBubbleUp
또는 HandleEventTrickleDown
을 오버라이드하는 클래스의 경우 해당 인스턴스에 콜백을 등록할 수도 있습니다.
HandleEventBubbleUp
또는 HandleEventTrickleDown
메서드나 둘 모두를 오버라이드하려면 VisualElement
의 새 서브 클래스를 파생합니다.
시각적 요소 서브 클래스의 각 인스턴스에서 이벤트를 수신하면 HandleEventBubbleUp
과 HandleEventTrickleDown
이 실행됩니다.
다음 예시는 이러한 가상 메서드를 커스터마이즈하는 방법을 보여 줍니다.
override void HandleEventBubbleUp(EventBase evt)
{
// Call the base function.
base.HandleEventBubbleUp(evt);
if (evt.eventTypeId == PointerDownEvent.TypeId())
{
// ...
}
else if (evt.eventTypeId == MouseUpEvent.TypeId())
{
// ...
}
// More event types
}
특정 클래스 인스턴스에 대해 다음과 같은 경우 커스텀 코드를 실행하면 동일한 결과가 나타납니다.
두 경우 모두 이벤트 전파를 중지하면 현재 타겟 콜백 및 메서드 오버라이드를 실행한 후 이벤트에 대한 반응을 방지합니다.
다음은 커스텀 컨트롤을 사용하여 이벤트를 처리하는 베스트 프랙티스입니다.
일반적으로 요소에서 동작을 구현하려면 HandleEventBubbleUp
메서드 오버라이드를 사용합니다.
BubbleUp이 콜백의 기본 전파 단계이므로 코드 실행 타이밍 변경에 대한 걱정 없이 콜백의 모든 코드를 HandleEventBubbleUp
메서드로 옮길 수 있습니다.
동작을 메서드 오버라이드로 구현하는 경우 다음과 같은 이점이 있습니다.
콜백 또는 가상 메서드 오버라이드 내에서 이벤트를 처리할 때 이벤트의 StopPropagation 메서드 중 하나를 호출하여 추가적인 이벤트 전파를 중지할 수 있습니다. 예를 들어, 부모 패널은 트리클다운 단계에서 전파를 중지하여 자식이 이벤트를 수신하지 못하도록 할 수 있습니다.
이벤트 클래스 자체 내에서 EventBase.PreDispatch()
및 EventBase.PostDispatch()
메서드의 실행을 방지할 수는 없습니다.
다음 메서드는 이벤트 전파에 영향을 미칩니다.
StopImmediatePropagation()
: 이벤트에 대한 후속 콜백이 실행되지 않도록 이벤트 전파 프로세스를 즉시 중지합니다.StopPropagation()
: 현재 요소의 마지막 콜백 이후 이벤트 전파 프로세스를 중지합니다. 이렇게 하면 모든 콜백이 현재 요소에서 실행되는 동시에 다른 요소가 이벤트에 응답하지 못하게 할 수 있습니다.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.