unity3D入门-制作简单计算器

通过对井字棋代码的学习,了解 OnGUI() 事件,提升阅读 API 文档能力,在编写代码时能运用数据-控制分离的编程思维,现在尝试参考“井字棋”案例,制作一个简单的计算器。

成果演示:

unity3d计算器效果展示-其他-高清完整正版视频在线观看-优酷 (youku.com)

首先创建一个3D工程项目,再在Assets中创建一个脚本,命名为cal

然后打开cal.cs,编写脚本。由于只需要用到IMGUI及OnGui()函数绘制UI界面,不需要建立其他模型。代码需要实现的内容有:最基本的加减乘除运算。

代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class 计算器 : MonoBehaviour
{
    // 模型
    public class CalculatorModel
    {
        public string Operand1 { get; set; } = "";
        public string Operand2 { get; set; } = "";
        public float Result { get; set; }
        public string Operator { get; set; }
        public string Process { get; set; } = "";
        public bool IsEnteringSecondOperand { get; set; } = false;
    }

    // 控制器
    public class CalculatorController
    {
        private CalculatorModel _model;

        public CalculatorController(CalculatorModel model)
        {
            _model = model;
        }

        public void Calculate()
        {
            float operand1 = float.Parse(_model.Operand1);
            float operand2 = float.Parse(_model.Operand2);

            switch (_model.Operator)
            {
                case "+":
                    _model.Result = operand1 + operand2;
                    break;
                case "-":
                    _model.Result = operand1 - operand2;
                    break;
                case "*":
                    _model.Result = operand1 * operand2;
                    break;
                case "/":
                    _model.Result = operand1 / operand2;
                    break;
            }
            _model.Process += _model.Operand1 + " " + _model.Operator + " " + _model.Operand2 + " = " + _model.Result + "\n";
            _model.Operand1 = "";
            _model.Operand2 = "";
            _model.IsEnteringSecondOperand = false;
        }

        public void OnGUI()
        {
            for (int i = 0; i <= 9; i++)
            {
                if (GUI.Button(new Rect(10 + (i % 3) * 70, 10 + (i / 3) * 30, 60, 20), i.ToString()))
                {
                    if (_model.IsEnteringSecondOperand)
                    {
                        _model.Operand2 += i.ToString();
                    }
                    else
                    {
                        _model.Operand1 += i.ToString();
                    }
                }
            }

            if (GUI.Button(new Rect(10, 130, 60, 20), "+"))
            {
                _model.Operator = "+";
                _model.IsEnteringSecondOperand = true;
            }

            if (GUI.Button(new Rect(80, 130, 60, 20), "-"))
            {
                _model.Operator = "-";
                _model.IsEnteringSecondOperand = true;
            }

            if (GUI.Button(new Rect(150, 130, 60, 20), "*"))
            {
                _model.Operator = "*";
                _model.IsEnteringSecondOperand = true;
            }

            if (GUI.Button(new Rect(220, 130, 60, 20), "/"))
            {
                _model.Operator = "/";
                _model.IsEnteringSecondOperand = true;
            }

            if (GUI.Button(new Rect(10, 160, 200, 20), "Calculate"))
            {
                Calculate();
            }

            GUI.Label(new Rect(10, 190, 200, 20), "Result: " + _model.Result);
            GUI.TextArea(new Rect(10, 220, 280, 100), _model.Process);

            // 显示当前输入
            string currentInput = _model.IsEnteringSecondOperand ? _model.Operand2 : _model.Operand1;
            GUI.TextField(new Rect(10, 330, 200, 20), currentInput);
        }
    }
    //系统指在游戏循环驱动下,运用部件完成游戏逻辑。
    private CalculatorModel _model;
    private CalculatorController _controller;

    // Start is called before the first frame update
    void Start()
    {
        _model = new CalculatorModel();
        _controller = new CalculatorController(_model);
    }

    // Update is called once per frame
    void Update()
    {

    }

    void OnGUI()
    {
        _controller.OnGUI();
    }
}

编写完脚本代码后,创建一个空的模型(Create Empty),命名为int,将calculator.cs拖动到init上面

点击play运行,检验功能完成度。

小结:计算器逻辑简单,适合进行初步上手,用于熟悉C#的编码,锻炼数据-控制分离的编程思维。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值