Problem with making a tower select GUI
Dear all,
I've been searching a long time for a solution to my problem, but so far no succes.
I have created a grid of tiles and all tiles have a script attached to it. When I click on a tile, a GUI should popup with four buttons. So far, this worked for me. But now I want that, when I click anywhere else than the four buttons, the GUI disables (goes away) itself.
I have allready tried this by using the coordinates from Input.GetMouseButtonDown(0) and convert this to world coordinates. However, when I enlarge the screen, this doesn't hold up anymore. So preferably I would use the OnDeselect function, but I do not know how to use this with code made buttons.
I use this line four times to create the buttons:
if (GUI.Button(new Rect(screenPos.x + 10, Screen.height - screenPos.y, 40, 30), "Test"))
So the start of the GUI looks like this:
void OnGUI()
{
if (ColliderClick == true && TileIsFree == true)
{
if (GUI.Button(new Rect(screenPos.x + 10, Screen.height - screenPos.y, 40, 30), "Test"))
So I would like to have a OnDeselect function that sets ColliderClick = false; Anyone that know how to do this?
Many thanks
Answer by reinoutdejongh · Jun 27, 2016 at 08:28 AM
I Fixed it, I used the following code:
Vector3 screenPos = Camera.main.WorldToScreenPoint(gameObject.transform.position);
Event e = Event.current;
Rect windowPosZero = new Rect(screenPos.x + 10,Screen.height - screenPos.y,40,30);
if (e.type == EventType.MouseDown && !windowPosZero.Contains(e.mousePosition))
{
ColliderClick = false;
}