Class Selectable
Implements
Inherited Members
Namespace: UnityEngine.UI
Assembly: UnityEngine.UI.dll
Syntax
[AddComponentMenu("UI/Selectable", 35)]
[ExecuteAlways]
[SelectionBase]
[DisallowMultipleComponent]
public class Selectable : UIBehaviour, IMoveHandler, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler, IPointerExitHandler, ISelectHandler, IDeselectHandler, IEventSystemHandler
Constructors
Selectable()
Declaration
protected Selectable()
Fields
m_CurrentIndex
Declaration
protected int m_CurrentIndex
Field Value
Type | Description |
---|---|
int |
s_SelectableCount
Declaration
protected static int s_SelectableCount
Field Value
Type | Description |
---|---|
int |
s_Selectables
Declaration
protected static Selectable[] s_Selectables
Field Value
Type | Description |
---|---|
Selectable[] |
Properties
allSelectableCount
How many selectable elements are currently active.
Declaration
public static int allSelectableCount { get; }
Property Value
Type | Description |
---|---|
int |
allSelectables
A List instance of the allSelectablesArray to maintain API compatibility.
Declaration
[Obsolete("Replaced with allSelectablesArray to have better performance when disabling a element", false)]
public static List<Selectable> allSelectables { get; }
Property Value
Type | Description |
---|---|
List<Selectable> |
allSelectablesArray
Copy of the array of all the selectable objects currently active in the scene.
Declaration
public static Selectable[] allSelectablesArray { get; }
Property Value
Type | Description |
---|---|
Selectable[] |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // required when using UI elements in scripts
public class Example : MonoBehaviour
{
//Displays the names of all selectable elements in the scene
public void GetNames()
{
foreach (Selectable selectableUI in Selectable.allSelectablesArray)
{
Debug.Log(selectableUI.name);
}
}
}
animationTriggers
The AnimationTriggers for this selectable object.
Declaration
public AnimationTriggers animationTriggers { get; set; }
Property Value
Type | Description |
---|---|
AnimationTriggers |
Remarks
Modifications will not be visible if transition is not Animation.
animator
Convenience function to get the Animator component on the GameObject.
Declaration
public Animator animator { get; }
Property Value
Type | Description |
---|---|
Animator |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class ExampleClass : MonoBehaviour
{
private Animator buttonAnimator;
public Button button;
void Start()
{
//Assigns the "buttonAnimator" with the button's animator.
buttonAnimator = button.animator;
}
}
colors
The ColorBlock for this selectable object.
Declaration
public ColorBlock colors { get; set; }
Property Value
Type | Description |
---|---|
ColorBlock |
Remarks
Modifications will not be visible if transition is not ColorTint.
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class ExampleClass : MonoBehaviour
{
public Button button;
void Start()
{
//Resets the colors in the buttons transitions.
button.colors = ColorBlock.defaultColorBlock;
}
}
currentSelectionState
Declaration
protected Selectable.SelectionState currentSelectionState { get; }
Property Value
Type | Description |
---|---|
Selectable.SelectionState |
image
Convenience function that converts the referenced Graphic to a Image, if possible.
Declaration
public Image image { get; set; }
Property Value
Type | Description |
---|---|
Image |
interactable
Is this object interactable.
Declaration
public bool interactable { get; set; }
Property Value
Type | Description |
---|---|
bool |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // required when using UI elements in scripts
public class Example : MonoBehaviour
{
public Button startButton;
public bool playersReady;
void Update()
{
// checks if the players are ready and if the start button is useable
if (playersReady == true && startButton.interactable == false)
{
//allows the start button to be used
startButton.interactable = true;
}
}
}
navigation
The Navigation setting for this selectable object.
Declaration
public Navigation navigation { get; set; }
Property Value
Type | Description |
---|---|
Navigation |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class ExampleClass : MonoBehaviour
{
public Button button;
void Start()
{
//Set the navigation to the default value. ("Automatic" is the default value).
button.navigation = Navigation.defaultNavigation;
}
}
spriteState
The SpriteState for this selectable object.
Declaration
public SpriteState spriteState { get; set; }
Property Value
Type | Description |
---|---|
SpriteState |
Remarks
Modifications will not be visible if transition is not SpriteSwap.
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class ExampleClass : MonoBehaviour
{
//Creates an instance of a sprite state (This includes the highlighted, pressed and disabled sprite.
public SpriteState sprState = new SpriteState();
public Button btnMain;
void Start()
{
//Assigns the new sprite states to the button.
btnMain.spriteState = sprState;
}
}
targetGraphic
Graphic that will be transitioned upon.
Declaration
public Graphic targetGraphic { get; set; }
Property Value
Type | Description |
---|---|
Graphic |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class ExampleClass : MonoBehaviour
{
public Image newImage;
public Button btnMain;
void SomeFunction()
{
//Displays the sprite transitions on the image when the transition to Highlighted,pressed or disabled is made.
btnMain.targetGraphic = newImage;
}
}
transition
The type of transition that will be applied to the targetGraphic when the state changes.
Declaration
public Selectable.Transition transition { get; set; }
Property Value
Type | Description |
---|---|
Selectable.Transition |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class ExampleClass : MonoBehaviour
{
public Button btnMain;
void SomeFunction()
{
//Sets the main button's transition setting to "Color Tint".
btnMain.transition = Selectable.Transition.ColorTint;
}
}
Methods
AllSelectablesNoAlloc(Selectable[])
Non allocating version for getting the all selectables. If selectables.Length is less then s_SelectableCount only selectables.Length elments will be copied which could result in a incomplete list of elements.
Declaration
public static int AllSelectablesNoAlloc(Selectable[] selectables)
Parameters
Type | Name | Description |
---|---|---|
Selectable[] | selectables | The array to be filled with current selectable objects |
Returns
Type | Description |
---|---|
int | The number of element copied. |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // required when using UI elements in scripts
public class Example : MonoBehaviour
{
Selectable[] m_Selectables = new Selectable[10];
//Displays the names of all selectable elements in the scene
public void GetNames()
{
if (m_Selectables.Length < Selectable.allSelectableCount)
m_Selectables = new Selectable[Selectable.allSelectableCount];
int count = Selectable.AllSelectablesNoAlloc(ref m_Selectables);
for (int i = 0; i < count; ++i)
{
Debug.Log(m_Selectables[i].name);
}
}
}
Awake()
Declaration
protected override void Awake()
Overrides
DoStateTransition(SelectionState, bool)
Transition the Selectable to the entered state.
Declaration
protected virtual void DoStateTransition(Selectable.SelectionState state, bool instant)
Parameters
Type | Name | Description |
---|---|---|
Selectable.SelectionState | state | State to transition to |
bool | instant | Should the transition occur instantly. |
FindSelectable(Vector3)
Finds the selectable object next to this one.
Declaration
public Selectable FindSelectable(Vector3 dir)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | dir | The direction in which to search for a neighbouring Selectable object. |
Returns
Type | Description |
---|---|
Selectable | The neighbouring Selectable object. Null if none found. |
Remarks
The direction is determined by a Vector3 variable.
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // required when using UI elements in scripts
public class ExampleClass : MonoBehaviour
{
//Sets the direction as "Up" (Y is in positive).
public Vector3 direction = new Vector3(0, 1, 0);
public Button btnMain;
public void Start()
{
//Finds and assigns the selectable above the main button
Selectable newSelectable = btnMain.FindSelectable(direction);
Debug.Log(newSelectable.name);
}
}
FindSelectableOnDown()
Find the selectable object below this one.
Declaration
public virtual Selectable FindSelectableOnDown()
Returns
Type | Description |
---|---|
Selectable |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // required when using UI elements in scripts
public class Example : MonoBehaviour
{
public Button startButton;
// Disables the selectable UI element directly below the Start Button
public void IgnoreSelectables()
{
//Finds the selectable UI element below the start button and assigns it to a variable of type "Selectable"
Selectable secondButton = startButton.FindSelectableOnDown();
//Disables interaction with the selectable UI element
secondButton.interactable = false;
}
}
FindSelectableOnLeft()
Find the selectable object to the left of this one.
Declaration
public virtual Selectable FindSelectableOnLeft()
Returns
Type | Description |
---|---|
Selectable |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // required when using UI elements in scripts
public class ExampleClass : MonoBehaviour
{
public Button btnMain;
// Disables the selectable UI element directly to the left of the Start Button
public void IgnoreSelectables()
{
//Finds the selectable UI element to the left the start button and assigns it to a variable of type "Selectable"
Selectable secondButton = startButton.FindSelectableOnLeft();
//Disables interaction with the selectable UI element
secondButton.interactable = false;
}
}
FindSelectableOnRight()
Find the selectable object to the right of this one.
Declaration
public virtual Selectable FindSelectableOnRight()
Returns
Type | Description |
---|---|
Selectable |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // required when using UI elements in scripts
public class ExampleClass : MonoBehaviour
{
public Button btnMain;
// Disables the selectable UI element directly to the right the Start Button
public void IgnoreSelectables()
{
//Finds the selectable UI element to the right the start button and assigns it to a variable of type "Selectable"
Selectable secondButton = startButton.FindSelectableOnRight();
//Disables interaction with the selectable UI element
secondButton.interactable = false;
}
}
FindSelectableOnUp()
The Selectable object above current
Declaration
public virtual Selectable FindSelectableOnUp()
Returns
Type | Description |
---|---|
Selectable |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // required when using UI elements in scripts
public class ExampleClass : MonoBehaviour
{
public Button btnMain;
// Disables the selectable UI element directly above the Start Button
public void IgnoreSelectables()
{
//Finds the selectable UI element above the start button and assigns it to a variable of type "Selectable"
Selectable secondButton = startButton.FindSelectableOnUp();
//Disables interaction with the selectable UI element
secondButton.interactable = false;
}
}
InstantClearState()
Clear any internal state from the Selectable (used when disabling).
Declaration
protected virtual void InstantClearState()
IsHighlighted()
Returns whether the selectable is currently 'highlighted' or not.
Declaration
protected bool IsHighlighted()
Returns
Type | Description |
---|---|
bool |
Remarks
Use this to check if the selectable UI element is currently highlighted.
Examples
//Create a UI element. To do this go to Create>UI and select from the list. Attach this script to the UI GameObject to see this script working. The script also works with non-UI elements, but highlighting works better with UI.
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
//Use the Selectable class as a base class to access the IsHighlighted method
public class Example : Selectable
{
//Use this to check what Events are happening
BaseEventData m_BaseEvent;
void Update()
{
//Check if the GameObject is being highlighted
if (IsHighlighted())
{
//Output that the GameObject was highlighted, or do something else
Debug.Log("Selectable is Highlighted");
}
}
}
IsInteractable()
Is the object interactable.
Declaration
public virtual bool IsInteractable()
Returns
Type | Description |
---|---|
bool |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // required when using UI elements in scripts
public class Example : MonoBehaviour
{
public Button startButton;
void Update()
{
if (!startButton.IsInteractable())
{
Debug.Log("Start Button has been Disabled");
}
}
}
IsPressed()
Whether the current selectable is being pressed.
Declaration
protected bool IsPressed()
Returns
Type | Description |
---|---|
bool |
OnCanvasGroupChanged()
Declaration
protected override void OnCanvasGroupChanged()
Overrides
OnDeselect(BaseEventData)
Unset selection and transition to appropriate state.
Declaration
public virtual void OnDeselect(BaseEventData eventData)
Parameters
Type | Name | Description |
---|---|---|
BaseEventData | eventData |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;// Required when using Event data.
public class ExampleClass : MonoBehaviour, IDeselectHandler //This Interface is required to receive OnDeselect callbacks.
{
public void OnDeselect(BaseEventData data)
{
Debug.Log("Deselected");
}
}
OnDidApplyAnimationProperties()
Declaration
protected override void OnDidApplyAnimationProperties()
Overrides
OnDisable()
Declaration
protected override void OnDisable()
Overrides
OnEnable()
Declaration
protected override void OnEnable()
Overrides
OnMove(AxisEventData)
Determine in which of the 4 move directions the next selectable object should be found.
Declaration
public virtual void OnMove(AxisEventData eventData)
Parameters
Type | Name | Description |
---|---|---|
AxisEventData | eventData |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;// Required when using Event data.
public class ExampleClass : MonoBehaviour, IMoveHandler
{
//When the focus moves to another selectable object, Invoke this Method.
public void OnMove(AxisEventData eventData)
{
//Assigns the move direction and the raw input vector representing the direction from the event data.
MoveDirection moveDir = eventData.moveDir;
Vector2 moveVector = eventData.moveVector;
//Displays the information in the console
Debug.Log(moveDir + ", " + moveVector);
}
}
OnPointerDown(PointerEventData)
Evaluate current state and transition to pressed state.
Declaration
public virtual void OnPointerDown(PointerEventData eventData)
Parameters
Type | Name | Description |
---|---|---|
PointerEventData | eventData |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;// Required when using Event data.
public class ExampleClass : MonoBehaviour, IPointerDownHandler// required interface when using the OnPointerDown method.
{
//Do this when the mouse is clicked over the selectable object this script is attached to.
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log(this.gameObject.name + " Was Clicked.");
}
}
OnPointerEnter(PointerEventData)
Evaluate current state and transition to appropriate state. New state could be pressed or hover depending on pressed state.
Declaration
public virtual void OnPointerEnter(PointerEventData eventData)
Parameters
Type | Name | Description |
---|---|---|
PointerEventData | eventData |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;// Required when using Event data.
public class ExampleClass : MonoBehaviour, IPointerEnterHandler// required interface when using the OnPointerEnter method.
{
//Do this when the cursor enters the rect area of this selectable UI object.
public void OnPointerEnter(PointerEventData eventData)
{
Debug.Log("The cursor entered the selectable UI element.");
}
}
OnPointerExit(PointerEventData)
Evaluate current state and transition to normal state.
Declaration
public virtual void OnPointerExit(PointerEventData eventData)
Parameters
Type | Name | Description |
---|---|---|
PointerEventData | eventData |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;// Required when using Event data.
public class ExampleClass : MonoBehaviour, IPointerExitHandler// required interface when using the OnPointerExit method.
{
//Do this when the cursor exits the rect area of this selectable UI object.
public void OnPointerExit(PointerEventData eventData)
{
Debug.Log("The cursor exited the selectable UI element.");
}
}
OnPointerUp(PointerEventData)
Evaluate eventData and transition to appropriate state.
Declaration
public virtual void OnPointerUp(PointerEventData eventData)
Parameters
Type | Name | Description |
---|---|---|
PointerEventData | eventData |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;// Required when using Event data.
public class ExampleClass : MonoBehaviour, IPointerUpHandler, IPointerDownHandler// These are the interfaces the OnPointerUp method requires.
{
//OnPointerDown is also required to receive OnPointerUp callbacks
public void OnPointerDown(PointerEventData eventData)
{
}
//Do this when the mouse click on this selectable UI object is released.
public void OnPointerUp(PointerEventData eventData)
{
Debug.Log("The mouse click was released");
}
}
OnSelect(BaseEventData)
Set selection and transition to appropriate state.
Declaration
public virtual void OnSelect(BaseEventData eventData)
Parameters
Type | Name | Description |
---|---|---|
BaseEventData | eventData |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;// Required when using Event data.
public class ExampleClass : MonoBehaviour, ISelectHandler// required interface when using the OnSelect method.
{
//Do this when the selectable UI object is selected.
public void OnSelect(BaseEventData eventData)
{
Debug.Log(this.gameObject.name + " was selected");
}
}
OnTransformParentChanged()
Declaration
protected override void OnTransformParentChanged()
Overrides
OnValidate()
Declaration
protected override void OnValidate()
Overrides
Reset()
Declaration
protected override void Reset()
Overrides
Select()
Selects this Selectable.
Declaration
public virtual void Select()
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // required when using UI elements in scripts
using UnityEngine.EventSystems;// Required when using Event data.
public class ExampleClass : MonoBehaviour// required interface when using the OnSelect method.
{
public InputField myInputField;
//Do this OnClick.
public void SaveGame()
{
//Makes the Input Field the selected UI Element.
myInputField.Select();
}
}