Back-end Seperation Calculator using unity to Construct

832101309杨箫 2023-10-16 12:01:17

Headline

  • Project Source address
  • PSP
  • Presentation of the Finished calculator
  • Design and Implementation Process
  • My understanding of back -end seperation
  • Code explanation
  • Some picture of the unity interface
  • Summary

Project Source address

The Link Your Classhttps://bbs.csdn.net/forums/ssynkqtd-04
Assignment RequirementsConstruct a back-end seperation calculator
Objectives of This AssignmentAccomplish the objectives of seperate the UI part and data storing part
MU STU ID and FZU STU ID<21126186_832101309>

the final project code:https://github.com/18959242570/using-unity-to-construct-an-back-end-seperator-calculator.git

PSP

My schedule of doing this back end seperation calculator:

Personal Software Process StagesEstimated Time(minutes)Actual Time(minutes)
Planning
Estimate25minutes30minutes
Development
Analysis30minutes40minutes
Design Spec45minutes50minutes
Design Review30minutes25minutes
Coding Standard1h1h
Design20minutes20minutes
Coding1h1.5h
Code Review15minutes30minutes
Test45minutes45minutes
Reporting
Test Report15minutes20minutes
Size Measurement10minutes10minutes
Postmortem &Process Improvement Plan
Sum5.92h7h

Presentation of the Finished calculator

Design and Implementation Process

My understanding of back -end seperation

img

  1. Plan what function I need to add -- the requirement I need to accomplish
    such as if I want to add a sinsoidol function, so I need to learn how this function operating. Just like this function, first I need to click the sin button, and then click what radiant I want to calculate. So if I want to calculate value of sin(30 degree). I need first click sin and then 30 just I show in the previous video.
    So this is a very important thing, that is trying to understand how the calculator operating the different operation.
  2. Accomplish the simple function of the easy operation --using the most easy way to achieve the goal will make the code better
    Such as my sin function code in the unity, I use the textMeshPro1.text to show the entering function and I also use a memory connected with a Mathf.Round to format the resulting results. And the function of Saving the data I will tell you after this part.
