- Home /
How to make OnGUI Texture able to be clicked
I'm developing a game based on turf/territory wars, and I plan to have a map overview menu where someone can select a territory and attack it. I've looked everywhere, but I can't seem to find a way to make a texture drawn with OnGUI click-able. How might I do this?
Answer by Cherno · Nov 09, 2015 at 02:37 PM
Well... Just use GUI.Button with the texture as it's image, or draw the texture with GUI.DrawTexture and also put an invisible button in the same space (rect) :) If you don't, for whatever reason, use a button, then you can also use the Contains query with a Rect variable (the one from your GUI.DrawTexture, of course) to check if the current cursor position is inside of it when clicking.
The problem here is that I need to make territories that are not rectangles, so a Rect will not work. How could I do that?
A little bit more complicated, but not impossible.
You have to get the color of the pixel of the texture that is under the mouse cursor via GetPixels(). if the color has an alpha value of 0, then ignore the button/rectangle. Otherwise, you know that a filled part of the texture was clicked on.
Check the Scripting API for further information on GetPixels and Color.
Your answer
Follow this Question
Related Questions
My OnGUI() Won't show the Button elements :( 0 Answers
Better performance? GUITextures or Texture2D drawn in function OnGUI? 0 Answers
Multiple Cars not working 1 Answer
Enabling and Disabling GUI Textures 1 Answer
Distribute terrain in zones 3 Answers