Unity developers are familiar with
But if you want a text field that only accepts numeric input, then you could accomplish it with some Regex filtering:
...
myText = Regex.Replace(GUI.TextField(myRect, myText), "[^.0-9]", "");
Now suppose you wanted to contain it all in a function. The first thing that comes to mind is:
{
string result = Regex.Replace(GUI.TextField(r, n.ToString()), "[^.0-9]", "");
return System.Convert.ToInt32(result);
}
Looks fine on the surface, but there's a glaring problem: It can't handle empty inputs. If you want to treat 0 as an empty input and change the function handle to it accordingly, you could do that. I chose to go this route:
{
string result = Regex.Replace(GUI.TextField(r, n.HasValue ? n.Value.ToString() : ""), "[^.0-9]", "");
if (string.IsNullOrEmpty(result))
{
return null;
}
else
{
return System.Convert.ToInt32(result);
}
}
Rather than processing an integer, I process a nullable integer. This way I can discern valid from invalid input without using sentinel values.
(Credit to Labs.kaliko.com for the expression ... and a special thanks to the author for actually including the namespace in their code snippet! )