Listen UI Selectable state change programatically.

Hi, Folks!

Is there a way to listen to UI Selectable state change programatically?

I know that I can do it in design time by setting up an Event Trigger component but I am looking to something like a OnStateChanged method or event on the GameObject or on the component that implements Selectable.

Thanks!
Frederico Zveiter
http://fredzvtgamedev.com

Hi, we don't have any callback for this currently. What is your use case?


Is there any event in any of the new UI classes? You know like;

public event EventHandler OnClick;

So we could hook ourself on what's going on...

Hi Tim, Here is my case:

I have a button that when pressed, the label must be translated 4 pixels down and when released the label must translate back to the original position. I've attached an EventTrigger component to that button that calls a methods on a custom script that do this translations on PointerDown and PointerUp events. That solves the question for when I click the button with the pointer but when I activate the button with the keyboard keys, there are no events to catch in order to translate the label. If I could handle the state change event, this would be solved for whatever input method I choose to activate the button, right?

Thanks!
Frederico Zveiter
http://fredzvtgamedev.com

I did this with the auto created animations by offsetting the child by 4pixels in the pressed state


Hm! That's a good solution but I will loose the SpriteSwap transition that I need as well. Is there a way to create animations AND sprite swapping in transitions?

Ya sprite swap easy in single frame animation too


thus... I can't use it. :(

i don't get it? why cant you use it? do offset and tint and sprite swap and everything in animation?


Can I create the sprite swap in the animation as well?

yes


Thus, I can use this solution. Thanks for the help!

Anyways, I would appreciate a callback for state changes.

If you derive your class from selectable you can override this:

protected void UpdateSelectionState (BaseEventData eventData)

to do something like (untested):

protected void UpdateSelectionState (BaseEventData eventData)
{
  var beforeState = currentSelectionState;
  base.UpdateSelectionState(eventData);
  if (currentSelectionState != beforeState)
    DoCallback ();
}
2 Likes


That's awesome! Thanks, Tim!