- Home /
Infinite GUI Button positions.
Basically I'm pulling data off a database and displaying said information if you click on buttons. However there is an infinite number of objects that are stored inside this database. I know how to position objects in a GUI if there are a finite number of them, but how would I achieve something like this with an infinite number of them? How could I set the X and Y positions for each new button?
Answer by Aequitas · Sep 08, 2010 at 07:26 AM
There was quite a simple solution to this one, just using a for loop, and incrementing the X and Y dimension variables. I thought it would effect all the buttons in the loop, but I was wrong.
Answer by DaveA · Aug 24, 2010 at 05:40 AM
Scrollview as SpikeX said, but if there is really a LOT of them, it may be too many for one list. Can you structure the number of objects to choose from with some criteria to shorten the list? For example 'Starting with the letter A' or 'the shortest ones' or some other way of dividing up the list? Thus you might have 2 or more UI objects (say, one scrollview for 'narrow the search' and another for 'pick from those found').
Data visualization is a big field of study, worth googling. I've found that making hierarchies (trees, for example) that hide/show their children works pretty well. Then you can make them 'share the screen' by not all being on screen at the same time.
I'm already using Scrollview. $$anonymous$$y questions wasn't relating to that however. I'm talking about giving each button a new width in the window, otherwise they will all display on top of each other after the for loop has finished.
GUI.Button can take a Rect as the placement (left,top,width,height), so if you want to lay them out as a 2D grid, decide the aspect ratio (how many rows/columns, for square it would be sqrt(count_of_items) roughly) then divide the available screen by number of rows and columns, that will give width & height. You'll need to pad for margins and general good-looking-ness. OnGUI() executes a lot, so you can change those size/positions any time and they will re-layout for you.
Just use GUILayout.Box ins$$anonymous$$d of GUI.Box, that will automatically position each item in its own row without a Rect
Answer by james flowerdew · Sep 07, 2010 at 12:39 PM
I think I know what you want, and the answer is to use maths to set the coordinates of the button instead of hard and fast values. a "for" loop with incrementing numbers is our chosen method, as long as the list doesn't get too big, the code below takes an array of string choices called pchoices (set elsewhere) and builds buttons on incrementing coordinates. I use the "vButChosen" Variable to execute the button action in an update.
int vMakeRoom=20;
int vButHeight=Screen.height/15;
int vButWidth=(int)(vButHeight*6.1f);
int vButPadding = vButHeight/5;
int vButPlaceX=Screen.width-vButWidth-vButPadding;
int vButPlaceY=Screen.height-(pChoices.Length*(vButHeight+vButPadding));
Rect vLoc;
vButChosen="Null";
for (int vBut=0;vBut<pChoices.Length;vBut++){
vLoc=new Rect(vButPlaceX,vButPlaceY+vBut*(vButHeight+vButPadding),vButWidth,vButHeight);
if(GUI.Button(vLoc,pChoices[vBut])){
vButChosen=pChoices[vBut];
}
}
}
I know that this is a bit long winded, but does it help?
Cheers,
James
Your answer
Follow this Question
Related Questions
GUI positioning 1 Answer
Change GUI button position in code 1 Answer
GUI Button and a gameObject's position 3 Answers
Question about positioning GUI. 1 Answer
Button position 4 Answers