Multiple Selected buttons Unity

Viewed 27

Is there support for multiple selected buttons in unity ui.

I saw this question before, but no good answers. Thanks

Like a button group that only deselects buttons in the same group.

2 Answers

Toggle or that script:

public class ButtonToggle : MonoBehaviour
{
    [SerializedField] private bool m_setOnStart;
    static event Action<ButtonToggle> OnPress;
    void Awake()
    {
         OnPress += ActionOnPress;   
         SetActive(m_setOnStart);
    }
    // Add this one to the button event listener
    public void OnButtonPress()
    {
         OnPress?.Invoke(this);
    }
    void ActionOnPress(ButtonToggle button) 
    {
       SetActive(button == this);
    }
    void SetActive(bool value)
    {
        // Do what you need for true/false
        // For instance, set active/inactive color
    }
}

This component goes on each button object, then the public method into the button listener. When a button is pressed, it informs other buttons with the static event. The caller passes itself so all others can set themselves off while the caller sets itself on.

Related