- Home /
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:
And I want:
Can anyone help me figure out how to get the GUI texture to fill in the button.
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
How do I make it so that only one button gets affected by it?
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...
}
}
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"
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613133223im_/https://answers.unity.com/themes/thub/images/avi.jpg)