unity实现简单计算器

 更新时间:2021年8月9日 00:00  点击:1640

本文实例为大家分享了unity实现简单计算器的具体代码,供大家参考,具体内容如下

using System.Text;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using System;

public class Calculator : MonoBehaviour
{
    public Text SpendText;
    private StringBuilder spendPrice;//初始金额
    private string rmbSymbol;
    private float totalPrice, spendPrices;//总和,初始金额
    private bool isFirstDecrease;//避免减为零后的第二次起不能为负
    private bool? isPlusOrDecrease, countType;//点击加减符号,点击等号
    public Button PointButton; 
    private int count;//限制最大输入数
    private void Start()
    {
        spendPrice = new StringBuilder();
        totalPrice = 0;
        spendPrices = 0;
        rmbSymbol = "<size='50'>¥</size> ";
        isPlusOrDecrease = null;//true为加,false为减
        countType = null;//true为加,false为减
        isFirstDecrease = true;
        count = 0;
    }

    public void PointButtonController(bool type)
    {
        PointButton.interactable = type;
    }

    public void InputNumber(int num)
    {
        //按钮
        switch (num)
        {
            case 0:
                if (count < 11)
                {
                    count++;
                    spendPrice.Append("0");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 1:
                if (count < 11)
                {
                    count++;
                    spendPrice.Append("1");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 2:
                if (count < 11)
                {
                    count++;
                    spendPrice.Append("2");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 3:
                if (count < 11)
                {
                    count++;
                    spendPrice.Append("3");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 4:
                if (count < 11)
                {
                    count++;
                    spendPrice.Append("4");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 5:
                if (count < 11)
                {
                    count++;
                    spendPrice.Append("5");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 6:
                if (count < 11)
                {
                    count++;
                    spendPrice.Append("6");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 7:
                if (count < 11)
                {
                    count++;
                    spendPrice.Append("7");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 8:
                if (count < 11)
                {
                    count++;
                    spendPrice.Append("8");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 9:
                if (count < 11)
                {
                    count++;
                    spendPrice.Append("9");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 10:
                if (count < 11)
                {
                    count += 2;
                    spendPrice.Append("00");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 11://加
                isPlusOrDecrease = true;
                countType = true;
                count = 0;
                if (!spendPrice.ToString().Equals(""))
                {
                    if ((totalPrice + float.Parse(spendPrice.ToString()) < 99999999999) && !totalPrice.ToString().Contains("E"))
                    {
                        
                        PointButtonController(true);
                        if (totalPrice != 0)//避免第一次点击加号时没反应
                        {
                            TotalCount();
                        }
                        CountNum();
                    }
                    else
                    {
                        count = 0;
                        PointButtonController(true);
                        totalPrice = 0;
                        spendPrice.Clear();
                        SpendText.DOText(rmbSymbol, 0);
                        isFirstDecrease = true;
                    }
                }
                break;
            case 12://减
                isPlusOrDecrease = false;
                countType = false;
                count = 0;
                if (!spendPrice.ToString().Equals(""))
                {
                    if ((totalPrice + float.Parse(spendPrice.ToString()) < 99999999999) && !totalPrice.ToString().Contains("E"))
                    {
                        PointButtonController(true);
                        if (totalPrice != 0)//避免第一次点击减号时没反应
                        {
                            TotalCount();
                        }
                        CountNum();
                    }
                    else
                    {
                        count = 0;
                        PointButtonController(true);
                        totalPrice = 0;
                        spendPrice.Clear();
                        SpendText.DOText(rmbSymbol, 0);
                        isFirstDecrease = true;
                    }
                }
                break;
            case 13://点
                PointButtonController(false);
                spendPrice.Append(".");
                SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                break;
            case 14://等号
                count = 0;
                if (!spendPrice.ToString().Equals(""))
                {
                    if ((totalPrice + float.Parse(spendPrice.ToString()) < 9999999999) && !totalPrice.ToString().Contains("E"))
                    {
                        PointButtonController(true);
                        TotalCount();
                    }
                    else
                    {
                        count = 0;
                        PointButtonController(true);
                        totalPrice = 0;
                        spendPrice.Clear();
                        SpendText.DOText(rmbSymbol, 0);
                        isFirstDecrease = true;
                    }
                }
                break;
            case 15://清零
                count = 0;
                PointButtonController(true);
                totalPrice = 0;
                spendPrice.Clear();
                SpendText.DOText(rmbSymbol, 0);
                isFirstDecrease = true;
                break;
            default:
                break;
        }
    }
    public void CountNum()
    {
        if (spendPrice.ToString().StartsWith("0") || spendPrice.ToString().Equals(""))//去除开始的无效零
        {
            if (spendPrice.ToString().TrimStart('0') == "" || spendPrice.ToString().TrimStart('0').TrimEnd('0') == ".")//0000,00.00,0.,.0
            {
                spendPrices = 0;
            }
            else
            {
                spendPrices = float.Parse((float.Parse(spendPrice.ToString().TrimStart('0'))).ToString("f2"));
            }
        }
        else
        {
            spendPrices = float.Parse((float.Parse(spendPrice.ToString())).ToString("f2"));
        }
        if (isPlusOrDecrease == true && totalPrice != 0 && spendPrices != 0)
        {
            totalPrice += spendPrices;
            spendPrice.Clear();
            SpendText.DOText(rmbSymbol + totalPrice.ToString(), 0.1f);
            isPlusOrDecrease = null;
        }
        else if (isPlusOrDecrease == true && totalPrice == 0 && spendPrices != 0 && spendPrices != 0)
        {
            totalPrice = spendPrices;
            spendPrice.Clear();
        }
        
        if (isPlusOrDecrease == false && totalPrice == 0 && spendPrices != 0 && isFirstDecrease)
        {
            totalPrice = spendPrices;
            spendPrice.Clear();
            isFirstDecrease = false;
        }
        else if (isPlusOrDecrease == false && spendPrices != 0)
        {
            totalPrice -= spendPrices;
            spendPrice.Clear();
            SpendText.DOText(rmbSymbol + totalPrice.ToString(), 0.1f);
            isPlusOrDecrease = null;
        }
    }

    public void TotalCount()
    {
        if (spendPrice.ToString().StartsWith("0") || spendPrice.ToString().Equals(""))
        {
            if (spendPrice.ToString().TrimStart('0') == "" || spendPrice.ToString().TrimStart('0').TrimEnd('0') == ".")
            {
                spendPrices = 0;
            }
            else
            {
                spendPrices = float.Parse((float.Parse(spendPrice.ToString().TrimStart('0'))).ToString("f2"));
            }
        }
        else
        {
            spendPrices = float.Parse((float.Parse(spendPrice.ToString())).ToString("f2"));
        }
        if (spendPrices != 0)
        {
            if (countType == true)
            {
                totalPrice += spendPrices;
            }
            else if (countType == false)
            {
                totalPrice -= spendPrices;
            }
            spendPrice.Clear();
            SpendText.DOText(rmbSymbol + totalPrice.ToString(), 0.1f);
            countType = null;
        }
    }
    //将科学计数法转化为普通数字
    private Decimal ChangeDataToD(string strData)
    {
        Decimal dData = 0.0M;
        if (strData.Contains("E"))
        {
            dData = Decimal.Parse(strData, System.Globalization.NumberStyles.Float);
        }
        return dData;
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。

[!--infotagslink--]

相关文章

  • C#实现简易计算器功能(附源码)

    这篇文章主要为大家详细介绍了C#实现简易计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-07-21
  • Unity时间戳的使用方法

    这篇文章主要为大家详细介绍了Unity时间戳的使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
  • Unity中 ShaderGraph 实现旋涡传送门效果入门级教程(推荐)

    通过Twirl 旋转节点对Gradient Noise 梯度噪声节点进行操作,就可得到一个旋转的旋涡效果。具体实现代码跟随小编一起通过本文学习下吧...2021-07-11
  • Unity延时执行的多种方法小结

    本文主要介绍了4种延时执行的方法,主要包括Update计时器,Invoke,协程,DoTween,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-07-07
  • C# WinForm程序设计简单计算器

    这篇文章主要为大家详细介绍了C# WinForm程序设计简单计算器,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
  • Unity shader实现遮罩效果

    这篇文章主要为大家详细介绍了Unity shader实现遮罩效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
  • 小程序实现计算器功能

    这篇文章主要为大家详细介绍了小程序实现计算器功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-07-19
  • unity 如何判断鼠标是否在哪个UI上(两种方法)

    这篇文章主要介绍了unity 判断鼠标是否在哪个UI上的两种实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-10
  • Unity实现换装系统

    这篇文章主要为大家详细介绍了Unity实现换装系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-04-11
  • 利用unity代码C#封装为dll的步骤分享

    这篇文章主要给大家介绍了关于利用unity代码C#封装为dll的相关资料,文中通过图文将实现的方法介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
  • unity 实现摄像机绕某点旋转一周

    这篇文章主要介绍了unity 实现摄像机绕某点旋转一周,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
  • Unity Shader实现径向模糊效果

    这篇文章主要为大家详细介绍了Unity Shader实现径向模糊效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-08-09
  • Unity实现截图功能

    这篇文章主要为大家详细介绍了Unity实现截图功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
  • Unity Shader实现2D水流效果

    这篇文章主要为大家详细介绍了Unity Shader实现2D水流效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
  • Unity 按钮添加OnClick事件操作

    这篇文章主要介绍了Unity 按钮添加OnClick事件操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-10
  • Unity使用EzySlice实现模型多边形顺序切割

    这篇文章主要为大家详细介绍了Unity使用EzySlice实现模型多边形顺序切割,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-11-03
  • Unity Shader实现描边OutLine效果

    这篇文章主要为大家详细介绍了Unity Shader实现描边OutLine效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
  • Unity Shader实现裁切效果

    这篇文章主要为大家详细介绍了Unity Shader实现裁切效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
  • unity实现车方向盘转动效果

    这篇文章主要为大家详细介绍了unity实现车方向盘转动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
  • Unity实现汽车前后轮倒车轨迹计算

    这篇文章主要为大家详细介绍了Unity实现汽车前后轮倒车轨迹计算,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-13