Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 4illeen · Aug 13, 2011 at 08:21 PM · guibuttonguicontent

More elements inside GUI button

alt text

This is my inventory screen. To display items I use buttons with GUIContent(WeaponName, WeaponIcon). Problem is I need more stuff inside the buttons (texture Icon,string Name, texture Coin Icon,int Price,texture Stone Icon,float Weight). Is it possible to add all of those inside the button, so it's still clickable? Is there any way to tell unity WHERE inside the button to show things mentioned above? Like having the icon on the left side, not centered.

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
2

Answer by Bunny83 · Aug 13, 2011 at 11:28 PM

I don't want to encourage people to dig to deep into foreign code, but i guess the UnityEngine.dll is not a big secret since it's all managed code ;). ILSpy gives me this:

 public static bool Button(Rect position, GUIContent content, GUIStyle style)
 {
     GUIUtility.CheckOnGUI();
     int controlID = GUIUtility.GetControlID(GUI.buttonHash, FocusType.Native, position);
     switch (Event.current.GetTypeForControl(controlID))
     {
         case EventType.MouseDown:
         {
             if (position.Contains(Event.current.mousePosition))
             {
                 GUIUtility.hotControl = controlID;
                 Event.current.Use();
             }
             return false;
         }
         case EventType.MouseUp:
         {
             if (GUIUtility.hotControl == controlID)
             {
                 GUIUtility.hotControl = 0;
                 Event.current.Use();
                 return position.Contains(Event.current.mousePosition);
             }
             return false;
         }
         case EventType.MouseDrag:
         {
             if (GUIUtility.hotControl == controlID)
             {
                 Event.current.Use();
             }
             break;
         }
         case EventType.Repaint:
         {
             style.Draw(position, content, controlID);
             break;
         }
     }
     return false;
 }

As you can see the magic behind the GUI.Button function is not black-magic. The whole GUI system is build on top of styles. Styles are almost the only things that can be drawn in the GUI system. A GUIStyle supports only 1 background image, 1 content image and the content-text. More complex GUI-elements like a Scrollview for example uses multiple styles. Each scrollbar uses 4 different styles (background, up-button, down-button, thumb). Those more complex elements just uses other simpler elements internally.

You can build more complex functions on your own. The GUI system is that simple to provide the best extensibility.

You can learn a lot about the GUI system by using ILSpy or any other .NET reflector.

Comment
Add comment · 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
1

Answer by Julien-Lynge · Aug 13, 2011 at 08:48 PM

Buttons are far too simple for what you're trying to do. I can't say I'm any expert at GUI, but you probably want to create a GUI Group (see GUI.BeginGroup) and then layout your group as you wish. To make it 'clickable', what I would do is, within the OnGUI() function, do something like:

 Vector2 clickPos = Input.mousePosition;
 clickPos.y = Screen.height - clickPos.y;
 if (Input.GetMouseDown(0)) //user clicked
 {
   foreach(Rect button in myButtons)
   {
     if (button.Contains(clickPos))
       //user clicked on this button, do something
   }
 }


Comment
Add comment · Show 7 · 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 4illeen · Aug 13, 2011 at 08:57 PM 0
Share

so it's not possible to add more than 1 texture and 1 string inside the button?

avatar image Julien-Lynge · Aug 13, 2011 at 09:02 PM 0
Share

Yes, a button is more of a primitive type than an extensible object. You could try doing creative things like painting additional labels over the button and seeing if you're still able to click the button through the labels, but the button object itself can only have 1 texture and 1 string.

avatar image 4illeen · Aug 13, 2011 at 09:21 PM 1
Share

I made a GUI.Label with the icon over the button and I was still able to click it normally. I guess I'll have to make an empty button and all the stuff I want over it.

avatar image Julien-Lynge · Aug 13, 2011 at 09:26 PM 0
Share

GUI.BeginGroup will be your friend if you're doing that. Create a Rect to place your button, then a group using the Rect, then all the other textures and text as a percentage of this Rect, with x,y = 0,0 corresponding to the top left (or just use the layout stuff I linked earlier). That way, you can later resize your button and everything within it will resize as well.

avatar image Remo · Aug 24, 2012 at 07:28 AM 0
Share

But what is the use of GUI.BeginGroup if simply drawing a label on top of the button does the work?? Could you please add a code snippet with GUI.BeginGroup(), as I have encountered the same problem?

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

GUIContent renders button elements different from intended 0 Answers

GUI.Button background image 0 Answers

GUI Button Jagged Edges JavaScript 0 Answers

Problem with texture to reset and re-enter the same texture 0 Answers

GUI Button sound problem, don´t work 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