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,加減乘除都可以計算,大功告成!

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: