简单2D游戏:2(计算器)
一、如图所示该计算器由一个输入框(InputField),和一些按键(button)组成。则可新建一个InputField输入框、一个text、三个panel(幕布背景(canvas)、计算器背景、控制面板背景),如下图:
二、对相关的button设置完成以后,新建计算器控制脚本(CalculateControl):
1、新建一个空的游戏对象用于挂载脚本,在button创建事件时关联UI事件。如图:
/** * * Title: 计算器开发 * * Description: * 1、定义按键的用户事件响应代码 * 2、用户点击按键但没有点击“=”的时候,显示用户输入不显示结果 * (1)用户连续点击按键的处理 * (2)用户点击操作符的处理 * 3、用户点击等号显示结果 * * Date: 2017.10.1 * * Version: * * Modify Recoder: * */ using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class CalculateControl : MonoBehaviour { private float _FirstNumber; //第一个操作数 private float _SecondNumber; //第二个操作数 private float _ResultNumber; //输入结果 private string _Operator; //操作符 private string _UserpartResult; //用户点击的部分结果 public InputField _DisplayInfo; //显示信息的控件 //初始化操作数 private void Start() { _FirstNumber = 0; _SecondNumber = 0; _ResultNumber = 0; _ResultNumber = 0; _Operator = null; _UserpartResult = null; } #region 用户点击数值事件 public void ClickButtonNumber_0() { ProcessNumber("0"); } public void ClickButtonNumber_1() { ProcessNumber("1"); } public void ClickButtonNumber_2() { ProcessNumber("2"); } public void ClickButtonNumber_3() { ProcessNumber("3"); } public void ClickButtonNumber_4() { ProcessNumber("4"); } public void ClickButtonNumber_5() { ProcessNumber("5"); } public void ClickButtonNumber_6() { ProcessNumber("6"); } public void ClickButtonNumber_7() { ProcessNumber("7"); } public void ClickButtonNumber_8() { ProcessNumber("8"); } public void ClickButtonNumber_9() { ProcessNumber("9"); } #endregion #region 用户点击运算符 public void ClickButtonChar_Add() { _Operator = "+"; _DisplayInfo.text = _Operator; //对前面的_UserpartResult清零,否则对后续输入有影响 _UserpartResult = null; } public void ClickButtonChar_Sub() { _Operator = "-"; _DisplayInfo.text = _Operator; _UserpartResult = null; } public void ClickButtonChar_Mult() { _Operator = "*"; _DisplayInfo.text = _Operator; _UserpartResult = null; } public void ClickButtonChar_Div() { _Operator = "/"; _DisplayInfo.text = _Operator; _UserpartResult = null; } /// <summary> /// 用户点击“=”做出运算结果并显示 /// </summary> public void ClickButtonChar_Equal() { if (_Operator != null) { switch (_Operator) { case "+": _ResultNumber = _FirstNumber + _SecondNumber; break; case "-": _ResultNumber = _FirstNumber - _SecondNumber; break; case "*": _ResultNumber = _FirstNumber * _SecondNumber; break; case "/": if (_SecondNumber != 0) { _ResultNumber = _FirstNumber / _SecondNumber; } break; default: break; } _UserpartResult = _ResultNumber.ToString(); //显示结果 _DisplayInfo.text = _UserpartResult; //数值初始化 _FirstNumber = 0; _SecondNumber = 0; _ResultNumber = 0; _ResultNumber = 0; _Operator = null; _UserpartResult = null; } } #endregion //用户连续点击按键的处理 private void ProcessNumber(string UserClickNumber) { //如果连续按键。 //则可用字符串相加的方法,再将结果转换为其他数据类型的方式 _UserpartResult += UserClickNumber; _ResultNumber = float.Parse(_UserpartResult); //判断是为第几个操作数 if (_Operator != null) { _SecondNumber = _ResultNumber; //将字符串转换为float类型 } else { _FirstNumber = _ResultNumber; } _DisplayInfo.text = _UserpartResult; } }