天天用计算器,有没有考虑过它怎么用代码实现的,看我如何用Unity做一个计算器

举报
恬静的小魔龙 发表于 2022/05/26 13:19:49 2022/05/26
【摘要】 推荐阅读CSDN主页GitHub开源地址Unity3D插件分享简书地址我的个人博客QQ群:1040082875 一、前言Hello,又见面了,今天分享如何使用Unity制作计算器,难度中等,可以用来学习,或者当成其他项目的小组件导入。当然,也可以导出来,发布到网页端,来做一个嵌入式工具也可以。 二、效果图及源工程效果图:源工程:https://wwr.lanzoui.com/iHOuzpo4...

推荐阅读

一、前言

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>();
        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;
        }
    }
}

效果图如下:
在这里插入图片描述

四、后记

完整代码278行,还是依旧那么简练,整体代码难度不大,主要是状态之间的切换:
1、输入数字的状态
2、输入操作符状态
3、输入操作符后再输入数字状态
4、计算结果后状态

理解这些状态后,代码就容易理解了。

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。