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
0
Question by davidflynn2 · Nov 02, 2012 at 02:17 PM · c#guitexturetext

C# GUI Button

I am working on a GUI button and I would like to make it so that each of my 6 buttons has a image for them that can be edited in the inspector, I would also like to leave it with text in the buttons. Currently I have the buttons set up with the text but I a unsure how to go about seting up exposed textures for each button and applying it to the button while keeping the text also. Here is the Code I have so far.

 if (GUI.Button(new Rect(30, 70, 50, 30), "Click"))
     {
             
     }
Comment
Add comment · Show 1
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 AlucardJay · Nov 02, 2012 at 02:43 PM 0
Share

http://docs.unity3d.com/Documentation/Components/gui-Basics.html

http://docs.unity3d.com/Documentation/Components/GUIScriptingGuide.html

http://docs.unity3d.com/Documentation/ScriptReference/GUI.html

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by markpdolby · Nov 02, 2012 at 03:05 PM

     [SerializeField] private string buttonText;
     [SerializeField] private Texture buttonTexture;
 
     void OnGUI()
     {
         if (GUI.Button(new Rect(30, 70, 50, 30), new GUIContent(buttonText, buttonTexture)))
         {
             //code here
         }
     }

Using the serialize field parameter you can expose variables to the inspector (another way is to just make the variable public)

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 coastwise · Nov 02, 2012 at 02:37 PM

To have a button with both text and an image you need to use GUIContent (see the very bottom of the GUI Basics page from the docs)

 if (GUI.Button(new Rect(30, 70, 50, 30), new GUIContent("Click", icon))) {
     Debug.Log("button was clicked");
 }

Comment
Add comment · Show 2 · 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 davidflynn2 · Nov 02, 2012 at 02:43 PM 0
Share

Is it possable to expose the text of a button to the inspector so that it can be typed in out there?

I would like it so that if latter I want to change the text of the button I can just do it in the inspector and be done.

avatar image MibZ · Nov 02, 2012 at 03:14 PM 0
Share

The easy answer is: If you only want to be able to change the text in the inspector, you can make a public string variable and put that variable as the text parameter in your GUI code, and then to change it select the GameObject the script is attached to and you will see the string variable in the Inspector.

If you want to create entire GUI elements from the Inspector: With Unity's old GUI system (the one you're currently using) by default there is no Editor support, but you can find a good selection of packages on the Asset Store that have custom inspector windows for making GUI easier to make. (No/less coding)

That said, Unity is working on a brand new much more powerful GUI system that has Inspector windows for Unity 4, but it isn't in the beta version yet.

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

13 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

Related Questions

How to create a texture, fill it with some color, add a text at a center and to set this texture to some object in C#? 0 Answers

How to display text on a cube and how to change it dynamically? 2 Answers

C# How to Drag and Scale with Mouse Window 0 Answers

Changing GUI.Box opacity 3 Answers

Equivelant of GUI.DrawSprite() ? 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