Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by SomeRandomGuy · Dec 25, 2011 at 01:17 PM · guibuttonvariable

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SomeRandomGuy · Dec 25, 2011 at 04:47 PM 0
Share

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

avatar image Simon V · Dec 25, 2011 at 05:16 PM 0
Share

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

avatar image Rabwin · Dec 26, 2011 at 06:48 AM 1
Share

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

avatar image SomeRandomGuy · Dec 26, 2011 at 11:02 AM 0
Share

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

avatar image SomeRandomGuy · Dec 26, 2011 at 10:04 PM 0
Share

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.

Show more comments
avatar image
0

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SomeRandomGuy · Dec 25, 2011 at 01:52 PM 0
Share

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

avatar image Rabwin · Dec 25, 2011 at 02:14 PM 0
Share

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.

avatar image SomeRandomGuy · Dec 25, 2011 at 03:15 PM 0
Share

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.

avatar image Rabwin · Dec 25, 2011 at 03:30 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Expecting variable error with a button 1 Answer

Push GUI button, change another objects variable 2 Answers

Is there any way to move a GUI.Button across the screen (when clicked) with a script? 1 Answer

Button does not display, need urgent help! 1 Answer

Gui Playing Audio Trouble 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges