How to understand if 4.6 UI button is highlighted?

Hi all,

I want to change the color of the child text object of a button, when the button is highlighted. To do that, I am trying to find a way to understand if the button is highlighted? There is a protected method called IsHighlighted in UI.Button API; however, I haven’t been able to make it work.

Any suggestions on how to do it?


You don’t even need the EventSystem although do look into the EventSystem as it’s very powerful.

With 4.6 UI buttons just change the color in the inspector, like this image show click on the Highlighted Color box and set it there:


There is no reason to use scripts to solve this issue. However, depending on the circumstances, the solution might be slightly different. If only the text needs to change color (and not the background of the button), you can drag the text object in the “Target Graphic” field; now it should give you the desired behaviour.

In case you want to change multiple things at the same time, I would suggest to use “Animation” as the transition instead of “Color Tint”. If no animator is present on the button, it will even create the animator with all required states (clicking “Auto Generate Animation”). From here on, you only need to create animations for the different states and assign them. This video from 3DBuzz explains most you need to know.

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,
        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.