Focusable
Base class for objects that can get the focus.
Read time 2 minutesLast updated 2 days ago
Definition
- Type: Class
- Namespace: UnityEngine.UIElements
- Assembly: UnityEngine.UIElementsModule
- Inherits from: CallbackEventHandler
- Implements: IEventHandler
public abstract class Focusable : CallbackEventHandler, IEventHandler
Remarks
The focus is used to designate an element that will receive keyboard and navigation events.
You can retrieve a panel's current focused element using its FocusController's _focusedElement property.
Additional Resources: KeyboardEventBase<T>, NavigationEventBase<T>
Examples
using UnityEngine;using UnityEngine.UIElements;[RequireComponent(typeof(UIDocument))]public class FocusExample : MonoBehaviour{ void OnEnable() { var startGameButton = GetComponent<UIDocument>().rootVisualElement.Q<Button>("StartGame"); startGameButton.RegisterCallback<FocusInEvent>(ev => { Debug.Log("FocusInEvent: button is focused and can be activated with keyboard or gamepad input."); }); Debug.Log("Calling startGameButton.Focus()."); startGameButton.Focus(); var isFocused = startGameButton.focusController.focusedElement == startGameButton; Debug.Log("Immediately after startGameButton.Focus(): isFocused=" + isFocused); // Expected output: // > Calling startGameButton.Focus(). // > FocusInEvent: button is focused and can be activated with keyboard or gamepad input. // > Immediately after startGameButton.Focus(): isFocused=true }}
Properties
Property | Description |
|---|---|
| canGrabFocus | Whether the element can be focused. |
| delegatesFocus | Whether the element delegates the focus to its children. |
| focusable | Whether an element can potentially receive focus. |
| focusController | Returns the focus controller for this element. |
| tabIndex | An integer used to sort focusable elements in the focus ring. Must be greater than or equal to zero. |
Methods
Method | Description |
|---|---|
| Blur | Tell the element to release the focus. |
| Focus | Attempts to give the focus to this element. |