NGUI单选框如何设置?其实NGUI中是使用Toggle来实现的,我们现在就来看一下怎样实现的。
using UnityEngine;
using System.Collections;
public class toggle : MonoBehaviour
{
UIToggle[] toggles;
int value = 0;  //单选框所代表的的值:1、2、3
void Awake()
{
toggles = new UIToggle[3];
for (int i = 0; i < 3; i++)
{
toggles[i] = this.transform.Find("Anchor/Panel/CheckBox/Toggle_" + (i+1)).gameObject.GetComponent<UIToggle>();
print(toggles[i].name);
toggles[i].gameObject.AddComponent<UIEventListener>();
UIEventListener.Get(toggles[i].gameObject).onClick = ButtonClick;
}
}
int click1 = 0;
int click2 = 0;
int click3 = 0;
void ButtonClick(GameObject obj)
{
string name = obj.name;
switch(name)
{
case "Toggle_1":
click1++;
click2 = 0;
click3 = 0;
if (click1 % 2 == 0)
{
value = 0;
toggles[0].value = false;
GameObject sp = toggles[0].transform.FindChild("Checkmark").gameObject;
sp.SetActive(false);
}
else
{
value = 1;
GameObject sp = toggles[0].transform.FindChild("Checkmark").gameObject;
sp.SetActive(true);
}
break;
case "Toggle_2":
click2++;
click1 = 0;
click3 = 0;
if (click2 % 2 == 0)
{
value = 0;
toggles[1].value = false;
GameObject sp = toggles[1].transform.FindChild("Checkmark").gameObject;
sp.SetActive(false);
}
else
{
value = 2;
GameObject sp = toggles[1].transform.FindChild("Checkmark").gameObject;
sp.SetActive(true);
}
break;
case "Toggle_3":
click3++;
click1 = 0;
click2 = 0;
if (click3 % 2 == 0)
{
value = 0;
toggles[2].value = false;
GameObject sp = toggles[2].transform.FindChild("Checkmark").gameObject;
sp.SetActive(false);
}
else
{
value = 3;
GameObject sp = toggles[2].transform.FindChild("Checkmark").gameObject;
sp.SetActive(true);
}
break;
default:
break;
}
print(value);
}
}