Unity3d GUI modal window - prevent mouse clickthrough

I have a main script that is attached to the “world” (root) object on the scene and is responsible for global things such as global key events, displaying debug GUI, etc…

void OnGUI()
{
GUI.color = Color.red;
GUI.depth = 220;
windowRect0 = GUI.ModalWindow(0, windowRect0, DoMyWindow, "Red Window");
}
void DoMyWindow(int windowID)
{
if (GUI.Button(new Rect(10, 20, 100, 20), "Hello World"))
{
print("Got a click in window with color " + GUI.color);
Event.current.Use();
}else
{
Event.current.Use();
}
GUI.DragWindow(new Rect(0, 0, 10000, 10000));
}

I also have multiple objects dynamically generated on the screen (chess squares), each has a script that highlights itself when mouse hovers over and each pops detailed info (a prefab) when mouse clicks over it:

public class ChessTileHandler : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler{

...
public void OnPointerEnter(PointerEventData eventData)
{
.. highlight object...
}
public void OnPointerExit(PointerEventData eventData)
{
... return to normal
}
public void OnPointerClick(PointerEventData eventData)
{
... pop details...
}

}

The idea is the root object would pop the little debugging modal window on a particular key combination. It does pop the window, but it is not modal, when I click on the button from the GUI debug window or try to move it around, the chess square behind it reacts properly with a click event, it also reacts with a hover event when i hover over the GUI elements while dragging the modal window.

My question is, how do I create a truly modal window that would “eat” any mouse click on it (and preferably any mouse click outside it too) until I disable the GUI element?

EDIT:
the only solution I came up with so far is to have a static/global variable that enables/disables EventSystem. Is there a more elegant solution to the issue?

I also had once this problem. I solved it by extending the event system and always ask the event system if there is some UI below the mouse.
It doesn’t feel like a good approach but I don’t know any better solution.

@jtg0d

hi there,

I’d ditch the old GUI completely - I know, it’s for debugging… but…

Create you debug panel with new (current) UI tools.

Then you can add CanvasGroup component to your debug menu.

If it’s showing only info, and not Interactable - you can make CanvasGroup ignore “hits” so you can have it over UI elements or you can make it block UI Raycasts, meaning that you can’t click “through” your debug panel… if you wish so.

It would be also possible to simply have some large transparent UI panel behind debug panel + make it have CanvasGroup blocking Raycasts, this way you couldn’t click outside of panel either, until you have closed debug panel.