The question of changing any properties has been answered by Yemachu, but if you are still interested in whether the button is highlighted:
First we assume that selected == highlighted. We will challenge this assumption later.
The Button class derives from the Selectable class which contains what interests us, so I will consider GameObjects with a Selectable component. Below selectable
stands for the component and selectableGameObject
for the game object.
A. If you want to check whether a selectable is currently selected.
using UnityEngine.EventSystems;
...
if (EventSystem.current.currentSelectedGameObject == selectableGameObject) {...}
The problem is that you cannot trigger something when the object becomes selected, except inside Update()
it is only useful for a one-time check.
B. If you want to trigger something when the object becomes selected/unselected, as Yemachu suggested, handle (de)selection in the OnXXX() methods associated to the interfaces of Selectable.
Selectable : UIBehaviour,
IMoveHandler,
IPointerDownHandler, IPointerUpHandler,
IPointerEnterHandler, IPointerExitHandler,
ISelectHandler, IDeselectHandler
Button : Selectable, IPointerClickHandler, ISubmitHandler
Since you want to use a button, you could create a subclass of Button and override some of the following methods:
OnDeselect()
, OnSelect()
( BaseEventData
parameter )
OnPointerEnter()
, OnPointerExit()
( PointerEventData
parameter )
and also OnPointerDown()
, OnPointerUp()
( PointerEventData
parameter ) if you want to add custom behaviours when the user presses / releases a mouse button.
Note: selectable.OnMove() will only check which other selectable object is in the direction you pressed (based on your current navigation mode), then select this object through OnSelect(), so you don’t need to override OnMove() itself.
Do not forget to call base.OnXXX(eventData) at the beginning of your overriding method to ensure the normal behaviour is maintained.
Question: is selected equivalent to highlighted?
No, if you try to select an object from script while it is inactive in the hierarchy, or deactivate a selected object, and then you reactivate it, it will be selected but not highlighted. I am currently studying the source code of the UI and have found a few hacks and fixes to correct this behaviour, but first I must ensure what behaviour is really intended by the development team. If you do not manipulate your objects this way you should have no issues with that.