Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Buttons as toggle group

Discussion in 'UGUI & TextMesh Pro' started by edredar, Jul 2, 2016.

  1. edredar

    edredar

    Joined:
    Jul 26, 2012
    Posts:
    58
    Hi,

    How can I create somewhat like toggle group, but with buttons instead of toggles? Imagine an array of buttons, and one should be non-interactable, and the others - interactable. Then you click one of the interactable buttons, it becomes non-interactable, while the others, including previously non-interactable, become interactable. I have several arrays of such buttons, and in every one array there should be one non-interactable. I have two ways to achieve this, but it involves either tons of separate scripts, or ridicoluosly huge amount of work, doing it manually in Button's inspector. Maybe, there is some elegant and easy way to achieve this?
    P. S. I will not use toggles, because I need not interactable state OnClick, which they don't do. Thanks in advance.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    Why not just either set all buttons that are part of a group under an empty game object and either loop through all buttons making them interactable and then non-interactable the one you click on (easy, but not prefered).

    or, just keep track of the button that you clicked on. So you have a button variable, when you click on a button, it turns it non-interactable, then assigns that button to the variable. Then when you click on another button, it checks that varable, makes that button interactable, then makes the new clicked on button non-interactable, then updates your variable to target the new button.

    Doesn't require a lot of extra stuff.
     
    eses likes this.
  3. edredar

    edredar

    Joined:
    Jul 26, 2012
    Posts:
    58
    First way I've tried, it's really slow and not so good. I'll give a try to the second one, thaks :)
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    Yeah, the first is ok if you've just got a few buttons. But from your description it sounds like only one button is "selected" at a time, thus the second method should work well. If you have different grouping of buttons, you might need to use several button variables or maybe an array where you know which index to use for each grouping.
     
  5. edredar

    edredar

    Joined:
    Jul 26, 2012
    Posts:
    58
    I've done through variables. Works fine, doesn't require lots of resources, and code is pretty tiny. Thanks again.
     
  6. old_pilgrim

    old_pilgrim

    Joined:
    Nov 15, 2019
    Posts:
    7
    If you're still stumbling across this, you can use Selectable.OnSelect() and Selectable.OnDeselect() as well, should you wish to reserve interactable for other uses.
     
  7. StanleyBishopBlack

    StanleyBishopBlack

    Joined:
    Aug 22, 2019
    Posts:
    4
    Necroposting time!
    I have a third approach, which I happen to like more, it's a bit more complicated though.

    Fire an event from a button on which all buttons in a group react upon, and can change their state accordingly.
    But the event should be a scriptable object, which "carries" information about the button pressed.

    This way you don't need a governing button group class at all, and all button logic stays in buttons

    Edit: I don't know about speed concers though. But it runs fine on mobile device, so why would I bother?
     
    Last edited: Apr 17, 2020