Hello,
I'm new to unity and have hit a snag and I'm hoping that someone can help me out.
I'm trying to create a group of buttons the number of buttons will vary throughout the life time of the application. I'm receiving the total number of buttons I'm going to need at anyone time and calculating from the size of the buttons the correct amount of room for the scroll area they are in. This is all working peachy, what I want to do now is when the user clicks one of the buttons in the scroll area, is open a pop-up window. But I don't know how to access which button is being clicked i have a feeling that because I'm creating my buttons with a for loops I have now no way of accessing their events after the fact.
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);
}
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));
for (int i = 0; i < (numrows); i++)
{
if ((i + 1) < numrows)
{
for (int j = 0; j < lessThan; j++)
{
name = displayItems[count].ItemName;
if (GUI.Button(new Rect((j * (buttonWidth + buttonBuffer)), (i * (buttonHeight + buttonBuffer)), buttonWidth, buttonHeight), name))
{
openItemWindow(count, name);
}
count++;
}
}
else
{
for (int j = 0; j < extraItems; j++)
{
name = displayItems[count].ItemName;
GUI.Button(new Rect((j * (buttonWidth + buttonBuffer)), (i * (buttonHeight + buttonBuffer)), buttonWidth, buttonHeight), name);
count++;
}
}
}
GUI.EndScrollView();
GUILayout.EndArea();
This is the part of code that creates the button grid and since unlike Java where you create a button and can access it by name I'm not sure how to do in with a Unity GUI.Button.
Any help would be greatly appreciated!
Thanks a bunch! It works wonderfully, only been using Unity for about a week, but it's incredible. Just trying to learn everything it can do. Another question slightly off topic, would I be able to access any of the buttons properties? ie. position, or name. The array is an array of boolean values right or is there a way for me to create an array of actual button objects, and I have just overlooked it.
– HHamelineI think it's indeed an array of booleans. You could 'fake' an array of buttons by having this array of booleans be constructed out of an array of Rect's, strings, etc that make up the button and then constantly update the value of each boolean. But for many buttons this would mean looping through each property value array every frame, which is a bit much.
– anon73820239Ok thanks, I was going to try to change the position of the GUI.Window element dynamically based on the buttons positions and was hoping I could easily reference the buttons themselves. Thanks a lot for the help. I will now go tinker with my code some more :D
– HHameline