Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Nominations have been announced for this years Unity Awards. Celebrate the wonderful projects made by your peers this year and get voting! Vote here!
    Dismiss Notice
  6. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Calculator with GUI resources

Discussion in 'Extensions & OnGUI' 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