Home > database >  Multiple Selected buttons Unity
Multiple Selected buttons Unity

Time:09-15

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.

CodePudding user response:

Have you check Toggle UI option? You can select and control multiple buttons and there's an option to create groups as well.

You can obviously control the UI to make it look similar to regular button.

Toggle: https://docs.unity3d.com/Packages/[email protected]/manual/script-Toggle.html

Toggle group: https://docs.unity3d.com/Packages/[email protected]/manual/script-ToggleGroup.html

CodePudding user response:

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