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 darkhog · Sep 23, 2013 at 10:44 PM · guibuttoninventoryinventory systemicons

GUI Button with both texture and text?

I'm currently working for inventory system for my game and to show items I have made icons and would want to put both item name and icon on inventory screen as button.

I'm looking for something like this:

image

Sorry for bad paint mockup, but I'm on my gf laptop and it lacks civilized graphical programs.

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

4 Replies

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

Answer by clunk47 · Sep 23, 2013 at 10:50 PM

Here's a C# example on how to use GUIContent, and how to set the content in GUI.skin for your button.

 using UnityEngine;
 using System.Collections;
 
 public class example : MonoBehaviour
 {
     GUIContent content = new GUIContent();
     public Texture2D image;
     string text = "ButtonText";
     
     void Awake()
     {
         content.image = (Texture2D)image;
         content.text = text;
     }
     
     void OnGUI()
     {
         GUI.skin.button.normal.background = (Texture2D)content.image;
         GUI.skin.button.hover.background = (Texture2D)content.image;
         GUI.skin.button.active.background = (Texture2D)content.image;
         
         if(GUI.Button(new Rect(0, 0, 128, 128), content))
         {
             //Do Something.    
         }
     }
 }
Comment
Add comment · Show 12 · 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 clunk47 · Sep 23, 2013 at 11:03 PM 1
Share

GUI.skin.button.alignment = TextAnchor.$$anonymous$$iddleCenter;

avatar image clunk47 · Sep 23, 2013 at 11:03 PM 1
Share

Or you could just use a public GUIStyle and edit values in the inspector. This works for all content, including text and background.

 public GUIStyle style;
 
 if(GUI.Button(new Rect(0, 0, 128, 128), content, style))
 {
     //Do Something.    
 }

avatar image clunk47 · Sep 23, 2013 at 11:14 PM 1
Share

Then edit the texture in GI$$anonymous$$P, Photoshop, or $$anonymous$$SPaint... Otherwise, make your question more clear.

avatar image clunk47 · Sep 23, 2013 at 11:21 PM 1
Share

I apologize if I misunderstand what you are looking for, but I need more to go off of. Have a look HERE.

avatar image clunk47 · Sep 23, 2013 at 11:35 PM 1
Share

The only way to really do this is to change the background dude.

 using UnityEngine;
 using System.Collections;
 
 public class example : $$anonymous$$onoBehaviour
 {
     GUIContent content = new GUIContent();
     public Texture2D image;
     string text = "ButtonText";
     
     void Awake()
     {
         content.text = text;
     }
     
     void OnGUI()
     {    
         GUI.skin.button.normal.background = (Texture2D)image;
         if(GUI.Button(new Rect(0, 0, 128, 128), content))
         {
             //Do Something.    
         }
     }
 }

Otherwise, you need to draw a GUI Button AND a GUI Label over the button.

Show more comments
avatar image
1

Answer by stalker_23b · Aug 22, 2014 at 11:03 AM

GUI.Button(new GUIContent("text", icon));

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
0

Answer by kayb14 · Sep 01, 2016 at 11:24 PM

Sry I am late, but the solution is so obvious! Since it's impossible!

If I get you right you want to create the standard unity-gui button-layout! A clickable Button with an image, and a text above it?

Well take a closer look at Unity's buttons, their text elemts are all childed gui text. So what you need to do is to create your button and than create a gui-text which you than child to the button to make it inherit the button-behaviour. Else if you'd just overlay it the space where the text is displayed wouldn't be clickable. and I simply don't recommend creating two overlaying buttons, one with an image and the next one with your text. Always remember that order and childing matters in GUI!

hope this may help,

best wishes, kayb14

ps: just stumbled across this problem on my own, so I'll put up the code if I am done figuring it out....

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
0

Answer by DjBledWolf · Feb 06, 2018 at 08:42 AM

is it possible to change an image to the GUIButton by a code?, is it possible to assign an image to the texture2D by a code?

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

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

19 People are following this question.

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

Related Questions

GUI Button to create another GUI 1 Answer

Button being pressed but an other button gets the effect. 0 Answers

GUI grid of buttons issue. 1 Answer

button wont work in GUI.BeginScrollView 0 Answers

GUISkin is overlaying my button image 0 Answers


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