Is there a simple way to create a GUI numeric stepper? I need it for setting the matrix sizes in logical games.
There isn’t a built-in function for that, no. Sounds like you have this sorted anyway, but for reference, you can make your own function. It would be nicer if there was a GUI.NumberField, but there isn’t, so you pretty much have to make do with strings. Also you’d probably want to use some nice up/down textures for the buttons, instead of “^” and “V”.
function Stepper (position : Rect, text : String) : String {
var size = position.height;
text = GUI.TextField(Rect(position.x+size+2, position.y, position.width-size*2-4, size), text);
var textValue = 0;
var parsed = int.TryParse(text, textValue);
if (GUI.Button(Rect(position.x, position.y, size, size), "V") ) {
textValue--;
}
if (GUI.Button(Rect(position.x+(position.width-size), position.y, size, size), "^") ) {
textValue++;
}
if (parsed) text = textValue.ToString();
return text;
}
Which you use like this:
private var something = "0";
function OnGUI () {
something = Stepper(Rect(10, 10, 100, 25), something);
}
Or, an alternate version:
function Stepper (position : Rect, text : String) : String {
var size = position.height;
text = GUI.TextField(Rect(position.x, position.y, position.width-size-2, size), text);
var textValue = 0;
var parsed = int.TryParse(text, textValue);
if (GUI.Button(Rect(position.x+(position.width-size), position.y, size, size/2), "^") ) {
textValue++;
}
if (GUI.Button(Rect(position.x+(position.width-size), position.y+size/2, size, size/2), "V") ) {
textValue--;
}
if (parsed) text = textValue.ToString();
return text;
}
If would not use a GUI you could use for loop like this.
var nextPositionX : int =10;
var nextPositionY : int =10;
var gridBox : GUITexture[];
function CreateGrid(){
for (var i = 0; i < gridBox.length; i++)
{
gridBox*.pixelInset = Rect(nextPositionX,nextPositionY,100,100);*
nextPositionY += 110;
if(i == 5 || i == 10)
nextPositionX += 110;
}
}
but if you need to use GUI then you have to find another way.