# 1. Screen Readers and the Accessibility Module Part of [[Screen Reader Accessibility]]. Next: [[2. Exposing a UI Toolkit Menu]]. ## 1.1 How a screen reader user plays Turn on Narrator (Windows: **Ctrl + Windows + Enter**), VoiceOver (Mac: **Cmd + F5**; iPhone: **Settings > Accessibility > VoiceOver**) or TalkBack (Android: **Settings > Accessibility > TalkBack**), and the system starts speaking. The user: - moves between items with a swipe right/left (mobile) or the screen reader's next/previous keys (desktop), hearing each one: "Start game, button"; - activates the focused item with a double tap or Enter/Space; - adjusts sliders with a swipe up/down or arrow keys; - explores by touch on mobile: dragging a finger reads whatever is under it. In a Unity game, none of that works by default. Unity draws everything itself, so to the operating system the game window is one blank rectangle. That's what the Accessibility module fixes. ## 1.2 What the module does and doesn't do It **does**: take a tree of nodes you create, each with a label, role, state, value, hint and a rectangle on screen, and present it to the platform's screen reader (UI Automation on Windows, NSAccessibility on macOS, UIAccessibility on iOS, AccessibilityNodeInfo on Android). It sends you events when the user activates, adjusts or focuses a node. It **doesn't**: look at your UI or scene. There's no automatic "make my UI Document accessible". You build and update the tree yourself. Part 2 automates that for UI Toolkit. ## 1.3 The API on one page | Type | Use | |---|---| | `AccessibilityHierarchy` | The tree. `AddNode(label, parent)`, `InsertNode`, `MoveNode`, `RemoveNode(node, removeChildren)`, `Clear`, `ContainsNode`, `TryGetNode`, `RefreshNodeFrames`, `rootNodes` | | `AccessibilityNode` | One item. `label`, `value`, `hint`, `role`, `state`, `frame` or `frameGetter`, `isActive`, `isFocused`, `allowsDirectInteraction`; events `invoked`, `incremented`, `decremented`, `scrolled`, `dismissed`, `focusChanged` | | `AccessibilityRole` | `Button`, `Toggle`, `Slider`, `Dropdown`, `Header`, `StaticText`, `Image`, `TextField`, `SearchField`, `TabBar`, `TabButton`, `ScrollView`, `Container`, `KeyboardKey`, `None` | | `AccessibilityState` | Flags: `Disabled`, `Selected`, `Expanded` | | `AssistiveSupport` | `isScreenReaderEnabled`, `screenReaderStatusChanged`, `activeHierarchy`, `notificationDispatcher`, `nodeFocusChanged`, `screenReaderStatusOverride` | | `IAccessibilityNotificationDispatcher` | `SendAnnouncement(text)`, `SendLayoutChanged(focus)`, `SendScreenChanged(focus)`, `SendPageScrolledAnnouncement(text, focus)` | | `AccessibilitySettings` | The OS's `fontScale`, `isBoldTextEnabled`, `isClosedCaptioningEnabled`, with change events; see [[Accessibility]] | `AccessibilityNode.selected` still exists but is obsolete. Use `invoked`, which is what it was renamed to in Unity 6.3. ### Label, value, hint, role, state The screen reader builds its sentence from these, in a platform-specific order, for example "Music volume, 70 percent, slider. Swipe up or down to adjust." - **Label**: what it is. Short. Never include the role: "Start game", not "Start game button". - **Value**: its current setting, for toggles, sliders and dropdowns. - **Hint**: what happens when activated, only if not obvious: "Deletes your save". - **Role**: tells the reader how to describe and operate it. - **State**: `Disabled` makes it say "dimmed" or "unavailable". ## 1.4 Rules from the manual 1. **Set `activeHierarchy` every time the screen reader turns on.** Unity sets it to `null` when the reader turns off, to save resources, and only accepts a hierarchy while a reader is on. Listen to `screenReaderStatusChanged`. 2. **Tell the reader when the tree changes.** After adding, removing or moving nodes, or changing frames, call `SendLayoutChanged` for small changes or `SendScreenChanged` for a whole new screen. 3. **Don't swap the active hierarchy often.** Building the native representation costs time, especially on macOS. Keep one hierarchy and change its nodes. ## 1.5 ScreenReaderService.cs One component owns the hierarchy. Every screen and world object adds nodes to it under its own container. **`ScreenReaderService.cs`** ```csharp using UnityEngine; using UnityEngine.Accessibility; /// <summary> /// Owns the one AccessibilityHierarchy the game exposes. Screens and world objects add nodes under /// their own container node; this service (re)activates the hierarchy whenever the screen reader turns /// on, sends layout/screen-changed notifications, and paces announcements through AnnouncementQueue. /// </summary> [DefaultExecutionOrder(-500)] public sealed class ScreenReaderService : MonoBehaviour { [Tooltip("Editor only: pretend a screen reader is on, so the Accessibility Hierarchy Viewer shows the hierarchy")] #pragma warning disable 414 // read only in the Editor [SerializeField] bool forceEnableInEditor = true; #pragma warning restore 414 [SerializeField] float announcementGap = 1.5f; public static ScreenReaderService Instance { get; private set; } public AccessibilityHierarchy Hierarchy { get; } = new AccessibilityHierarchy(); public static bool IsScreenReaderOn => AssistiveSupport.isScreenReaderEnabled; readonly AnnouncementQueue _announcements = new AnnouncementQueue(); void Awake() { if (Instance != null) { Destroy(gameObject); return; } Instance = this; DontDestroyOnLoad(gameObject); _announcements.Gap = announcementGap; #if UNITY_EDITOR if (forceEnableInEditor) AssistiveSupport.screenReaderStatusOverride = AssistiveSupport.ScreenReaderStatusOverride.ForceEnabled; #endif } void OnEnable() { AssistiveSupport.screenReaderStatusChanged += OnStatusChanged; Activate(); } void OnDisable() { AssistiveSupport.screenReaderStatusChanged -= OnStatusChanged; if (Instance == this) AssistiveSupport.activeHierarchy = null; } void OnDestroy() { #if UNITY_EDITOR if (Instance == this) AssistiveSupport.screenReaderStatusOverride = AssistiveSupport.ScreenReaderStatusOverride.OSDriven; #endif if (Instance == this) Instance = null; } void OnStatusChanged(bool on) { // Unity clears activeHierarchy when the reader turns off; set it again every time it comes on. if (on) Activate(); else _announcements.Clear(); } void Activate() { if (AssistiveSupport.isScreenReaderEnabled) AssistiveSupport.activeHierarchy = Hierarchy; } /// <summary>A few nodes changed (added, removed, moved, relabelled). Optionally move focus.</summary> public void LayoutChanged(AccessibilityNode focus = null) { if (IsScreenReaderOn) AssistiveSupport.notificationDispatcher.SendLayoutChanged(focus); } /// <summary>A whole new screen appeared (menu opened, scene loaded). Focus moves to the given node.</summary> public void ScreenChanged(AccessibilityNode focus = null) { if (IsScreenReaderOn) AssistiveSupport.notificationDispatcher.SendScreenChanged(focus); } public static void Announce(string text, AnnouncementPriority priority = AnnouncementPriority.Normal) { if (Instance != null && IsScreenReaderOn) Instance._announcements.Enqueue(text, priority, Time.unscaledTimeAsDouble); } void Update() { var next = _announcements.Next(Time.unscaledTimeAsDouble); if (next != null && IsScreenReaderOn) AssistiveSupport.notificationDispatcher.SendAnnouncement(next); } } ``` - `[DefaultExecutionOrder(-500)]` makes it exist before screens enable. - `Activate` follows rule 1. `LayoutChanged` and `ScreenChanged` wrap rule 2 and do nothing when no reader is on. - **Force Enable In Editor** sets `screenReaderStatusOverride` to `ForceEnabled` in the Editor, so the hierarchy is live in Play mode and you can inspect it in **Window > Accessibility > Accessibility Hierarchy Viewer**. It's reset to `OSDriven` when the service is destroyed. - `Announce` queues speech through the `AnnouncementQueue` (part 3) and `Update` releases one at a time. It uses unscaled time so announcements still flow while the game is paused. Set up: create an empty `ScreenReader` GameObject in your first scene and add **Screen Reader Service**. Next: [[2. Exposing a UI Toolkit Menu]].