- Home /
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"))
{
}
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)
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");
}
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.
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
Follow this Question
Related Questions
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