Check pseudo-state of a control
Check the pseudo-state of a control to ensure styles apply correctly.
読み終わるまでの所要時間 4 分最終更新 1ヶ月前
Version: 6000.6+
You can check the pseudo-state of a control in the UI Toolkit. This is useful for debugging and ensuring that your styles are applied correctly based on the state of the control.
Example overview
This example demonstrates how to check the pseudo-state of a button and a toggle control.
You can find the completed files that this example creates in this GitHub repository.
Prerequisites
This guide is for developers familiar with the Unity Editor, UI Toolkit, and C# scripting. Before you start, get familiar with the following:
- Pointer events
- Focus events
- hasHoverPseudoState
- hasActivePseudoState
- hasFocusPseudoState
- hasCheckedPseudoState
- hasDisabledPseudoState
Create the UXML file
Create a UXML file to define the UI structure. This file contains a button and a toggle control that you can interact with to check their pseudo states.
- Create a Unity project with any template.
- In the Project window, create a UXML file named .
ButtonAndToggle.uxml - Double-click the file to open it in the UI Builder.
ButtonAndToggle.uxml - Add a Button to the Hierarchy panel and name it as .
my-button - Add a Toggle to the Hierarchy panel and name it as .
my-toggle - Save your changes.
The finished file looks like this:
ButtonAndToggle.uxml<ui:UXML xmlns:ui="UnityEngine.UIElements"> <ui:Button text="Button" name="my-button"/> <ui:Toggle label="Toggle" name="my-toggle"/></ui:UXML>
Create the C# script
Create a C# script to check the pseudo-states of the button and toggle control. This script registers callbacks to log the pseudo-states when the user interacts with the controls.
- hasFocusPseudoState is only when the element has focus.
true - hasActivePseudoState is only when the pointer is down and over the element.
true - hasHoverPseudoState is only when the pointer is over the element.
true
Create a C# script named with the following content:
PseudoStateChecker.csusing UnityEngine;using UnityEngine.UIElements;public class PseudoStateChecker : MonoBehaviour{ private Button myButton; private Toggle myToggle; void OnEnable() { GetComponent<PanelRenderer>().RegisterUIReloadCallback(OnUIReload); } void OnDisable() { GetComponent<PanelRenderer>().UnregisterUIReloadCallback(OnUIReload); } void OnUIReload(PanelRenderer renderer, VisualElement rootElement, int version) { // Query for the elements. myButton = rootElement.Q<Button>("my-button"); myToggle = rootElement.Q<Toggle>("my-toggle"); if (myButton == null || myToggle == null) { Debug.LogError("Button or Toggle not found in the UI Document!"); return; } // Register callbacks to check states on a Button. myButton.RegisterCallback<PointerEnterEvent>(evt => Debug.Log($"Button PointerEnterEvent: hasHoverPseudoState = {myButton.hasHoverPseudoState}")); myButton.RegisterCallback<PointerLeaveEvent>(evt => Debug.Log($"Button PointerLeaveEvent: hasHoverPseudoState = {myButton.hasHoverPseudoState}")); // Use TrickleDown to ensure the callback receives the event before it is stopped by the Button. myButton.RegisterCallback<PointerDownEvent>( evt => Debug.Log($"Button PointerDownEvent: hasActivePseudoState = {myButton.hasActivePseudoState}"), TrickleDown.TrickleDown); myButton.RegisterCallback<PointerUpEvent>(evt => Debug.Log($"Button PointerUpEvent: hasActivePseudoState = {myButton.hasActivePseudoState}")); myButton.RegisterCallback<FocusInEvent>(evt => Debug.Log($"Button FocusInEvent: hasFocusPseudoState = {myButton.hasFocusPseudoState}")); myButton.RegisterCallback<FocusOutEvent>(evt => Debug.Log($"Button FocusOutEvent: hasFocusPseudoState = {myButton.hasFocusPseudoState}")); // Register a callback to check the state on a Toggle. myToggle.RegisterValueChangedCallback(evt => Debug.Log($"Toggle ValueChangedEvent: hasCheckedPseudoState = {myToggle.hasCheckedPseudoState}")); // Example of checking the disabled state. // You can un-comment this to check the disabled state after 3 seconds. // Invoke(nameof(DisableTheButton), 3f); } private void DisableTheButton() { if (myButton != null) { myButton.SetEnabled(false); Debug.Log($"Button has been disabled: hasDisabledPseudoState = {myButton.hasDisabledPseudoState}"); } }}
Test the example
You can test the example in the SampleScene. The script logs the pseudo-state changes of the button and toggle control to the Console window when you interact with them.
- In the SampleScene, select + > UI Toolkit > Panel Renderer to create a new Panel Renderer GameObject.
- Assign the file to the Source Asset field of the Panel Renderer.
ButtonAndToggle.uxml - Select Add Component and add the script.
PseudoStateChecker - Save the scene.
- Enter Play mode.
- Click the button and toggle the toggle control to check the pseudo-state changes in the Console window.