Doesn’t seem like Unity comes with an integer (or float) field for GUI. I’m trying to go about this the simplest way possible. I want the players to be able to input an integer value into a text field. I’ve tried using parse float and casting that to an integer, and that works fine, but I get errors if I put a letter for example. That is also fine, I just know the script errors out after that. I want to be able to tell the player to input the correct integer basically, so just change the value of the string in the textfield to something like: “N/A” if they put a letter, for example. I’ve also set that up, but like I said, before my code reaches this point, it errors out. Any ideas on how to make this as simple as possible?
–Eric
So, I understand what they’re trying to do, but I’m using Javascript, and right now I’m getting an error that says type char can’t be compared to type string. Any ideas?
function OnGUI ()
{
var chr : char= Event.current.character;
if ( (chr < '0' || chr > '9') (chr != 'Backspace') (chr != 'Minus'))
{
Event.current.character = '\0';
}
Okay, Now I’ve gotten this far. It’s a little more tedious (if anyone knows an easier way to do this, let me know). But anyways, I’ve got it comparing the array, but it only compares it to the last member of the array, in this case “w”. I can still type “s”, and “a”, which I don’t want to happen. I think it may have to do with the for loop I’m using, but I want the for loop to pbe considered basically every time the user changes a value in the text field. Right now I think the script runs through it all in the beginning, and because “w” is the last value saved by the for-loop, that’s the only one you cannot type. How would I go about fixing this?
private var minNum : int;
private var maxNum : int;
var minString : String = "1";
var maxString : String = "20";
private var badChars = new Array ("s", "a", "w");
private var drawminNum : boolean = true;
function OnGUI ()
{
GUI.BeginGroup (Rect ((Screen.width - Screen.width) + 10, Screen.height - 210, 210, 210));
var newString : String = GUI.TextField(Rect (10, 10, 100, 100), minString);
for (var value : String in badChars)
{
if (newString == value)
{
drawminNum = false;
Debug.Log ("off");
}
else
{
drawminNum = true;
}
}
if (drawminNum == true)
{
minString = newString;
}
GUI.EndGroup ();
}
Well, figured it out this way. Not the easiest, still don’t know the full options for this, but it should work for my purposes. In case anyone wanted it, this is the final code to where I got it working. You must make sure you include the GUI.Changed, or else the event will only be triggered once.
private var minNum : int;
private var maxNum : int;
var minString : String = "1";
var maxString : String = "20";
private var badChars = new Array ("s", "a", "w");
private var drawNum : boolean = true;
function OnGUI ()
{
GUI.BeginGroup (Rect ((Screen.width - Screen.width) + 10, Screen.height - 210, 210, 210));
var newString : String = GUI.TextField(Rect (10, 10, 100, 100), minString);
if (GUI.changed)
{
CheckKeys (newString);
}
if (drawNum == true)
{
minString = newString;
Debug.Log ("type");
}
GUI.EndGroup ();
}
function CheckKeys(input)
{
for (var value : String in badChars)
{
if (input == value)
{
drawNum = false;
Debug.Log ("off");
return;
}
else
{
drawNum = true;
Debug.Log ("on");
}
}
}
Javascript uses an index of a string for a char (since strings are essentially char arrays).
private var integerText = "";
function OnGUI () {
var chr = Event.current.character;
if (chr < "0"[0] || chr > "9"[0]) {
Event.current.character = "\0"[0];
}
integerText = GUILayout.TextField (integerText, GUILayout.Width(100));
}
By the way, you’d pretty much never want to use Array. This:
private var badChars = new Array ("s", "a", "w");
Should be:
private var badChars = ["s", "a", "w"];
Which makes it of type String[ ].
–Eric
Since it is just for integers, building your own simple parser (as demonstrated earlier) is fine.
However in the future, you might want to build functionality around TryParse, which’ll make your life easier than say manually parsing a float.