- Home /
Is there any easy way to keep buttons inside of box? (c#)
I made an inventory script, but I would love for it to auto-determine how many buttons can fit in a row, and put them on the next row. Is there any way?
Edit: even not "auto determining" but if I say that 2 buttons fit on each row, I would like it to auto-move the next set to the next row and PREFERABLY resize them.
Here is the important part of my current script:
GUI.BeginGroup (new Rect(Screen.width/2 +100, Screen.height - 300, 400, 100));
GUI.Box (new Rect(0,0,400,100), "Weapons");
foreach(GameObject inv in inventory)
{
if(invcount < inventory.Count)
{
invcount ++;
}
if(GUI.Button (new Rect(10 + (invcount - 1) * 205,20,200,30),new GUIContent(inv.name, "Damage: " + inventory[invcount-1].GetComponent<Item>().dmg)))
{
Debug.Log ("equipping" + inv.name);
if(hasCloned)
{
Destroy (clone1);
}
toUpdate = true;
hasCloned = true;
if (toUpdate)
{
clone1 = Instantiate(inv, WepHolder.transform.position, WepHolder.transform.rotation) as GameObject;
clone1.transform.parent = GameObject.Find ("Main Camera").transform;
clone1.SetActive (true);
Debug.Log ("You are now using: " + inv.name);
clone1.tag = "weaponEquipped";
toUpdate = false;
}
}
GUI.Label (new Rect(10 + (invcount - 1) * 205, 70, 100, 30), GUI.tooltip);
GUI.tooltip = null;
}
So to clarify, you have some number of buttons and you want to have it draw them automatically, such that n items are on each row, and then it draws the next row?
preferably I'd like it to see how large inventory.Count is, and make room for (for example) x rows of 2 buttons, but, I'd even be fine with manually specifying how much room is needed and it just automatically puts the buttons in rows of 2. I was thinking a switch or something, but that seems like a lot of work
Ah! So you always want rows of size 2 and you want to draw as many rows as you need?
yes! Exactly! As I said, the switch statement seems a bit more than it could need, although I could be wrong.
I am absolutely horrible at this UI stuff, it took me like 3 days to man up and just make the inventory system.
Answer by perchik · Jul 19, 2013 at 06:34 PM
You could draw your buttons in a loop and then after [size of row] objects, change the y value.
If you were using GUILayout, it'd be something like this:
int size = inventory.Count
int rowLen = 5;
GUILayout.BeginVertical();
GUILayout.BeginHorizontal();
for(int i=0; i<size; i++){
GUILayout.Button( content);
if( (i+1)%rowLen == 0){ //if the next object needs to be on a new row
GUILayout.EndHorizontal(); //end row
GUILayout.BeginHorizontal(); //create new row
}
}
GUILayout.EndHorizontal();
GUILayout.EndVeritcal();
If you're not using GUILayout, you'll have to do something a little more fancy like this:
int xPos = 10 ; //xpos of first button
int yPos = 10; //ypos of first row;
int xSpacing = 5; //horiz distance between buttons
int ySpacing = 5; //vert distance between buttons
int buttonW = 50;
int buttonH = 10;
int size = inventory.Count;
int rowLen = 5; //number of items per row
int currX = xPos;
int currY = yPos;
for(int i=0; i<size; i++){
GUI.Button(new Rect(currX, currY, buttonW, buttonH);
currX += xSpacing;
if( (i+1)%rowLen == 0 )
currY += ySpacing;
}
Slight Caveat Don't copy this code verbatim. No clue if it actually works or is the correct syntax, but should be pretty close. I wrote this off the top of my head without Unity around
I would recommend looking heavily at GUILayout unless you have a reason to hardcode widths and heights
Wow...I wish I had known about this GUIlayout. I'm trying to help a friend right now, but that looks SO much easier.
The only trick with the GUILayout is that BeginVertical and BeginHorizontal are somewhat confusingly named. Begin Vertical creates things in a column, Begin Horizontal creates things in a row
I'll have to figure the GUILayout thing later. I'm having trouble trying to carry over my current GUIContent stuff over to it.