x


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?

more ▼

asked Aug 23 '10 at 04:13 PM

Aequitas gravatar image

Aequitas
29 6 7 9

You should probably use a scrollview.

Aug 23 '10 at 10:49 PM qJake
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.

more ▼

answered Sep 08 '10 at 07:26 AM

Aequitas gravatar image

Aequitas
29 6 7 9

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Aug 24 '10 at 05:40 AM

DaveA gravatar image

DaveA
26.8k 153 171 257

I'm already using Scrollview. My 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.

Aug 25 '10 at 08:44 AM Aequitas

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.

Aug 25 '10 at 07:30 PM DaveA

Just use GUILayout.Box instead of GUI.Box, that will automatically position each item in its own row without a Rect

Sep 07 '10 at 12:51 PM spinaljack
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Sep 07 '10 at 12:39 PM

james flowerdew gravatar image

james flowerdew
37 3 4 8

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3815
x915
x820
x35

asked: Aug 23 '10 at 04:13 PM

Seen: 1156 times

Last Updated: Aug 23 '10 at 04:13 PM