Search Unity

Listen UI Selectable state change programatically.

Discussion in 'UGUI & TextMesh Pro' started by FredZvt81, Aug 22, 2014.

  1. FredZvt81

    FredZvt81

    Joined:
    Apr 8, 2014
    Posts:
    24
    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
     
  2. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    Hi, we don't have any callback for this currently. What is your use case?
     
  3. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    Is there any event in any of the new UI classes? You know like;

    Code (CSharp):
    1. public event EventHandler OnClick;
    So we could hook ourself on what's going on...
     
  4. FredZvt81

    FredZvt81

    Joined:
    Apr 8, 2014
    Posts:
    24
    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
     
  5. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
    I did this with the auto created animations by offsetting the child by 4pixels in the pressed state
     
  6. FredZvt81

    FredZvt81

    Joined:
    Apr 8, 2014
    Posts:
    24
    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?
     
  7. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
    Ya sprite swap easy in single frame animation too
     
  8. FredZvt81

    FredZvt81

    Joined:
    Apr 8, 2014
    Posts:
    24
    thus... I can't use it. :(
     
  9. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
    i don't get it? why cant you use it? do offset and tint and sprite swap and everything in animation?
     
  10. FredZvt81

    FredZvt81

    Joined:
    Apr 8, 2014
    Posts:
    24
    Can I create the sprite swap in the animation as well?
     
  11. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
  12. FredZvt81

    FredZvt81

    Joined:
    Apr 8, 2014
    Posts:
    24
    Thus, I can use this solution. Thanks for the help!

    Anyways, I would appreciate a callback for state changes.
     
  13. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    If you derive your class from selectable you can override this:

    protected void UpdateSelectionState (BaseEventData eventData)

    to do something like (untested):
    Code (csharp):
    1.  
    2. protected void UpdateSelectionState (BaseEventData eventData)
    3. {
    4.   var beforeState = currentSelectionState;
    5.   base.UpdateSelectionState(eventData);
    6.   if (currentSelectionState != beforeState)
    7.     DoCallback ();
    8. }
    9.  
     
    Voxel-Busters and FredZvt81 like this.
  14. FredZvt81

    FredZvt81

    Joined:
    Apr 8, 2014
    Posts:
    24
    That's awesome! Thanks, Tim!