Unity制作虚拟按键输入数字答案
1、层级结构
GameScene是Panel,其实就是一张灰白色的背景;1level-1就是灰色的背景;Text就是题目;InputField是输入框;input是一张盖住了输入框的全透明图片;input的子对象都是按钮。
注意这张input,需要盖住输入框,不然运行时点击输入框可以键盘输入。
2、代码
public Button btnDelete;
public Button btnQueren;
public Button[] btnCount;
public InputField field;
private GameObject WinPanel;
// Start is called before the first frame update
void Start()
{
WinPanel = GameObject.Find("Canvas/GameScene/WinPanel");
if (btnDelete) btnDelete.onClick.AddListener(ClickDeleteButton);
if (btnQueren) btnQueren.onClick.AddListener(ClickQuerenButton);
if (btnCount[0]) btnCount[0].onClick.AddListener(ClickCount0Button);
if (btnCount[1]) btnCount[1].onClick.AddListener(ClickCount1Button);
if (btnCount[2]) btnCount[2].onClick.AddListener(ClickCount2Button);
if (btnCount[3]) btnCount[3].onClick.AddListener(ClickCount3Button);
if (btnCount[4]) btnCount[4].onClick.AddListener(ClickCount4Button);
if (btnCount[5]) btnCount[5].onClick.AddListener(ClickCount5Button);
if (btnCount[6]) btnCount[6].onClick.AddListener(ClickCount6Button);
if (btnCount[7]) btnCount[7].onClick.AddListener(ClickCount7Button);
if (btnCount[8]) btnCount[8].onClick.AddListener(ClickCount8Button);
if (btnCount[9]) btnCount[9].onClick.AddListener(ClickCount9Button);
}
btnDelete:删除按键
btnQueren:确认按键
btnCount:数字按键
field:输入框
WinPanel:胜利面板
void ClickDeleteButton()
{
if(!field.text.Equals("")||field.text.Trim().Length!=0)
{
field.text = field.text.Remove(field.text.Trim().Length - 1, 1);
}
}
void ClickQuerenButton()
{
if (gameObject.name == "1level-1")
{
if (field.text.Equals("96"))
{
WinPanel.SetActive(true);
Debug.Log("恭喜过关!");
}
else
Debug.Log("答案错误!");
}
else if(gameObject.name == "1level-2")
{
if (field.text.Equals("72"))
{
WinPanel.SetActive(true);
Debug.Log("恭喜过关!");
}
else
Debug.Log("答案错误!");
}
else if (gameObject.name == "1level-3")
{
if (field.text.Equals("0"))
{
WinPanel.SetActive(true);
Debug.Log("恭喜过关!");
}
else
Debug.Log("答案错误!");
}
else if (gameObject.name == "1level-4")
{
if (field.text.Equals("2"))
{
WinPanel.SetActive(true);
Debug.Log("恭喜过关!");
}
else
Debug.Log("答案错误!");
}
else if (gameObjec