- Home /
make texfield accept numers only
how do i make a textfield accept numbers only, tried other code but i couldn't get it to work. thanks
var information: String;
private var guiOn = false;
private var rect: Rect;
var btnTexture : Texture;
var ClosePostion : Vector2 = new Vector2 (312,5);
var CloseSize : Vector2 = new Vector2 (35,35);
var CloseIcon : Texture;
var stringToEdit : String = "";
//click to open gui
function OnMouseDown(){
guiOn = true;
rect = Rect(Input.mousePosition.x, Input.mousePosition.y, 300, 100);
yield WaitForSeconds(500);
guiOn = false;
}
function OnGUI(){
if (guiOn){
GUI.Box(Rect(0,0,200,200),"Current bid");
if (!btnTexture) {
Debug.LogError("Assign a texture");
return;
}
if (GUI.Button(Rect(10,10,50,50),btnTexture))
Debug.Log("Image Button");
if (GUI.Button(Rect(10,70,50,30),"Bid"))
Debug.Log("Text button");
//close button
if (GUI.Button(Rect(ClosePostion.x,ClosePostion.y,CloseSize.x,CloseSize.y),CloseIcon))
{
guiOn = false;
}
stringToEdit = GUI.TextField (Rect (10, 130, 200, 20), stringToEdit, 25);
}
}
Comment
thank you thats solved that problem, how do you link a button to the fieldtext and have that value stored?
Based on a GUI.Button() call you can use int.TryParse() to convert the string to an integer.
still a little puzzled on how to get the textfield and button working together
Best Answer
Answer by robertbu · Aug 06, 2013 at 12:09 AM
Here is another way to only enter numbers. I'm not sure it is "sanctioned" method. I just tried it out and thought it might work:
#pragma strict
var text = "";
var zero = "0"[0];
var nine = "9"[0];
function OnGUI() {
var e = Event.current;
if (e.isKey) {
if (e.character < zero || e.character > nine)
e.character = 0;
}
text = GUI.TextField(Rect(0,0,100,50), text);
}
It would be simpler to write in C# since Javascript does not support character literals.