Home > Net >  [Unity]: Multiple UI Button Selection
[Unity]: Multiple UI Button Selection

Time:07-05

can we select multiple UI buttons? Something like this : https://ibb.co/gSgDvqh

CodePudding user response:

Did you check ToggleGroup? If it is not what you needed, you can simply create your own control.

CodePudding user response:

if you want create button like image , you can get bool parameter for each button for manage is click or not like that :

bool btn1Selected=false;
bool btn2Selected=false;

than in UIManager script add button and when button clicked , change UI of the button to selected image button.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class test : MonoBehaviour
{

    public bool btn1Selected;
    public bool btn2Selected;

    public Sprite selectedSprite;
    public Sprite notSelectedSprite;

    Button btn1, btn2;


    public void BtnClicked(int btnIndex)
    {
        if (btnIndex == 1)
        {
            if (btn1Selected)
                btn1.GetComponent<Button>().image.sprite = notSelectedSprite;
            else
                btn1.GetComponent<Button>().image.sprite = selectedSprite;

            btn1Selected = !btn1Selected;
        }
        else
        {
            if (btn2Selected)
                btn2.GetComponent<Button>().image.sprite = notSelectedSprite;
            else
                btn2.GetComponent<Button>().image.sprite = selectedSprite;

            btn2Selected = !btn2Selected;
        }
    }


}
  • Related