내비게이션 이벤트는 사용자가 D패드를 누르거나, 조이스틱을 움직이거나, Escape
, Enter
, 화살표 키를 누를 때 런타임 시 발생합니다.이는 사용자가 UI를 탐색하고 있다는 표시이지만, 키보드와 같은 특정 입력 기기에만 국한되지 않습니다.포커스 이벤트와의 차이점은 내비게이션 이벤트는 포커스 없이도 새 UI 요소로 이동할 수 있다는 것입니다.
모든 내비게이션 이벤트의 기본 클래스는 NavigationEventBase입니다.
모든 내비게이션 이벤트는 트리클다운하고, 버블업하고, 취소가 가능하지만, 버블업 전파 단계에서는 이러한 이벤트를 수신하는 것이 좋습니다.내비게이션 이벤트는 개별 컨트롤과 상호 작용하는 데 사용할 수도 있는 입력 이벤트에 의해 트리거되기 때문입니다.예를 들어 버튼은 버튼 클릭을 통한 Enter
키 누름에 반응하고, NavigationSubmitEvent
를 취소합니다.버블업 단계에서 이러한 이벤트를 수신하면 해당 이벤트가 내비게이션 이벤트인지 확인할 수 있습니다.
이벤트 | 설명 | 트리클다운 | 버블업 | 취소 가능 |
---|---|---|---|---|
NavigationMoveEvent | 사용자가 이동 입력을 수행할 때 전송됩니다. | ✔ | ✔ | ✔ |
NavigationCancelEvent | 사용자가 취소 입력을 수행할 때 전송됩니다. | ✔ | ✔ | ✔ |
NavigationSubmitEvent | 사용자가 제출 입력을 수행할 때 전송됩니다. | ✔ | ✔ | ✔ |
NavigationMoveEvent
는 사용자가 D패드를 누르거나, 조이스틱을 움직이거나, 화살표 키를 누를 때 전송됩니다.
일부 컨트롤은 자체 기능을 위해 화살표 키를 사용합니다.예를 들어 ListView를 사용하면 사용자가 위쪽 및 아래쪽 화살표 키를 사용하여 항목을 선택할 수 있습니다.이 경우 컨트롤은 NavigationMoveEvent
를 취소하고, 이벤트는 버블업 단계로 들어가지 않습니다.
direction
: 내비게이션 방향입니다. (None
, Left
, Up
, Right
, Down
)
move
:이동 벡터입니다.이벤트가 조이스틱과 같은 아날로그 축 입력에 의해 트리거된 경우 이 프로퍼티는 방향 벡터에 대한 액세스를 제공합니다.
NavigationCancelEvent
는 사용자가 키보드의 Escape
키를 누를 때와 같이 현재 내비게이션 동작을 취소할 때 트리거됩니다.중요한 점은 이 이벤트는 현재 포커스가 맞춰진 요소에는 영향을 미치지 않으므로 취소 전에 포커스가 맞춰진 UI 요소는 선택된 상태로 유지된다는 점입니다.
NavigationSubmitEvent
는 사용자가 키보드의 Enter
키를 누를 때와 같이 제출 버튼을 누를 때 트리거됩니다.
컨트롤이 이벤트를 자체 처리하는 경우에는 이벤트를 취소하여 버블업 단계에 들어가는 것을 방지합니다.예를 들어 포커스가 맞춰진 TextField는 NavigationSubmitEvent
가 버블업되지 않도록 합니다.하지만 포커스를 둘 수 있는 레이블이나 이미지를 사용하면 NavigationSubmitEvent
를 부모 요소에 버블업하여 필요한 경우 이벤트를 처리할 수 있습니다.
다음 코드 예시는 런타임 UI에서 내비게이션 이벤트의 콜백을 등록하는 방법을 보여줍니다.
using UnityEngine.UIElements;
public class MyNavigationHandler :MonoBehaviour
{
void OnEnable()
{
// Get a reference to the root visual element
var uiDocument = GetComponent<UIDocument>();
var rootVisualElement = uiDocument.rootVisualElement;
// Register for navigation events
rootVisualElement.RegisterCallback<NavigationCancelEvent>(OnNavCancelEvent);
rootVisualElement.RegisterCallback<NavigationMoveEvent>(OnNavMoveEvent);
rootVisualElement.RegisterCallback<NavigationSubmitEvent>(OnNavSubmitEvent);
}
private void OnNavSubmitEvent(NavigationSubmitEvent evt)
{
Debug.Log($"OnNavSubmitEvent {evt.propagationPhase}");
}
private void OnNavMoveEvent(NavigationMoveEvent evt)
{
Debug.Log($"OnNavMoveEvent {evt.propagationPhase} - move {evt.move} - direction {evt.direction}");
}
private void OnNavCancelEvent(NavigationCancelEvent evt)
{
Debug.Log($"OnNavCancelEvent {evt.propagationPhase}");
}
}
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.