- Home /
GUI.Button Functionality which button is clicked
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!
Answer by Joshua · Apr 28, 2011 at 01:27 PM
You're absolutely right you can access them by their name! I have no experience with this method, but I assume it'll work, if not - I apologies. Start of by adding an (empty) array which will contain all the buttons.
var buttons : Array = new Array ();
whenever you create a button you'll add it to the array:
for(stuff) {
newButton = GUI.Button(new Rect((j * (buttonWidth + buttonBuffer)), (i * (buttonHeight + buttonBuffer)), buttonWidth, buttonHeight), name);
buttons.Add(newButton) //because of your two loops the location in the array will be(i+1)*(lessThan-1)*j or if I'm correct. Edit: I see you have the count var keeping track of this.
}
Now you can reference each single button by looping through this array.
for(k=0;k
if(buttons[k]) {
openItemWindow(k, name);
}
As I said, untested, but I think it'll work.
Good luck, and for someone who is "new to unity" it looks like you're doing great!
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.
I 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.
Ok 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
Your answer
Follow this Question
Related Questions
Precise Timing 2 Answers
Changing SpriteRenderer color with UnityEvent.Event 1 Answer
How to change a Player direction by colliding a Game Object 1 Answer
IPointerClickHandler works until I add IPointerDownHandler 0 Answers
How to create an in game search engine? And how to select the searched object? 0 Answers