- Home /
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?
Thanks.
Answer by Mr. Mud · Feb 01, 2015 at 06:54 PM
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 tips you gave definitely solves my problem at the moment. Thanks. However, im asking just out of curiosity, is there any way to make IsHighlighted (UI.Button API) work? What is it's purpose?
Due to the property/field being protected, I have a feeling you are not supposed to use it for whatever you are trying to achieve. What it does precisely, is a mystery to me; I suppose it takes care of showing the correct states.
In any case though, you will most likely need to take care of the states themselves. Unless there is an easier method that I am not aware of. The interfaces in the UnityEngine.EventSystems namespace can help you in doing so, as they recieve the events for different states (the documentation on them is a bit lacking). I have bolded the ones you will most likely need:
IBeginDragHandler ICancelHandler IDeselectHandler IDragHandler IDropHandler IEndDragHandler IEventSystemHandler IInitializePotentialDragHandler I$$anonymous$$oveHandler IPointerClickHandler IPointerDownHandler IPointerEnterHandler IPointerExitHandler IPointerUpHandler IScrollHandler ISelectHandler ISubmitHandler IUpdateSelectedHandler
Answer by Mmmpies · Feb 01, 2015 at 06:06 PM
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:
Answer by huulong · Feb 16, 2015 at 01:17 PM
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.
Your answer
Follow this Question
Related Questions
Best method for updating children of instantiated buttons? 0 Answers
How To Change Color Of Text On UI When It's Selected | Unity 4.6 3 Answers
Is there a way to have multiple sets of button highlights? 0 Answers
How do I set a GUI button's text using a string from another script? 0 Answers
Unable to reference a component. 1 Answer