Textbox only allow ints, but also negative ints as well as positive

How would one do this easily?

I figured positive only can be done by

tempText = GUI.TextField(Rect(0,0,100,100), amt.ToString());
    temp = 0;
    if (int.TryParse(tempText,temp))
    {
        amt = Mathf.Clamp(temp, -90, 90);
        Debug.Log("Vaild");
    }

but it doesn’t allow for negative numbers to be entered

Well actually this works but if the number if negative but not if you start by typing -

Why not just use a slider and typecast to int?
http://docs.unity3d.com/Documentation/ScriptReference/GUILayout.HorizontalSlider.html

Because someone designed interface and I am just implementing it :slight_smile:

I figured how to but my code isn’t very elegant.

Love this kind of challenge;

public class NumberTextField
{
    private string temp = "";
    private float value = 0;
    private string format = "";

    public NumberTextField() { }

    public NumberTextField(string format)
    {
        this.format = format;
    }

    public float Draw(string label, float value)
    { 
        if (this.value != value)
        {
            this.value = value;
            temp = value.ToString();
        }

        GUILayout.Label(label);
        string input = GUILayout.TextField(temp);

        float result;
        if (float.TryParse(input, out result)  !float.IsInfinity(result))
        {
            temp = result.ToString(format);
            return result;
        }

        if (input.Length == 0 || (input.Length == 1  input.StartsWith("-")))
        {
            temp = input;
            return 0;
        }

        return value;
    }
}

Right now it’s in float, but should be simple enough to turn into integer.
This class is reusable wherever you want it.