- Home /
Can you use GUI.DrawTexture to make a clickable button
I am using the GUI.DrawTexture to make a gui texture appear so that it overlaps two cameras, but the script I am using to click on it is not working since I have changed to using this function instead of having a gui texture in the project panel. How can I make this clickable? I am using the OnMouseUp function currently.
I converted your answer to a comment. Next time please use the comments to reply to someone. The answers are only used to answer the main question.
Answer by Ranza · Jan 17, 2011 at 11:08 PM
Probably the best idea is to use the Rect coordinates that you're passing to the DrawTexture function.
Try something like this:
Rect r = new Rect(x,y,width,height);
GUI.DrawTexture(r, texture);
Input.GetMouseButton(0)
{
if (r.Contains(Event.current.mousePosition)) DoStuff();
}
If your graphics is static you may also want to put Rect in the class body.
Answer by Flynn · Jan 17, 2011 at 11:11 PM
There are three ways:
Use OnMouseUp as you said, and compare Input.MousePosition at that location to the rect of your texture.
Use a normal button, but replace the text section with your texture, and place a third parameter with the value : "label". This will make your button render as a label, and also use the texture as its content.
Place a button over your texture using the same rect. Make the text of the button blank, and add the label parameter as before. This will create an invisible button above your texture which can be used instead.
Hope this helps -- Flynn
Answer by maikonfarias · Feb 06, 2014 at 12:45 PM
Hi, for creating a simple texture as button I use this code, for me it looks simple.
C#:
Rect position = new Rect(x,y,width,height);
GUI.DrawTexture(position , texture);
if (GUI.Button(position , "", new GUIStyle()))
{
DoStuff();
}
I use the same rect to draw a button with a new empty GUIStyle, making the actual button invisible but still working for click/touch.