Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by tbrewerart · Feb 27, 2013 at 03:11 AM · inputmathgui-buttonnumbers

Simple Calculator for kid math app?

I am a beginning coder trying to make a math app for kids. I have been trying to incorporate calculator type user input into the game but am going nowhere fast. I dont want the app to be simply multiple choice. E.G., I have 3D numbers as buttons (that do various tricks) but they need to input real usable data into a text field (?) I’ve made the selectable Number GO’s, as GUI buttons, but I don’t know how to assign them sequentially as an answer to simple equations: a + b = ? etc. I presume I'll need an enter button like a calculator..

Can anyone just point me in a workable direction? Many thanks in advance for any advice. Perhaps, a tut I might look at? I must say, that I am amazed at the generosity of Unity Community and am thrilled to be a part of it.

Thanks again.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by fafase · Feb 27, 2013 at 07:09 AM

Here is something you can start with:

 using UnityEngine;
 using System.Collections;
 
 public class Calc : MonoBehaviour {
 
     string val;
     int temp;
     Rect text,button1, button2,button3,buttonPlus,buttonEqual;
     bool endOfCalc;
     void Start () {
         text = new Rect (10, 10, 200, 20);
         button1 = new Rect (10, 30, 20, 20);
         button2 = new Rect (30, 30, 20, 20);
         button3 = new Rect(50,30,20,20);
         buttonPlus = new Rect (70, 30, 20, 20);
         buttonEqual = new Rect (90, 30, 20, 20);
         val ="";
         endOfCalc = false;
     }
     
     // Update is called once per frame
     void OnGUI () {
         GUI.TextField (text, val);
         if(GUI.Button (button1,"1")){
             Calculation("1");
         }
         if(GUI.Button (button2,"2")){
             Calculation("2");
         }
         if(GUI.Button (button3,"3")){
             Calculation("3");
         }
         if(GUI.Button (buttonPlus,"+")){
             temp +=int.Parse(val);
             val = "";
         }
         if(GUI.Button (buttonEqual,"=")){
             temp +=int.Parse(val);
             val = temp.ToString();
             endOfCalc = true;
         }
     }
     void Calculation(string str){
         if(!endOfCalc)
                 val += str;
         else{
             val = "";
             val += str;
             endOfCalc = false;
             temp = 0;
         }
     }
 }

It only has three values and one operation but the principle remains the same for other operation. You can also change the layout ans some actions at will.

The Calculation method simply check if we are done with the calculation, that is to erase the previous one.

Enjoy!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Feb 27, 2013 at 11:38 AM 0
Share

very kind :)

avatar image
0

Answer by robertbu · Feb 27, 2013 at 06:37 AM

The logic is largely not Unity. If you Google combinations of 'calculator' 'simple' 'source' 'app' 'stack' 'C#', you will find numerous hits and bodies of code. Writing a simple calculator app can be found in entry-level computer science courses, often in conjunction with the introduction of the 'Stack' data structure. It is a good data structure for accumulating and processing the input from your buttons, and there is a class for it available inside Unity.

One method of implementing the calculator would be to have a central class that collects input and process data. As a first step, you might want to figure out how to communicate between your buttons and a single, bare-bones class that simply outputs a unique message for each button pressed.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by tbrewerart · Feb 27, 2013 at 10:44 PM

Amazing! Thanks for everyone's generosity. Haven't had a moment to really scrutinize it— day job and friends visiting. I'll get back. Hopefully I can do something clever with it.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make each button load a different number with out cycling 1 Answer

Linearization of Android Acceleration Input 1 Answer

How would you convert circular joystick input to a square overlay? 1 Answer

Unity GUI Style with if Statements 1 Answer

Get number input from user 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges