- Home /
GUITexture Button?
Hello, how can I make something happen when my GUITexture is clicked? My GUITexture is "achieved" by this script which work's fine btw :-) :-)
public Texture2D background;
int theHeight = 50;
int theWidth = 200;
void OnGUI() {
GUI.DrawTexture(new Rect(Screen.width / 2 - (theWidth / 2), Screen.height - 50, theWidth, theHeight), background);
}
Thanks, Andreas :-)
Answer by robertbu · Mar 31, 2013 at 05:13 PM
Use GUI.Button() instead of GUI.DrawTexture. There are many different forms of this function. The second one is probably the one you want.
if ( GUI.Button(new Rect(Screen.width / 2 - (theWidth / 2), Screen.height - 50, theWidth, theHeight), background)) {
// Do something when button is pressed.
}
Thank you :D, how can I remove the "border" of the button, so that it look's like the DrawTexture? :-)
That's a bit more complicated. I'm not at my desktop, so I may not have this exactly right.
public Texture2D background;
int theHeight = 50;
int theWidth = 200;
void OnGUI() {
Rect rect = new Rect(Screen.width / 2 - (theWidth / 2), Screen.height - 50, theWidth, theHeight);
GUI.DrawTexture(rect, background);
Event e = Event.current;
if (e.type == EventType.$$anonymous$$ouseUp) {
if (rect.contains(e.mousePosition) {
// Do your mouse stuff
}
}
}
The action occurs on $$anonymous$$ouseUp, but you could use $$anonymous$$ouseDown.
Isn't an easier option to just change the style of the button?
@Unitraxx - possibly. I always reach for code. @AndreasX12 as @Unitraxx points out you can change your button style ins$$anonymous$$d of the code posted above. Here is a link:
http://docs.unity3d.com/Documentation/Components/gui-Customization.html.
Thank you all for the answers, I figured it out now, using the Custom gui style :-)