Unity3D应用案例系列计算器工具开发
Posted 恬静的小魔龙
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity3D应用案例系列计算器工具开发相关的知识,希望对你有一定的参考价值。
推荐阅读
一、前言
Hello,又见面了,今天分享如何使用Unity制作计算器,难度中等,可以用来学习,或者当成其他项目的小组件导入。
当然,也可以导出来,发布到网页端,来做一个嵌入式工具也可以。
二、效果图及源工程
效果图:
源工程:
https://wwr.lanzoui.com/iHOuzpo4lti
三、实现
3-1 界面搭建
所有的按钮摆放到Background下面。
3-2 代码实现
首先找到所有的按钮,添加到事件:
//结果显示
TextComputeProcess = GameObject.Find("Canvas/Background/Image/TextComputeProcess").GetComponent<Text>();
TextComputeResult = GameObject.Find("Canvas/Background/Image/TextComputeResult").GetComponent<Text>();
TextComputeResult.text = "0";
RUNSTATE = 0;
//操作
BtnReset = GameObject.Find("Canvas/Background/重置").GetComponent<Button>();
BtnReset.onClick.AddListener(() => OperationDispose("CE"));
BtnDelete = GameObject.Find("Canvas/Background/删除").GetComponent<Button>();
BtnDelete.onClick.AddListener(() => OperationDispose("Del"));
//加减乘除
BtnAdd = GameObject.Find("Canvas/Background/加").GetComponent<Button>();
BtnAdd.onClick.AddListener(() => OperationDispose("+"));
BtnSub = GameObject.Find("Canvas/Background/减").GetComponent<Button>();
BtnSub.onClick.AddListener(() => OperationDispose("-"));
BtnMul = GameObject.Find("Canvas/Background/乘").GetComponent<Button>();
BtnMul.onClick.AddListener(() => OperationDispose("*"));
BtnDiv = GameObject.Find("Canvas/Background/除").GetComponent<Button>();
BtnDiv.onClick.AddListener(() => OperationDispose("/"));
BtnEqual = GameObject.Find("Canvas/Background/等于").GetComponent<Button>();
BtnEqual.onClick.AddListener(() => OperationDispose("="));
//数字
Btn0 = GameObject.Find("Canvas/Background/0").GetComponent<Button>();
Btn0.onClick.AddListener(() => NumDispose("0"));
Btn1 = GameObject.Find("Canvas/Background/1").GetComponent<Button>();
Btn1.onClick.AddListener(() => NumDispose("1"));
Btn2 = GameObject.Find("Canvas/Background/2").GetComponent<Button>();
Btn2.onClick.AddListener(() => NumDispose("2"));
Btn3 = GameObject.Find("Canvas/Background/3").GetComponent<Button>();
Btn3.onClick.AddListener(() => NumDispose("3"));
Btn4 = GameObject.Find("Canvas/Background/4").GetComponent<Button>();
Btn4.onClick.AddListener(() => NumDispose("4"));
Btn5 = GameObject.Find("Canvas/Background/5").GetComponent<Button>();
Btn5.onClick.AddListener(() => NumDispose("5"));
Btn6 = GameObject.Find("Canvas/Background/6").GetComponent<Button>();
Btn6.onClick.AddListener(() => NumDispose("6"));
Btn7 = GameObject.Find("Canvas/Background/7").GetComponent<Button>();
Btn7.onClick.AddListener(() => NumDispose("7"));
Btn8 = GameObject.Find("Canvas/Background/8").GetComponent<Button>();
Btn8.onClick.AddListener(() => NumDispose("8"));
Btn9 = GameObject.Find("Canvas/Background/9").GetComponent<Button>();
Btn9.onClick.AddListener(() => NumDispose("9"));
BtnPoint = GameObject.Find("Canvas/Background/点").GetComponent<Button>();
BtnPoint.onClick.AddListener(() => NumDispose("."));
BtnPm = GameObject.Find("Canvas/Background/正负").GetComponent<Button>();
BtnPm.onClick.AddListener(() => NumDispose("-"));
按钮操作:
//操作点击处理
private void OperationDispose(string operation)
{
switch (operation)
{
case "+":
if (RUNSTATE == 0)
TextComputeProcess.text = "0 + ";
else
{
TextComputeProcess.text = TextComputeResult.text + " + ";
m_operation = "+";
RUNSTATE = 2;
}
break;
case "-":
if (RUNSTATE == 0)
TextComputeProcess.text = "0 - ";
else
{
TextComputeProcess.text = TextComputeResult.text + " - ";
m_operation = "-";
RUNSTATE = 2;
}
break;
case "*":
if (RUNSTATE == 0)
TextComputeProcess.text = "0 * ";
else
{
TextComputeProcess.text = TextComputeResult.text + " * ";
m_operation = "*";
RUNSTATE = 2;
}
break;
case "/":
if (RUNSTATE == 0)
TextComputeProcess.text = "0 / ";
else
{
TextComputeProcess.text = TextComputeResult.text + " / ";
m_operation = "/";
RUNSTATE = 2;
}
break;
case "=":
if (RUNSTATE == 0)
TextComputeProcess.text = "0 = ";
else
{
if (RUNSTATE == 3)
{
double result;
switch (m_operation)
{
case "+":
result = double.Parse(calculateString) + double.Parse(TextComputeResult.text);
TextComputeProcess.text = calculateString + " + " + TextComputeResult.text + " = ";
TextComputeResult.text = result.ToString();
RUNSTATE = 4;
break;
case "-":
result = double.Parse(calculateString) - double.Parse(TextComputeResult.text);
TextComputeProcess.text = calculateString + " - " + TextComputeResult.text + " = ";
TextComputeResult.text = result.ToString();
RUNSTATE = 4;
break;
case "*":
result = double.Parse(calculateString) * double.Parse(TextComputeResult.text);
TextComputeProcess.text = calculateString + " * " + TextComputeResult.text + " = ";
TextComputeResult.text = result.ToString();
RUNSTATE = 4;
break;
case "/":
result = double.Parse(calculateString) / double.Parse(TextComputeResult.text);
TextComputeProcess.text = calculateString + " / " + TextComputeResult.text + " = ";
TextComputeResult.text = result.ToString();
RUNSTATE = 4;
break;
default:
break;
}
}
else
{
TextComputeProcess.text = TextComputeResult.text + " = ";
}
}
break;
case "CE":
TextComputeProcess.text = "";
TextComputeResult.text = "0";
RUNSTATE = 0;
break;
case "Del":
if (RUNSTATE == 0)
return;
else
{
if (TextComputeResult.text.Length == 1)
{
TextComputeResult.text = "0";
}
else
{
TextComputeResult.text = TextComputeResult.text.Remove(TextComputeResult.text.Length - 1, 1);
}
}
break;
default:
break;
}
}
数字点击处理:
//数字点击处理
private void NumDispose(string num)
{
switch (num)
{
case ".":
if (RUNSTATE == 0)
TextComputeResult.text = "0";
else
TextComputeResult.text += num;
break;
case "-":
if (RUNSTATE == 0)
TextComputeResult.text = "0";
else
{
if (RUNSTATE == 1)
{
if (pmState)
{
TextComputeResult.text = TextComputeResult.text.Remove(0, 1);
pmState = false;
}
else
{
TextComputeResult.text = num + TextComputeResult.text;
pmState = true;
}
}
else if (RUNSTATE == 2)
{
pmState = false;
}
else if (RUNSTATE == 3)
{
if (pmState)
{
TextComputeResult.text = TextComputeResult.text.Remove(0, 1);
pmState = false;
}
else
{
TextComputeResult.text = num + TextComputeResult.text;
pmState = true;
}
}
else if (RUNSTATE == 4)
{
pmState = false;
OperationDispose("CE");
}
}
break;
default:
if (RUNSTATE == 0)
{
TextComputeResult.text = num;
RUNSTATE = 1;
}
else if (RUNSTATE == 1)
{
pmState = false;
TextComputeResult.text += num;
}
else if (RUNSTATE == 2)
{
calculateString = TextComputeResult.text;
TextComputeResult.text = "";
TextComputeResult.text += num;
RUNSTATE = 3;
}
else if (RUNSTATE == 3)
{
TextComputeResult.text += num;
}
else if (RUNSTATE == 4)
{
OperationDispose("CE");
TextComputeResult.text = num;
RUNSTATE = 1;
}
break;
}
}
完整代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CalculatorControl : MonoBehaviour
{
private Text TextComputeProcess;//计算过程
private Text TextComputeResult;//计算结果
private Button BtnReset;
private Button BtnDelete;
private Button BtnAdd;
private Button BtnSub;
private Button BtnMul;
private Button BtnDiv;
private Button BtnEqual;
private Button Btn0, Btn1, Btn2, Btn3, Btn4, Btn5, Btn6, Btn7, Btn8, Btn9;
private Button BtnPoint, BtnPm;
private string calculateString = "";//计算数
private string m_operation = "";//操作数
private bool pmState = false;//正负状态
private int RUNSTATE = 0;//0 默认 1 输入数字 2 输入操作符 3 输入操作符再输入数字 4 计算结果后
void Start()
{
//结果显示
TextComputeProcess = GameObject.Find("Canvas/Background/Image/TextComputeProcess").GetComponent<Text>()以上是关于Unity3D应用案例系列计算器工具开发的主要内容,如果未能解决你的问题,请参考以下文章