Use unity to realize calculator function

1. Build ui, create a new panel in Canvas, and create two InputFields as two input boxes to get the input value. Create four new button components as clicked buttons. Create a new Text component as the display of calculation results. The interface is built as shown below

 2. Write code

using System;
using UnityEngine;
using UnityEngine.UI;

namespace JiSuanQi.UI
{
    public class JISuanQiPanel : MonoBehaviour
    {
        public Button mBtnJiaFa, mBtnJianFa, mBtnChengFa, mBtnChuFa;
        public Text mResult;
        public InputField mInputNumA;
        public InputField mInputNumB;
        private void Awake()
        {
            mBtnJiaFa.onClick.AddListener(JiaFa);
            mBtnJianFa.onClick.AddListener(JianFa);
            mBtnChengFa.onClick.AddListener(ChengFa);
            mBtnChuFa.onClick.AddListener(ChuFa);
        }

        private void JiaFa()
        {
            double result = Operation(0);
            UpdateResult(result);
        }

        private void JianFa()
        {
            double result = Operation(1);
            UpdateResult(result);
        }

        private void ChengFa()
        {
            double result = Operation(2);
            UpdateResult(result);
        }

        private void ChuFa()
        {
            double result = Operation(3);
            UpdateResult(result);
        }

        private double Operation(int index)
        {
            if (string.IsNullOrEmpty(mInputNumA.text)||string.IsNullOrEmpty(mInputNumB.text))
            {
                return -1;
            }
            double NumA = double.Parse(mInputNumA.text);
            double NumB = double.Parse(mInputNumB.text);
            switch (index)
            {
                case 0:
                    return NumA + NumB;
                case 1:
                    return NumA - NumB;
                case 2:

                    return NumA * NumB;
                case 3:
                    if (NumB == 0)
                    {
                        return -1;
                    }
                    else
                    {
                        return NumA / NumB;
                    }
                default:
                    return -1;
            }
        }

        private void UpdateResult(double result)
        {
            mResult.text = "结果:" + result;
        }
    }
}

The ui components are assigned by dragging and dropping. One way to assign values ​​to ui components is to use code to find transform.Find("ui panel path of the component").getComponent<component type>(); initialize in the Awake or Start method. I prefer to assign values ​​by dragging and dropping. Because the interface is something that changes frequently, the drag and drop method is used to assign values. If the ui is deleted, the code part does not need to be modified. If you use the code search method and the component is deleted, an error will be reported in the searched part of the code, which will involve the modification of the code part. However, you can see the complete method binding logic in the script by using the code search method. easy to read.

The above simple addition, subtraction, multiplication and division code is completed. This method is the easiest to think of during development, and all functions are realized in one interface.

3. The above code functions normally, but there is a problem. The interface business logic is mixed together. But there are many kinds of interfaces. If a new interface is added now, the business logic of addition, subtraction, multiplication and division must also be used, but the interface is not the interface of a calculator. At this time, it is necessary to rewrite the calculation logic on one side. If the calculation logic changes in the future, then both interfaces must modify the calculation logic. This will not only increase the workload, but also cannot guarantee that each modification is the same, resulting in bugs generation. If you want to use gui to implement the same calculator function now, you also need to copy the interface business logic to the gui code. Every time I change the UI interface, I will encounter the same problem.

4. Code optimization: separation of ui logic and business logic

UI interface logic:

using System;
using UnityEngine;
using UnityEngine.UI;

namespace JiSuanQi.UI
{
    public class JISuanQiPanel : MonoBehaviour
    {
        public Button mBtnJiaFa, mBtnJianFa, mBtnChengFa, mBtnChuFa;
        public Text mResult;
        public InputField mInputNumA;
        public InputField mInputNumB;
        private TestOperation mOperation;
        private void Awake()
        {
            mOperation = new TestOperation();
            mBtnJiaFa.onClick.AddListener(JiaFa);
            mBtnJianFa.onClick.AddListener(JianFa);
            mBtnChengFa.onClick.AddListener(ChengFa);
            mBtnChuFa.onClick.AddListener(ChuFa);
        }

        private void JiaFa()
        {
            double result = Operation(0);
            UpdateResult(result);
        }

        private void JianFa()
        {
            double result = Operation(1);
            UpdateResult(result);
        }

        private void ChengFa()
        {
            double result = Operation(2);
            UpdateResult(result);
        }

        private void ChuFa()
        {
            double result = Operation(3);
            UpdateResult(result);
        }

        private double Operation(int index)
        {
            if (string.IsNullOrEmpty(mInputNumA.text)||string.IsNullOrEmpty(mInputNumB.text))
            {
                return -1;
            }
            double NumA = double.Parse(mInputNumA.text);
            double NumB = double.Parse(mInputNumB.text);
            return mOperation.GetResult(NumA, NumB, index);
        }

        private void UpdateResult(double result)
        {
            mResult.text = "结果:" + result;
        }
    }
}

Business logic:

namespace JiSuanQi
{
    public class TestOperation 
    {
        public double GetResult(double NumA,double NumB,int index)
        {
            switch (index)
            {
                case 0:
                    return NumA + NumB;
                case 1:
                    return NumA - NumB;
                case 2:

                    return NumA * NumB;
                case 3:
                    if (NumB == 0)
                    {
                        return -1;
                    }
                    else
                    {
                        return NumA / NumB;
                    }
                default:
                    return -1;
            }
        }
    }
}

The business logic is extracted into the new class TestOperation, so that the business logic is extracted.

The above code works fine.

4. Problems with the code: All calculation logic is maintained in the TestOperation class. If other calculation logics are added later, such as rooting, remainder, and other new calculation logics, or special modifications to the logic of addition, subtraction, multiplication and division, the modification of the TestOperation class will be involved, so that if the Operation class appears An error will cause the calculation function to be abnormal. And there is no guarantee that each modification will not affect the functions already used. Because it is the TestOperation class that is modified.

5. Code optimization:

using UnityEngine;

namespace JiSuanQi
{
    public abstract class Operation
    {
        public double NumberA;
        public double NumberB;
        public abstract double GetResult();
    }
}



namespace JiSuanQi
{
    public class OperationAdd:Operation
    {
        public override double GetResult()
        {
            return NumberA + NumberB;
        }
    }
}


namespace JiSuanQi
{
    public class OperationReduce:Operation
    {
        public override double GetResult()
        {
            return NumberA - NumberB;
        }
    }
}

namespace JiSuanQi
{
    public class OperationChengFa:Operation
    {
        public override double GetResult()
        {
            return NumberA * NumberB;
        }
    }
}

namespace JiSuanQi
{
    public class OperationChuFa:Operation
    {
        public override double GetResult()
        {
            return NumberA / NumberB;
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using JiSuanQi;
using UnityEngine;

public class SimpleFactory 
{
   public static Operation CreateOperate(int index)
   {
      switch (index)
      {
         case 0:
            return new OperationAdd();
         case 1:
            return new OperationReduce();
         case 2:

            return new OperationChengFa();
         case 3:
            return new OperationChuFa();
         default:
            return null;
      }
   }
}

Business logic optimization, the Operation class is extracted, the operation of addition, subtraction, multiplication and division is used as an object, and the Operation class is used as the base class of the operation. The base class only needs to hold two fields of NumA and NumB and a GetResult method. Concrete implementations are overridden by subclasses. Implement different logic.

SimpleFactory class. Belongs to the factory for object creation. Depending on the input index, different Operations are instantiated. If there is a modification later, you only need to modify the corresponding font.

 

Guess you like

Origin blog.csdn.net/xiaonan1996/article/details/125233643
Recommended