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

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

1 Answer

1

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? :-)

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 instead of the code posted above. Here is a link: [http://docs.unity3d.com/Documentation/Components/gui-Customization.html][1]. [1]: 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 :-)

I've run into an issue when trying to implement the code in the second part. I'm getting a semi colon error (syntax) when I have nothing missing. Any suggestions? var playButton:GUITexture; void onGUI() { Event e = Event.current; if (e.type == EventType.MouseUp) { if (playButton.contains(e.mousePosition) { // Perform Action } } }