第3个回答 2022-02-10
脚本挂载后,把几个public的按钮 拖进去
public class button_switch:MonoBehaviour{
//
public Button btn_1;
public Button btn_2;
public Button btn_3;
private Sprite btn_on;
private Sprite btn_off;
private int cur_btn = 1;
void Start()
{
btn_on = btn_1.GetComponent<Image>().sprite;
btn_off = btn_2.GetComPonent<Image>().sprite;
btn_1.onClick.AddListener(delegate(){OnClick(1);});
btn_2.onClick.AddListener(delegate(){OnClick(2);});
btn_3.onClick.AddListener(delegate(){OnClick(3);});
}
public void OnClick(int index)
{
//
if(index == cur_btn)
{
return;
}
on_switch(cur_btn,false);
on_switch(index,true);
cur_btn = index;
}
private void on_switch(int index,bool set)
{
switch(index)
{
case 1:btn_1.image.sprite = set == true? btn_on:btn_off; break;
case 2:btn_2.image.sprite = set == true? btn_on:btn_off; break;
case 3:btn_3.image.sprite = set == true? btn_on:btn_off; break;
}
}