- Home /
OnGUI and Displaying GUI Features
Ok right now I have a vague beginnings of my UI. The buttons get added to the scroll area and then track each button clicked, in order to open a GUI.Window item which I haven't implemented yet.
void OnGUI() {
AutoItemLayout();
for (int i = 0; i < buttons.Count; i++)
{
if (buttons[i])
{
//do something
}
}
}
void AutoItemLayout() { GUI.enabled = windowNotOpen; GUILayout.BeginArea(new Rect(0, 0, SHOP_WIDTH, SHOP_HEIGHT)); GUI.Box(new Rect(0, 0, SHOP_WIDTH, SHOP_HEIGHT), "");
category = GUI.Toolbar(new Rect(5, 5, SHOP_WIDTH - 10, 85), category, categoryName);
GUI.Box(new Rect(5, 95, (SHOP_WIDTH - 10), (SHOP_HEIGHT - 100)), "");
scrollViewVector = GUI.BeginScrollView(new Rect(10, 100, (SHOP_WIDTH - 20), (SHOP_HEIGHT - 110)), scrollViewVector, new Rect(0, 0, 0, scrollHeight));
if (category != currentCategory)
{
buttons.Clear();
switch (category)
{
case 0:
displayItems = clothes;
currentCategory = 0;
break;
case 1:
displayItems = playground;
currentCategory = 1;
break;
case 2:
displayItems = music;
currentCategory = 2;
break;
case 3:
displayItems = zopet;
currentCategory = 3;
break;
default:
Debug.Log("SHOP TOOLBAR BROKE");
break;
}
setUpShopLayout();
for (int i = 0; i < buttonPositions.Count; i++)
{
buttons.Add(GUI.Button(buttonPositions[i], displayItems[i].ItemName));
}
}
GUI.EndScrollView();
GUILayout.EndArea();
GUI.enabled = true;
}
public void setUpShopLayout() { buttonPositions.Clear(); count = 0; noExtraItems = true; total = displayItems.Count;
lessThan = (int)(SHOP_WIDTH - 35) / (buttonWidth); ;
buttonBuffer = (int)(SHOP_WIDTH - 35 - (buttonWidth * lessThan)) / lessThan;
scrollHeight = ((total / lessThan) * (buttonHeight + buttonBuffer));
numrows = total / lessThan;
if ((total % lessThan) > 0)
{
extraItems = total % lessThan;
numrows++;
scrollHeight = scrollHeight + (buttonHeight + buttonBuffer);
noExtraItems = false;
}
for (int i = 0; i < (numrows); i++)
{
if ((i + 1) < numrows || noExtraItems)
{
for (int j = 0; j < lessThan; j++)
{
name = displayItems[count].ItemName;
buttonPositions.Add(new Rect((j * (buttonWidth + buttonBuffer)), (i * (buttonHeight + buttonBuffer)), buttonWidth, buttonHeight));
count++;
}
}
else
{
for (int j = 0; j < extraItems; j++)
{
name = displayItems[count].ItemName;
buttonPositions.Add(new Rect((j * (buttonWidth + buttonBuffer)), (i * (buttonHeight + buttonBuffer)), buttonWidth, buttonHeight));
count++;
}
}
}
}
That's the main chunk of my code. However the loop, that adds the GUI.Button to the buttons array, is infinity adding buttons outside of the if(category != currentCategory) statement, and inside the if statement it adds the correct number of buttons, however they do not appear on the UI. Any ideas?
Thanks, Hans Unity Newb
Answer by Bunny83 · Apr 28, 2011 at 05:55 PM
Unity uses an immediate GUI system. That means ObGUI is called every frame and you have to draw your GUI every frame. You can't create a GUI and just display it. At the moment you stop calling GUI.Button() the button will disappear.
Also take a look at GUILayout that provides the same elements as GUI but the size and position is determined automatically. You can use horizontal or vertical groups to design your layout. Those groups can be nested. With GUILayoutOptions like Width you still can influence the layouting
Thanks much more elegant than my current method. Greatly appreciated.
I just realised that you already mixed GUI and GUILayout. But that makes no sense at all. If you want a relative area for the normal GUI stuff use GUI.BeginGroup.
Jeepers, Unity just has so much stuff to get used to, hard to keep track of it all. Thanks again for the help.
No problem ;) Unity gets quite handy once you figured out how it works...