I am learning and teaching myself scripting with unity, but ran into a brick wall when trying to figure out how to force a player to only enter numbers into a gui textfield.
Google and the forums here had some ideas, but all were long and confusing for something that I felt should be fairly simple. I finally figured it out, so thought I would share here in case any others starting out run into the same wall I did.
function stringIsInt(testString:String) {
try {
testString = "" + parseInt(testString);
}
catch(err) {
testString = "0";
}
return testString;
}
What this does is take the string (text entered by the user in the textfield) and try to convert it to an integer, and then overwrite the string with that number.
If it is not able to do this, then it will overwrite the string with the number β0β.
With that function in place all you need to do for your textfield is this,
myNumberTextField = GUI.TextField(Rect (10,10,25,100),myNumberTextField);
myNumberTextField = stringIsInt(myNumberTextField);
This places the textfield on your screen and puts the user input into the string myNumberTextField.
Then it enters that string into the function you just wrote and overwrites it with the results, which in turn update the text in the field.
So, if the user enters the following text, this is what will be displayed. (in the same box they are typing in, so if they type something not allowed it is changed instantly)
β00β = β0β
β01β = β1β
β1β = β1β
βasdfβ = β0β
β12dfβ = β0β
βdf12β = β0β
This worked perfectly for what I was needing, but then I thought, βwhat if I wanted a different default number than β0β? or what if I wanted to limit the range of numbers allowed?β
So I refined it and added some more options,
function stringIsInt(testString:String) {
try {
testString = "" + parseInt(testString);
}
catch(err) {
testString = "0";
}
return testString;
}
function stringIsInt(testString:String,defaultNum:int,minNumber:int,maxNumber:int) {
if (defaultNum < minNumber || defaultNum > maxNumber) {
defaultNum = 0;
minNumber = 0;
maxNumber = 0;
}
try {
testString = "" + parseInt(testString);
}
catch(err) {
testString = defaultNum.ToString();
}
if (!(minNumber <= parseInt(testString) parseInt(testString) <= maxNumber)) {
testString = defaultNum.ToString();
}
return testString;
}
Now you can use this function as I originally intended with the default number it resets to as β0β and no limit to the range, or you can define the default and range.
The second option here is a little more complex, but still fairly simple.
First it checks if the default number entered falls within the range specified it sets the default, min and max all to 0.
Then it does the same check as before to see if it can be converted to an integer.
This time though, if it can not be set to an integer, it sets it to the specified default number instead of β0β like we did earlier.
Then it checks to see if the number falls within the range specified, and if not sets it to default.
I am just learning, so if any experts have comments on how to do this better I am open to suggestions. I had a hard time googling this though so wanted to get it out here for discussion or reference.
have fun
narfi