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 mmangual_83 · Feb 11, 2014 at 06:56 PM · c#guigui-button

Adding a texture to a GUI button

How do I add a texture to a button that fills the entire button and not just a portion of it?

Right now I have my code set up as:

         if (drawButon(btn_Begin, new GUIContent("Begin", _ButtonTexture)))
         {
             //Logic here...
         }

// I created this method to simplify the creation of GUI buttons. All I have to do is call it and pass the necessary values into the parameters bool drawButon(Rect rect, GUIContent content) {

         if (GUI.Button(new Rect(GUIRectWindow.x + rect.x, GUIRectWindow.y + rect.y, rect.width, rect.height), content))
         {
             return true;
         }
         return false;
     }


The GUI button width and height is 100 x 50 and the texture I created is the same width and height so I figured it would fit just fine but All I get is:

alt text

And I want:

alt text

Can anyone help me figure out how to get the GUI texture to fill in the button.

button.png (1.6 kB)
button error.png (3.9 kB)
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

1 Reply

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

Answer by iamvishnusankar · Feb 11, 2014 at 07:03 PM

Use GUIStyle to adjust your buttonStyle. And add your image as Normal background on GUIstyle. Adjust your button texture style on GUIStyle settings on the Inspector Menu

public GUIStyle _myCustomstyle;

Void OnGUI()

{

GUI.Skin.Button = _myCustomstyle;

if (drawButon(btn_Begin, new GUIContent("Begin", "")))

     {
         //Logic here...
     }


}

For More on GUIStyles : http://docs.unity3d.com/Documentation/ScriptReference/GUIStyle.html

Comment
Add comment · Show 6 · 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 mmangual_83 · Feb 11, 2014 at 07:15 PM 0
Share

That worked thanks!

avatar image mmangual_83 · Feb 11, 2014 at 07:21 PM 0
Share

How do I make it so that only one button gets affected by it?

avatar image iamvishnusankar · Feb 11, 2014 at 07:28 PM 0
Share

Define your custom GUIStyle at the last portion ,Or define seperate GUIStyle for each button "JUST ABOVE THE BUTTON DEFINITION".

Void OnGUI()

{ //All other Button Definitions

//GUI.Skin.Button = _myStyle1;

//here is button1

//GUI.Skin.Button = _myStyle2;

//here is button2

//.....

//GuiStyle I want for your "needed" button

GUI.Skin.Button = _myCustomstyle;

if (drawButon(btn_Begin, new GUIContent("Begin", "")))

 {
     //Logic here...
 }

}

avatar image mmangual_83 · Feb 11, 2014 at 07:40 PM 0
Share

I went ahead and defined it at the end like so:

         GUIStyle _myButtonStyle;//the style for the button        
         if (drawButon(btn_Begin, _ButtonTexture))
         {
            //Logic here...
         }
         GUI.skin.button = _myButtonStyle;


But for some reason its giving me an error saying something about unassigned local variable "_myButtonStyle"

avatar image iamvishnusankar · Feb 11, 2014 at 08:01 PM 1
Share

No matter how many separate classes are being used , Ultimately a player is seeing things on screen. That's the Graphical User Interface (GUI). GUIStyle will inherit the properties of the Above Defined GUIStyles. So , according to my knowledge, I believe you will have to define seperate GUIStyles if needed.

For more clarification, I recommend you to check Unity Documentation on GUISTyle

Link : http://docs.unity3d.com/Documentation/ScriptReference/GUIStyle.html

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

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

Enable and disable a button selection 3 Answers

How to loop an array of 2d sprites 1 Answer

How to dynamically change the text in Unity(Augmented Reality + NYARtoolkit(C#)) ? 0 Answers

A node in a childnode? 1 Answer

C# how to create a Descending GUI List 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