Concepts
This page explains how UIFlow decides where focus goes, so you know which tool to use when a move doesn’t do what you want.
The navigator
Section titled “The navigator”A UIFlowNavigator owns one subtree of a panel: its root. Add a UIFlowDocument component next to a
UIDocument to attach one to the document’s root, or call UIFlowNavigator.Attach(element) on any element.
Elements outside an attached subtree keep Unity’s default behaviour, and several navigators can live in one panel.
The navigator listens for NavigationMoveEvents, the events UI Toolkit sends for the d-pad, the left stick, the
arrow keys and Tab. Controls that use directions themselves handle the event first and keep working: sliders,
list views, open dropdowns and text fields being edited with the keyboard. When the navigator takes a move, it
cancels Unity’s own focus move.
A move goes through these steps, and stops at the first one that gives an answer:
- Links on the focused element, or on the composite control that owns it.
- The search, inside the innermost scope around the focused element.
- Links on containers inside that scope: a way out of a group.
- The scope’s edge: wrap, stop, or escape and search again in the enclosing scope.
navigator.lastStep tells you which step decided the last move, and the debugger shows it too.
Focus rules: what can take focus
Section titled “Focus rules: what can take focus”Before scoring, every element is checked. An element is a candidate only when all of these hold:
- It can take focus (
focusable, enabled in the hierarchy,canGrabFocus). - It is shown: no
display: noneon it or an ancestor, notvisibility: hidden, not fully transparent. - It has a size (layout has run).
- Its
tabIndexis not negative (setting:skipNegativeTabIndex). - It is not excluded: no
uiflow-excludedclass on it or an ancestor. - It is not inside a container that is transitioning: the
uiflow-transitioningclass, or a running USS transition on translate, scale, rotate, opacity, position or size (setting:skipTransitioningContainers). - It is not clipped out of view by an ancestor with
overflow: hidden, not off the panel, and not covered by another element (setting:skipClippedAndCovered). Content scrolled out of a visible ScrollView stays reachable: the navigator scrolls to it.
Composite controls are treated the way Unity treats them: a Slider or a Toggle is one target, a ListView
is entered as a whole. Scroll bars are never targets.
navigator.Evaluate(element) returns the first rule an element fails, as a RejectionReason.
Scoring: which element is “next”
Section titled “Scoring: which element is “next””The search compares the rectangles of the focused element and of each candidate, in panel space.
- A candidate must lie ahead in the pressed direction: its centre past the focused element’s centre.
- It must lie inside a cone around the direction (setting:
coneHalfAngle, 60 degrees by default). - Its score is the gap along the direction, plus the sideways gap times
orthogonalWeight(5 by default), plus a smallalignmentWeightfor the sideways offset of the centres. The lowest score wins. So an element that lines up wins over a closer element that is offset. - If nothing lies inside the cone, every element ahead counts (setting:
halfPlaneFallback), so focus doesn’t get stuck on staggered layouts. - Two candidates with exactly the same score: the one first in reading order wins (top to bottom, then left to right). Mirrored layouts behave the same way in both directions.
When no target is found, focus stays where it is. Unity’s fallback, which wraps around the whole panel, is
blocked (setting: blockDefaultNavigation).
Links: when the search guesses wrong
Section titled “Links: when the search guesses wrong”A link fixes one direction of one element. Each direction holds a query:
| Query | Meaning |
|---|---|
Name or #Name |
The element with that name. |
.class |
The first element with that USS class. |
none |
Block the direction: focus stays where it is. |
empty or auto |
No link: the search decides. |
The target is looked up under the navigator’s root first, then in the whole panel. When the target is a container, focus enters it. A target that can’t take focus right now (hidden, disabled) is skipped and the search decides. A target that doesn’t exist logs one warning.
Links can be set in three places. Code and UXML win over USS.
USS, handy for one-off fixes and for links that depend on a style:
#Quit { --uiflow-nav-down: "#Continue"; }.last-in-row { --uiflow-nav-right: none; }The properties are --uiflow-nav-up, --uiflow-nav-down, --uiflow-nav-left and --uiflow-nav-right.
UXML, on any element, including built-in controls, inside its Bindings. The property attribute is required
by UI Toolkit’s binding system but not used; any value works:
<ui:Button name="PatchNotes" text="Patch notes"> <Bindings> <uiflow:NavigationLinks property="uiflow" left="#NewGame" /> </Bindings></ui:Button>Code, which can also link directly to an element:
play.SetNavigationLink(Direction.Down, "Options");play.SetNavigationLink(Direction.Right, creditsButton);quit.BlockNavigation(Direction.Down);quit.ClearNavigationLink(Direction.Down);A link on a container applies when the search finds nothing inside the container’s scope in that direction: use it as an exit from a group.
Scopes: modals, tabs, grids
Section titled “Scopes: modals, tabs, grids”A scope is a container with its own rules. Use a NavigationScope element in UXML, or turn any element into one
from code with element.MakeNavigationScope(), which returns its NavigationScopeOptions.
| UXML attribute | Option | What it does |
|---|---|---|
horizontal-edge, vertical-edge |
horizontalEdge, verticalEdge |
At the scope’s edge: Escape (default) searches the enclosing scope, Stop keeps focus, Wrap goes to the other side. |
trap-focus |
trapFocus |
Moves can’t take focus out of the scope, and neither can Tab with tabNavigation set to ReadingOrder. Use it for modals. |
cancel-behavior |
cancelBehavior |
ReturnToOrigin: Cancel (B / Esc) puts focus back where it was before it entered the scope. The cancelled event fires first so you can hide the scope. |
enter-behavior |
enterBehavior |
Nearest (default) lands on the nearest element; Remembered lands on the last element focused in the scope. |
navigation-order |
navigationOrder |
Spatial (default) uses the search; TabIndex walks the elements in tabindex order for every direction. |
remember-last-focused |
rememberLastFocused |
Remember the last focused element (default on). |
focus-on-show |
focusOnShow |
Move focus in whenever the scope is shown. Typical for screens and modals. |
default-focus |
defaultFocus |
The element to focus when nothing is remembered: a name or .class. |
Scopes nest. A move searches the innermost scope first; at its edge, Escape repeats the search in the next scope
out, up to the navigator’s root.
Some patterns:
- Modal dialog:
trap-focus,cancel-behavior="ReturnToOrigin",focus-on-show,default-focus. Show and hide it withdisplay; UIFlow moves focus in and out. - Tabs: one scope per tab with
focus-on-showandenter-behavior="Remembered". Switching tabs needs no focus code, and every tab comes back to where the player left it. - Carousel or wrapping menu:
horizontal-edge="Wrap"orvertical-edge="Wrap". - Curated order, when the layout doesn’t match the intended order:
navigation-order="TabIndex"and atabindexon each element.
Collections and scrolling
Section titled “Collections and scrolling”- ScrollView: after every move, the ScrollViews around the new element scroll so it is fully visible
(setting:
scrollFocusedIntoView). When the next element is more than a page away, or nothing focusable is left in that direction but there is still content, the view scrolls a page instead of moving focus (settings:scrollBeforeLeaving,scrollPageFraction). To scroll a ScrollView that has nothing focusable in it, such as a long text, callscrollView.MakeScrollNavigable(): the view takes focus and the d-pad scrolls it. - The right stick scrolls the ScrollView around the focused element (
UIFlowDocument.rightStickScrollSpeed). - ListView, MultiColumnListView, TreeView: entering one selects a visible row (or reveals the current selection); Up and Down move the selection and leave the list at its ends. A sideways move starts from the selected row, so it lands next to it, not next to the middle of the list. Virtualized lists work.
- RadioButtonGroup: entering it lands on the checked option.
- DropdownField: while the menu is open, moves stay inside it and Cancel closes it.
Focus memory
Section titled “Focus memory”- Each scope remembers its last focused element (unless
remember-last-focused="false"). navigator.FocusInto(container)focuses a container’s remembered element, else its default focus, else its top-left element. Use it to open a screen.- When the focused element is hidden, disabled or removed, focus moves to the nearest element left in the same
scope (setting:
recoverLostFocus). This doesn’t happen when the player last used a mouse: clicking empty space is allowed to clear focus. - When nothing is focused and the player presses a direction, focus comes back where it was
(
navigator.RestoreFocus()), not on the first element of the screen.
Devices
Section titled “Devices”InputDeviceTracker watches the Input System and knows which kind of device the player used last: Gamepad,
Keyboard, Pointer (mouse, pen) or Touch. Only deliberate input counts, so stick drift or a nudged mouse don’t
switch devices. Subscribe to InputDeviceTracker.changed to swap button prompts.
UIFlow sets classes on the panel’s root element to match, for your USS:
| Class | When |
|---|---|
uiflow-navigating |
Gamepad or keyboard. Scope focus rings to it. |
uiflow-device-gamepad |
Gamepad. |
uiflow-device-keyboard |
Keyboard. |
uiflow-device-pointer |
Mouse or pen. Scope hover styles to it. |
uiflow-device-touch |
Touch screen. |
When the player switches from the mouse to a gamepad or keyboard while nothing is focused, focus comes back right
away. The press that switched devices only shows focus; it doesn’t also move or press anything
(setting: restoreFocusOnDeviceSwitch). UIFlowDocument.hideCursorWhileNavigating hides the cursor in the
meantime.
Tab and Shift+Tab arrive as moves with the direction Next and Previous. NavigationSettings.tabNavigation
chooses what they do: Unity’s Default order across the panel, Disabled, or ReadingOrder (top to bottom, left to
right, wrapping, and staying inside focus-trapping scopes).
The debugger
Section titled “The debugger”- The overlay (F9 in the editor and development builds,
UIFlowDocument.debugOverlayKey) draws every focusable element (green when it can take focus, red with the reason when it can’t), explicit links as arrows, and for the last move the score or rejection reason of every candidate. Nothing is created or recorded while it is hidden. From code:UIFlowDebugOverlay.Toggle(navigator). - Window > UI Toolkit > UIFlow Debugger lists the candidates of the last move with their scores, and answers “why didn’t focus go to this element?”.
- From code,
NavigationDebugger.Analyzefills aNavigationMoveReportfor any element and direction without moving focus, and aNavigationMoveRecorderrecords every move a navigator makes.