enumeration
Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
CloseOptions for defining the role of an AccessibilityNode to assistive technologies.
You can use values from this enum to set the
AccessibilityNode.role. This property helps inform
assistive technologies how to interact with your
AccessibilityNode. Setting accurate roles improves the
usability and user experience of your UI by enabling assistive
technologies to set clear expectations for users. A missing role can
block a user from interacting with your UI, and an incorrect role can
cause confusion and frustration. You can use the default value,
AccessibilityRole.None, for nodes whose role is not
described by any of the enum options. Some examples are container
elements and highly customized controls.
Additional resources: mobile-accessibility.
The following example demonstrates assigning an AccessibilityRole to a
UI element based on its VisualElement type.
// Attach this script to a UIDocument GameObject. // This script demonstrates creating an AccessibilityNode for each child of a // UIDocument's rootVisualElement and assigning an appropriate // AccessibilityRole based on its VisualElement type. // It also shows how to create custom controls to support AccessibilityRoles // that don't have a corresponding VisualElement type. These can be used in // UXML files and the UI Builder. // To view the properties of the AccessibilityNodes in Playmode, go to // Window > Accessibility > Hierarchy Viewer.
using System; using UnityEngine; using UnityEngine.Accessibility; using UnityEngine.UIElements;
public class AccessibilityManager : MonoBehaviour { // The active accessibility hierarchy. AccessibilityHierarchy m_Hierarchy;
void OnEnable() { // Create an accessibility hierarchy and set it as the active // hierarchy. m_Hierarchy = new AccessibilityHierarchy(); AssistiveSupport.activeHierarchy = m_Hierarchy;
// Add accessibility nodes to each child of the root VisualElement. AddNodesToChildren(); }
void AddNodesToChildren() { UIDocument document = GetComponent<UIDocument>(); if (document == null) { Debug.LogError("UIDocument is not attached to this GameObject."); return; }
// Loop through each child of the rootVisualElement of the UIDocument. foreach (var child in document.rootVisualElement.Children()) { // Create a node in the active hierarchy to represent the element. AccessibilityNode node = m_Hierarchy.AddNode();
// Set the node's label to the name of the VisualElement for clarity in // the Hierarchy Viewer. node.label = $"{child.name}";
// Assign an appropriate AccessibilityRole to the node based on the // type of VisualElement. AssignNodeRole(node, child); } }
void AssignNodeRole(AccessibilityNode node, VisualElement element) { // Match the VisualElement type to an AccessibilityRole using the "is" // operator to include subclasses. We check KeyboardKey before Button // because it's a subclass of Button with a distinct AccessibilityRole. if (element is KeyboardKey) node.role = AccessibilityRole.KeyboardKey; else if (element is Button) node.role = AccessibilityRole.Button; else if (element is Image) node.role = AccessibilityRole.Image; else if (element is Label) node.role = AccessibilityRole.StaticText; else if (element is SearchField) node.role = AccessibilityRole.SearchField; else if (element is Header) node.role = AccessibilityRole.Header; else if (element is TabView) node.role = AccessibilityRole.TabBar; else if (element is SliderInt || element is Slider) node.role = AccessibilityRole.Slider; else if (element is Toggle) node.role = AccessibilityRole.Toggle;
// The default value AccessibilityRole.None is used for elements that // don't have a corresponding role. } }
// The following classes define custom controls. They can be extended with // additional properties and methods as needed.
[UxmlElement] public partial class SearchField : TextField {}
[UxmlElement] public partial class KeyboardKey : Button {}
[UxmlElement] public partial class Header : VisualElement {}
None | The accessibility node has no roles. |
Button | The accessibility node behaves like a button. |
Image | The accessibility node behaves like an image. |
StaticText | The accessibility node behaves like static text that can't change. |
SearchField | The accessibility node behaves like a search field. |
KeyboardKey | The accessibility node behaves like a keyboard key. |
Header | The accessibility node behaves like a header that divides content into sections (for example, the title of a navigation bar). |
TabBar | The accessibility node behaves like an ordered list of tabs. |
Slider | The accessibility node behaves like a slider. The value of this node can be continuously adjusted through a range. |
Toggle | The accessibility node behaves like a toggle. |
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.