x


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"))
    {

    }
more ▼

asked Nov 02, 2012 at 02:17 PM

davidflynn2 gravatar image

davidflynn2
419 196 238 266

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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");
}
more ▼

answered Nov 02, 2012 at 02:37 PM

coastwise gravatar image

coastwise
477 10 11 23

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.

Nov 02, 2012 at 02:43 PM davidflynn2

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.

Nov 02, 2012 at 03:14 PM MibZ
(comments are locked)
10|3000 characters needed characters left
    [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)

more ▼

answered Nov 02, 2012 at 03:05 PM

markpdolby gravatar image

markpdolby
594 2 8

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x9881
x6706
x5794
x3543
x892

asked: Nov 02, 2012 at 02:17 PM

Seen: 8677 times

Last Updated: Nov 02, 2012 at 03:14 PM