Unity使用多态制作计算器

Unity使用多态制作计算器

Unity中需要的组件

在Unity中创建两个InputField,一个Dropdown,一个Button和一个Text
在这里插入图片描述
在这里插入图片描述

创建脚本文件

计算父类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Jsq : MonoBehaviour
{
    public abstract class Calculate
    {
        public float Number1
        {
            get;
            set;
        }
        public float Number2
        {
            get;
            set;
        }
        public abstract float Compute();
    }
}

加法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Addition : Jsq.Calculate
{
    /// <summary>
    /// 实现父类计算方法
    /// </summary>
    /// <returns>加法计算结果</returns>
    public override float Compute()
    {
        return Number1 + Number2;
    }
}

减法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Subtraction : Jsq.Calculate
{
    /// <summary>
    /// 实现父类计算方法
    /// </summary>
    /// <returns>减法计算结果</returns>
    public override float Compute()
    {
        return Number1 - Number2;
    }
}

乘法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Multiplication : Jsq.Calculate
{
    public override float Compute()
    {
        return Number1 * Number2;
    }
}

除法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : Jsq.Calculate
{
    public override float Compute()
    {
        return Number1 / Number2;
    }
}

等于号按钮
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;

public class EqualBtn : Jsq
{
    public Text txt_Number1;
    public Text txt_Number2;
    public Text cbb_Operator;
    public Text lab_Result;

    public void btn_click()
    {
        //获取两个参数
        float number1 = Convert.ToSingle(txt_Number1.text.Trim());
        float number2 = Convert.ToSingle(txt_Number2.text.Trim());
        //获取运算符
        string operation = cbb_Operator.text.Trim();
        //通过运算符,返回父类类型
        Calculate calculate = GetCalculateResult(operation);
        calculate.Number1 = number1;
        calculate.Number2 = number2;
        //利用多态返回运算结果
        string result = calculate.Compute().ToString();
       this.lab_Result.text = result;

    }

    private Calculate GetCalculateResult(string operation)
    {
        Calculate calculate = null;
        switch (operation)
        {
            case "+":
                calculate = new Addition();
                break;
            case "-":
                calculate = new Subtraction();
                break;
            case "*":
                calculate = new Multiplication();
                break;
            case "/":
                calculate = new Division();
                break;
        }
        return calculate;
    }
}

将等于号按钮脚本赋给Canvas组件

在这里插入图片描述

再将对应的组件拉进去:顺序分别是第一个InputField里的text(注意都是text),第二个InputField里的text,然后是Dropdown里面的Label,最后是显示结果的Text

在这里插入图片描述
在这里插入图片描述

最后在给等于号按钮添加一个执行函数

在这里插入图片描述

最后在运行unity,加减乘除都可以计算,大功告成!

相关推荐
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页