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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

C# how to create a Descending GUI List 1 Answer

Unity 4.6 GUI vs Unity 4.5 GUI 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges