Search Unity

ToolbarToggle that behaves like a RadioButton ?

Discussion in 'UI Toolkit' started by aybeone, May 25, 2019.

  1. aybeone

    aybeone

    Joined:
    May 24, 2015
    Posts:
    107
    I am trying to achieve a somewhat simple thing but it turns out to be more convoluted than expected:

    Get ToolbarToggle to behave like a radio button, much like what you can on see the lighting window.

    I do register ChangeEvent<bool> since MouseDownEvent does not work, then on that unified callback I do negate value without notifying for other toggles in the toolbar.

    This works fine except that you can un-toggle one of them ... this is because base class Toggle adds a manipulator which will negate value on click.

    I was hoping to solve that relatively easily, that is, without having to roll-out my own control just to shortcut this behavior.

    Any ideas?

    Thanks!
     
  2. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    You can try to add your own Clickable manipulator to the Toggle like so:
    Code (CSharp):
    1. theToggle.AddManipulator(new Clickable(YourOnClickEventFunc));
    and then make sure to stop event propagation in your YourOnClickEventFunc(evt). That way, you would prevent the default Clickable manipulator from firing.
     
  3. aybeone

    aybeone

    Joined:
    May 24, 2015
    Posts:
    107
    Thank you!
     
  4. FuguFirecracker

    FuguFirecracker

    Joined:
    Sep 20, 2011
    Posts:
    419
    @uDamian
    Could you please expand on this?
    Because :
    Code (CSharp):
    1.  
    2. firstToggle.AddManipulator(new Clickable(Handler));
    3.  
    4. private static void Handler(EventBase evt)
    5.         {
    6.            Debug.Log("Pudding");
    7.         }
    Does nothing
     
  5. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    You're right. The Toggle really does not want to be messed with. The main issue is that its Manipulator eats the mouse events and they never get to your new manipulator.

    My recommendation is to keep your toggle states in your own data structure and only use the ChangeEvents from the Toggle to know when they are clicked. Then overwrite all Toggle states from your own data all the time, using
    SetValueWithoutNotify()
    so you don't cause a feedback loop.
     
    Last edited: Aug 26, 2019
    FuguFirecracker likes this.
  6. FuguFirecracker

    FuguFirecracker

    Joined:
    Sep 20, 2011
    Posts:
    419
    Yimmeny ! I had changed my ToggleGroupHandler to react to the MouseUpEvent instead of the ChangeEvent<bool> because I could not in no way get in front of that damnable bool and stop the associated method from firing (twice).

    I'm gonna see if I can go back using SetValueWithoutNotify() ... Thanks