Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

only number textfield?

Discussion in 'Immediate Mode GUI (IMGUI)' started by half_voxel, Apr 3, 2008.

  1. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    977
    Hi.
    I wonder if i can make a textfield that refer to a int instead of a string?
    I'm making a math program and i want the user to be able to type in numbers but not letters. alternatively can you transform a string to a int?
     
  2. Talzor

    Talzor

    Joined:
    May 30, 2006
    Posts:
    197
  3. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    977
    Ok... mm, i have no idea of how to use this in java script. Can you make an example?

    I got this far:

    Code (csharp):
    1. var s :String;
    2. var result = 0;
    3. var boll : boolean;
    4.  
    5. function Start () {
    6.     boll = public static function TryParse( s, result)
    7. }
     
  4. Talzor

    Talzor

    Joined:
    May 30, 2006
    Posts:
    197
    Code (csharp):
    1. var fieldContent : String = "0";
    2. var lastValidNumber : int = 0;
    3.  
    4. function OnGUI() {
    5.     fieldContent = GUILayout.TextField(fieldContent);
    6.    
    7.     var number : int;
    8.     if (System.Int32.TryParse(fieldContent, number)) {
    9.         lastValidNumber = number;
    10.     }
    11.    
    12.     //This both removes illigal characters as well as leading zeros
    13.     fieldContent = lastValidNumber.ToString();
    14. }
    15.  
    16. function Update() {
    17.     Debug.Log(lastValidNumber);
    18. }
     
  5. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    977
    Thank you very much!! It's working now! :D