- Home /
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:
Sorry for bad paint mockup, but I'm on my gf laptop and it lacks civilized graphical programs.
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.
}
}
}
GUI.skin.button.alignment = TextAnchor.$$anonymous$$iddleCenter;
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.
}
Then edit the texture in GI$$anonymous$$P, Photoshop, or $$anonymous$$SPaint... Otherwise, make your question more clear.
I apologize if I misunderstand what you are looking for, but I need more to go off of. Have a look HERE.
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.
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....
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?
Your answer
Follow this Question
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