- Home /
Adding an extra variable to GUI buttons?
The question is quite simple, Is it possible to add extra variables to a button, for example, if I have multiple buttons, could I add some sort of ID for each button? I'm asking this since I am trying to make some sort of an inventory system and what I have in mind would work perfectly if I'd be able to access buttons individually by IDs.
EDIT: here's the code I already have:
var Icon : Texture2D = null;
var invOffset : float = 50;
var windowRect : Rect = Rect (10, 10, (200), (200));
var invButton : boolean = false;
function OnGUI ()
{
invButton = GUI.Toggle(Rect((Screen.width/2),(Screen.height-20),100,20),invButton, "Inventory");
if(invButton)
{
GUI.Window (0, windowRect, makeSlots, "Inventory");
}
}
function makeSlots ()
{
for(var x : int = 0; x < 5; x++)
{
for(var y : int = 0; y < 5; y++)
if (GUI.Button (Rect (x*40,y*40,30,30), Icon))
{
print ("clicked the button on row and column: " +(y+1) +(x+1));
}
}
GUI.DragWindow (Rect (0,0,10000,10000));
}
So basically all that I would need to have is a list of all items, and a way to see in which slot they are(if in any). Or at least thats all I think I need...
Answer by Simon V · Dec 25, 2011 at 04:02 PM
Create a custom button class, that inherits from the basic unity button class
Then in this class add the extra functionality you would like, for example the item that the button holds. You should then use this new class instead of the basic unity button class.
The new class could look like this (I'm unfamiliar with JavaScript, so I'll write it in C#):
public class CustomButton : GUI.Button { Item item = new Item() //This adds an Item class to the CustomButton class. int buttonID; //This is the ID you wanted.
void displayIcon()
{
//Add some code to display the icon.
}
//add any other logic you might need.
}
Then you'd best create an array of CustomButtons to hold the required number of buttons.
Well that would be exactly what I want, but I never really worked with classes before(am still quite new to program$$anonymous$$g as a whole, have never programmed anything at all up until about 2 years ago, and class isn't going very fast :|) So I'll have to look at how that works, but thanks for the answer, now I know what I should learn :D
I know the feeling :D I'll add a little how-to to my answer.
I can't believe I missed something as simple as this, you learn something everyday (or at least get re$$anonymous$$ded of something cool).
Wow, seems pretty simple, and from what I can see not all that different from javascript. Thanks a lot, this will definitely help a lot for the thing I'm trying to make now, and probably will be a good thing to know later on aswell!:D
Little update: seems classes work slightly different in javascript, I can't seem to inherit from a function inside a class, or at least, I haven't found a way to do so. Other than that I got most of the class thing to work properly I suppose.
Answer by Rabwin · Dec 25, 2011 at 01:27 PM
You could create an array of buttons I suppose:
int buttonAmount = 1;
Rect buttonPos = new Rect(0,0,50,50);
for(int i=0; i < buttonAmount; i++)
{
buttonPos.height+=55;
if(GUI.Button(buttonPos,"button " + i))
{
switch(i)
{
case 1:
//set a variable or call a function?
break;
default:
//huh? maybe a button wasn't pressed
}
}
Leave a comment! I'd be glad to come back and try to help if this wasn't the answer you were looking for.
Hmm I already have something quite similar to this, except for the switch(i) and below, however, this way I can still not access the buttons themselves, only the variable which places the button, right? I'll post the code I got in the original question, might be of some help aswell.8D
What do you mean access the buttons?
Do you want to modify the buttons after they're pressed? You can create an array of Rect and store button info that way. If you need to change the button name, you could make a struct which stores Rect and a string and make an array of that.
Or you can sort the button id for which was clicked in your original code. Ins$$anonymous$$d of having a print, you can use that data (the x and y) and send it to a method to perform an action or something.
Hmm yea, the main thing I would like to access the buttons for would be to be able to change their content, so I can have little icons of my items displayed on them when they have been clicked.
Perhaps "ID" isn't the right way to call it, rather something similar to transform.rotation.y, but in this case something like button(i).content = something.
$$anonymous$$aybe this whole thing is still a bit too complicated for me, maybe I should watch some tutorials to see how others do it.
How about an array of booleans to check if they were pressed? You could then create an if statement to either draw the normal button or the other image and anything else you wanted to change.
Your answer
![](https://koobas.hobune.stream/wayback/20220613062354im_/https://answers.unity.com/themes/thub/images/avi.jpg)