Search Unity

Calculator with GUI resources

Discussion in 'Immediate Mode GUI (IMGUI)' started by saori_123, May 22, 2013.

  1. saori_123

    saori_123

    Joined:
    Feb 10, 2013
    Posts:
    7
    Hello, I need help (My english is bad) :p

    My problem is: I need an Example (complete code in C#) how to make a mini-calculator using gui buttons

    where the user can type a number X, and Y number, imagine this to be done in a text field (on screen), then you do the sum of these two numbers if the user presses the button ("Result") .

    Please help meee, I understand that the integer values ​​must be converted to string, but I do not know how to do that

    I need a funcionally short script in C# please

    This is my idea:

    $Calcu.png
     
  2. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    I hope it'll help you :wink:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class Calculator : MonoBehaviour
    5. {  
    6.     string x = "0";
    7.     string y = "0";
    8.     string result = "0";
    9.  
    10.     void OnGUI()
    11.     {
    12.         x = GUI.TextField(new Rect(10, 10, 100, 30), x);
    13.         y = GUI.TextField(new Rect(120, 10, 100, 30), y);
    14.  
    15.         if (GUI.Button(new Rect(200, 10, 100, 30), "Resul"))
    16.         {
    17.             int integer_x = 0;
    18.             int integer_y = 0;
    19.  
    20.             System.Int32.TryParse(x, out integer_x);
    21.             System.Int32.TryParse(x, out integer_y);
    22.  
    23.             result = (integer_x * integer_y).ToString();
    24.         }
    25.  
    26.         GUI.Label(new Rect(220, 10, 100, 30), result);
    27.     }
    28. }
    29.  
     
  3. sanlys_

    sanlys_

    Joined:
    Apr 1, 2016
    Posts:
    15
    im late as F***, but for other people reading, inthink a float value or double would do better than an integer
     
  4. NickTrent

    NickTrent

    Joined:
    Feb 26, 2021
    Posts:
    1
    I'm even later but if you ever do a project similar to this then use a GUI Designer. VisualTK is good but https://labdeck.com/python/ is better. Also, they have a scientific calculator GUI as well ()
    Trent240