- Home /
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:
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");
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
Answer by Simon Wittber · Jan 21, 2011 at 02:42 AM
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).
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.
just letting you know this helped me out a bundle too, thanks for that :D
Answer by Jessy · Jan 21, 2011 at 02:41 AM
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.
http://unity3d.com/support/documentation/ScriptReference/GUI.Button.html
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); }
Good call on the GUIStyle, I got rid of the default style and everything is working great! Thanks!
Answer by charnew · Dec 26, 2011 at 09:05 AM
if (GUI.Button(new Rect(100, 10, 200, 250), nameofthebutton)) {}
Answer by ivan1233 · Feb 27, 2013 at 08:14 PM
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!
Your answer
![](https://koobas.hobune.stream/wayback/20220613100403im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer
Make more buttons appear, on button click. 1 Answer
dynamic GUI Texture.. clipping and rotation HUD 1 Answer
script to create gui when detection collision between cube and first controler person ?? 1 Answer
WorldToScreenPoint returns the same value when facing the opposite direction. 0 Answers