x


GUI Button using .png image?

I want to create a GUI button with a .png image I imported to my game assets folder. It is 100x30.

more ▼

asked Nov 21 '10 at 06:09 AM

Jose gravatar image

Jose
42 1 1 3

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

1 answer: sort voted first

If you're using OnGUI for this, you can either make a direct texture reference in your script, and use that value in the GUI.Button command:

var btnTexture : Texture;
function OnGUI() {
    if (GUI.Button(Rect(10,10,50,50),btnTexture)) {
        Debug.Log("Clicked the button!");
    }
}

Or, you need to create a custom GUI Style, to use with it, and drag your imported png texture into the appropriate "background" slots in the Style's inspector properties (eg, under the "Normal", "Hover" and "Active" slots). Then use that Style for the button:

var customGuiStyle : GUIStyle;

function OnGUI() {
    GUILayout.Button ("I am a custom-styled Button", customGuiStyle);
}
more ▼

answered Nov 21 '10 at 07:37 AM

duck gravatar image

duck ♦♦
41k 92 148 415

(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:

x3675
x786
x257
x72

asked: Nov 21 '10 at 06:09 AM

Seen: 7910 times

Last Updated: Nov 21 '10 at 06:09 AM