x


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...

more ▼

asked Dec 25 '11 at 01:17 PM

SomeRandomGuy gravatar image

SomeRandomGuy
152 14 28 30

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

2 answers: sort voted first

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.

more ▼

answered Dec 25 '11 at 04:02 PM

Simon V gravatar image

Simon V
138 16 22 28

Well that would be exactly what I want, but I never really worked with classes before(am still quite new to programming 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

Dec 25 '11 at 04:47 PM SomeRandomGuy

I know the feeling :D I'll add a little how-to to my answer.

Dec 25 '11 at 05:16 PM Simon V

I can't believe I missed something as simple as this, you learn something everyday (or at least get reminded of something cool).

Dec 26 '11 at 06:48 AM Rabwin

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

Dec 26 '11 at 11:02 AM SomeRandomGuy

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.

Dec 26 '11 at 10:04 PM SomeRandomGuy
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Dec 25 '11 at 01:27 PM

Rabwin gravatar image

Rabwin
389 11 15 27

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

Dec 25 '11 at 01:52 PM SomeRandomGuy

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. Instead 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.

Dec 25 '11 at 02:14 PM Rabwin

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.

Maybe this whole thing is still a bit too complicated for me, maybe I should watch some tutorials to see how others do it.

Dec 25 '11 at 03:15 PM SomeRandomGuy

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.

Dec 25 '11 at 03:30 PM Rabwin
(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:

x3813
x850
x820

asked: Dec 25 '11 at 01:17 PM

Seen: 1223 times

Last Updated: Dec 26 '11 at 10:48 PM