if(sin ==true)
{
    textMeshPro1.text += "=";
    memory = Mathf.Round(Mathf.Sin(memory2*Mathf.PI/180f)*100f)/100f;
    Save(index.ToString(), textMeshPro1.text += memory.ToString());
    CLEAR0();
    index++;
    sin = false;
Code explanation
  1. Accomplish the requirement of doing the back-end seperation programming -- because for unity, the separation of the front and back ends in the code is not so clear. This is because unity's entire software already divides the UI and code into two separate blocks. So at the following, I will concerntrate on telling you how I save the data using the database.
    For the saving function, I use the particular funtion only exist in unity--PlayerPrefs.SetString(string key, string value); This is a funtion that unity can let the string key and string value save into unity's registry--just like the database in other apps. And Ans is a function that can call database data,Ansclear is a function that can clear all the data in the database.
public void Save(string formula, string result) 
    {    
        PlayerPrefs.SetString(formula,result);
        PlayerPrefs.Save();
    }
public void Ans()
{
    string[] ansfind = { "1","2","3","4","5","6","7","8","9","10"};
    
    if (PlayerPrefs.HasKey(ansfind[i]))
    {
        textMeshPro.text = PlayerPrefs.GetString(ansfind[i]);
        i++;
        Debug.Log(i);
    }
    
    if (i == 9)
    {
        i = 0;
    }
    
    
}
public void AnsClear()
{
    CLEAR();
    PlayerPrefs.DeleteAll();
}

the main code of doing the operation

    public TextMeshProUGUI textMeshPro, textMeshPro1;

    private float memory, memory2, result = 0;
    private bool plus, minus, mul, devide,remainder,sin,cos = false;
    public int index = 1;
    public int i = 0;
    public void num1()
    {

        Debug.Log("在遮挡范围内");
        if (GameObject.FindGameObjectWithTag("Show"))
        {
            textMeshPro.text += "1";
            textMeshPro1.text += "1";

        }
    }
    public void num2()
    {

        Debug.Log("在遮挡范围内");
        if (GameObject.FindGameObjectWithTag("Show"))
        {
            textMeshPro.text += "2";
            textMeshPro1.text += "2";
        }
    }
    public void num3()
    {

        Debug.Log("在遮挡范围内");
        if (GameObject.FindGameObjectWithTag("Show"))
        {
            textMeshPro.text += "3";
            textMeshPro1.text += "3";
        }
    }
    public void num4()
    {

        Debug.Log("在遮挡范围内");
        if (GameObject.FindGameObjectWithTag("Show"))
        {
            textMeshPro.text += "4";
            textMeshPro1.text += "4";
        }
    }
    public void num5()
    {

        Debug.Log("在遮挡范围内");
        if (GameObject.FindGameObjectWithTag("Show"))
        {
            textMeshPro.text += "5";
            textMeshPro1.text += "5";
        }
    }
    public void num6()
    {

        Debug.Log("在遮挡范围内");
        if (GameObject.FindGameObjectWithTag("Show"))
        {
            textMeshPro.text += "6";
            textMeshPro1.text += "6";
        }
    }
    public void num7()
    {

        Debug.Log("在遮挡范围内");
        if (GameObject.FindGameObjectWithTag("Show"))
        {
            textMeshPro.text += "7";
            textMeshPro1.text += "7";
        }
    }
    public void num8()
    {

        Debug.Log("在遮挡范围内");
        if (GameObject.FindGameObjectWithTag("Show"))
        {
            textMeshPro.text += "8";
            textMeshPro1.text += "8";
        }
    }
    public void num9()
    {

        Debug.Log("在遮挡范围内");
        if (GameObject.FindGameObjectWithTag("Show"))
        {
            textMeshPro.text += "9";
            textMeshPro1.text += "9";
        }
    }
    public void num0()
    {

        Debug.Log("在遮挡范围内");  // this is a func that can be a debug func which can debug whether the func below their func can be done
        if (GameObject.FindGameObjectWithTag("Show"))
        {
            textMeshPro.text += "0";
            textMeshPro1.text += "0";
        }
    }
    public void numDOT()
    {

        Debug.Log("在遮挡范围内");
        if (GameObject.FindGameObjectWithTag("Show"))
        {
            textMeshPro.text += ".";
            textMeshPro1.text += ".";
        }
    }
    public void numPLUS()
    {
        plus = true;
        textMeshPro1.text += "+";
    }
    public void numMINUS()
    {
        minus = true;
        textMeshPro1.text += "-";
    }
    public void numMUL()
    {
        mul = true;
        textMeshPro1.text += "*";
    }
    public void numDEVIDE()
    {
        devide = true;
        textMeshPro1.text += "/";
    }
    public void numREMINDER()// fullfill the remainder funciton
    {
        remainder = true;
        textMeshPro1.text += "%"; 
    }
    public void numSin()
    {
        sin = true;
        textMeshPro1.text += "sin";
    }
    public void numCos()
    {
        cos = true;
        textMeshPro1.text += "cos";
    }
    public void CLEARM()
    {

        Debug.Log("在遮挡范围内");
        if (GameObject.FindGameObjectWithTag("Show"))
        {
            textMeshPro.text = null;
            memory2 = 0;
            memory = 0;
            textMeshPro1.text = null;
        }
    }
    public void CLEAR()
    {

        Debug.Log("在遮挡范围内");
        if (GameObject.FindGameObjectWithTag("Show"))
        {
            textMeshPro.text = null;

        }
    }
    public void CLEAR0()
    {

        Debug.Log("在遮挡范围内");
        if (GameObject.FindGameObjectWithTag("Show"))
        {
            textMeshPro1.text = null;

        }
    }
    public void cal()
    {
        memory2 = float.Parse(textMeshPro.text);
        memory = memory2;
        CLEAR();

    }
    public void ent()
    {
        
        if (index <= 10)
        {
            memory2 = float.Parse(textMeshPro.text);
            if (plus == true)
            {
                textMeshPro1.text += "=";
                memory += memory2;
                Save(index.ToString(), textMeshPro1.text += memory.ToString());
                
                CLEAR0();
                index++;

                plus = false;
                
            }
            if (minus == true)
            {
                textMeshPro1.text += "=";
                
                memory -= memory2;
                Save(index.ToString(), textMeshPro1.text += memory.ToString());
                CLEAR0();
                index++;
                minus = false;
            }
            if (mul == true)
            {
                textMeshPro1.text += "=";
                
                memory *= memory2;
                Save(index.ToString(), textMeshPro1.text += memory.ToString());
                CLEAR0();
                index++;
                mul = false;
            }
            if (devide == true)
            {
                textMeshPro1.text += "=";
                
                memory /= memory2;
                Save(index.ToString(), textMeshPro1.text += memory.ToString());
                CLEAR0();
                index++;
                devide = false;
            }
            if (remainder == true)
            {
                textMeshPro1.text += "=";
                
                memory %= memory2;
                Save(index.ToString(), textMeshPro1.text += memory.ToString());
                CLEAR0();
                index++;
                devide = false;
            }
            if(sin ==true)
            {
                textMeshPro1.text += "=";
                memory = Mathf.Round(Mathf.Sin(memory2*Mathf.PI/180f)*100f)/100f; //the Mathf.Round() is a function that can format the result
                Save(index.ToString(), textMeshPro1.text += memory.ToString());
                CLEAR0();
                index++;
                sin = false;
            }
            if (cos == true)
            {
                textMeshPro1.text += "=";
                memory = Mathf.Round(Mathf.Cos(memory2 * Mathf.PI / 180f)*100f)/100f;
                Save(index.ToString(), textMeshPro1.text += memory.ToString());
                CLEAR0();
                index++;
                cos = false;
            }
            Debug.Log(index);
            textMeshPro.text = memory.ToString();
            
        }
        if (index == 10)
        {
            index = 0;
        }
    }

the database part

    public void Save(string formula, string result) 
    {    
        PlayerPrefs.SetString(formula,result);
        PlayerPrefs.Save();
    }
    public void Ans()
    {
        string[] ansfind = { "1","2","3","4","5","6","7","8","9","10"}; // this is the key that I made to be a key to memory what key it is
        
        if (PlayerPrefs.HasKey(ansfind[i]))
        {
            textMeshPro.text = PlayerPrefs.GetString(ansfind[i]);
            i++;
            Debug.Log(i);
        }
        
        if (i == 9)
        {
            i = 0;
        }
        
        
    }
    public void AnsClear()
    {
        CLEAR();
        PlayerPrefs.DeleteAll();
    }

Some picture of the unity interface

img


the Onclick funtion in the button component

img


the inspector of the playercontroller gameobject

img


the Hierarchy of the project

img


the sight of the calculator of the final

Summary

For this assignment 2, I find that I have learn a new knowledge that in th eunity --database and how to memory the previous data and call out theis data. This also enabled me to discover how to cache previous game data in unity games, and the use of function this time gave me a new idea on how to write function, which will be of great help to my further study.

...全文
139 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

176

社区成员

发帖
与我相关
我的任务
社区描述
梅努斯软件工程
软件工程 高校 福建省·福州市
社区管理员
  • LinQF39
  • Jcandc
  • chjinhuu
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