Before updating to Windows 10 you may need to return your Pro license. More information here

Assigning Texture2D as GUI.Button

I'm attempting to create a custom GUI from an Illustrator file which will control character movement. Here is a sample of the script I'm attempting to build:

// GUI active elements: var dPadTexture : Texture2D; var position : Rect;

function OnGUI () {

GUI.DrawTexture( position, dPadTexture ); GUI.Button(position, dPadTexture); }

My question is how to draw a Texture2D in the GUI and have it also function as a button, basically just creating a custom button graphic that inputs as a mouseEvent telling the character to move.

Here is a screenshot of the above code's result:alt text

more ▼

asked Jan 21, 2011 at 02:15 AM

avatar image

54 34 30 35

After doing some reading it seems this may work however I haven't found a way to implement it:

myVar = GUILayout.Button("Test dPad", myVar, "Texture2D");

Jan 21, 2011 at 02:27 AM LANDO

To replicate the problem I'm encountering use this script: "// Draws a button with an image and a button with text var tex : Texture;

function OnGUI() { if(!tex) { Debug.LogError("No texture found, please assign a texture on the inspector"); }

if(GUILayout.Button (tex)) { Debug.Log("Clicked the image"); }

if(GUILayout.Button ("I am a regular Automatic Layout Button")) { Debug.Log("Clicked Button"); } }"

Script found @ http://unity3d.com/support/documentation/ScriptReference/GUILayout.Button.html

Jan 21, 2011 at 02:40 AM LANDO
(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

I think what you want is this:

GUI.Button(position, dPadTexture, "label")

This will create a button with the texture, and make it look like a label (no button border).

more ▼

answered Jan 21, 2011 at 02:42 AM

avatar image

Simon Wittber
353 14 30 22

This is exactly what I was looking for, thanks for the help! I'll post my final working code at the end of the day.

Jan 21, 2011 at 04:49 PM LANDO

just letting you know this helped me out a bundle too, thanks for that :D

May 14, 2011 at 10:13 AM Proportion
(comments are locked)
10|3000 characters needed characters left

I can't really tell what you're asking. Your DrawTexture doesn't look like it's helping anything. And you need an if statement around your button code, if you want it to be functional and not just graphical.


Edit: I'm assuming now that you're talking about the default style. Get rid of it.

var style : GUIStyle;

function OnGUI () { GUI.Button(position, dPadTexture, style); }

more ▼

answered Jan 21, 2011 at 02:41 AM

avatar image

19.1k 212 203 317

Good call on the GUIStyle, I got rid of the default style and everything is working great! Thanks!

Jan 21, 2011 at 04:51 PM LANDO
(comments are locked)
10|3000 characters needed characters left

if (GUI.Button(new Rect(100, 10, 200, 250), nameofthebutton)) {}

more ▼

answered Dec 26, 2011 at 09:05 AM

avatar image

31 54 43 46

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

This is my way, I do it like this:

1)Create GUISkin...

2)Write script, or copy and paste this JavaScript:

var Skin : GUISkin;

 function OnGUI {
     GUI.skin = Skin;
     //button code...

Good Luck, Ivan!

more ▼

answered Feb 27, 2013 at 08:14 PM

avatar image

1 2

(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



Answers and Comments



asked: Jan 21, 2011 at 02:15 AM

Seen: 9207 times

Last Updated: Feb 27, 2013 at 08:14 PM