[Unity]:多个UI按钮选择



我们可以选择多个UI按钮吗?像这样:https://ibb.co/gSgDvqh

是否勾选了ToggleGroup?如果它不是你需要的,你可以简单地创建你自己的控件。

如果你想创建像图像一样的按钮,你可以为每个按钮获取bool参数来管理是否点击:

bool btn1Selected=false;
bool btn2Selected=false;

UIManager脚本中添加按钮,当按钮被点击时,将按钮的UI更改为选中的图像按钮。

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;
}
}

}

相关内容

  • 没有找到相关文章

最新更新