EditorWindow: Window works, why does ModalWindow not?

The code beneath works perfectly when I replace GUI.ModalWindow by GUI.Window.

    public class Test2Window : EditorWindow
    {
        Rect window_position = new Rect(100, 100, 100, 100);

        [MenuItem("SCCD/Open Test")]
        public static void Init()
        {
            Test2Window window = (Test2Window) EditorWindow.GetWindow(typeof(Test2Window), false);
            window.title = "Editor Window";
        }

        private void WindowFunction(int id)
        {
            GUILayout.Button("test");
        }

        public void OnGUI() {
            this.BeginWindows();
            GUI.ModalWindow(0, this.window_position, WindowFunction, "");
            this.EndWindows();
        }
    }

With GUI.ModalWindow however I get the errors:

ArgumentException: Getting control 0's position in a group with only 0 controls when doing mouseDrag

(mouseDrag is replaced by the current event, so most of the time it’s just mouseMove)

So is this a bug? Or what magic do I have to perform to get a simple modal window working in an EditorWindow?

EDIT: if I use GUI instead of GUILayout to position the button inside the window, then I do not get the error anymore, however I do not get a window either.

Here’s a workaround modal window system for editor windows ^^

using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;

public class ModalWindow
{
    private static int m_WinIDManager = 1337;
    protected int m_WinID = m_WinIDManager++;
    protected bool enabled = true;

    public event System.Action<ModalWindow> OnWindow;
    public string title;
    public Rect position;
    public bool ShouldClose = false;    
    public int ID { get { return m_WinID; } }
    public ModalWindow(Rect aInitialPos, string aTitle)
    {
        position = aInitialPos;
        title = aTitle;
    }
    public ModalWindow(Rect aInitialPos, string aTitle, System.Action<ModalWindow> aCallback)
    {
        position = aInitialPos;
        title = aTitle;
        OnWindow += aCallback;
    }
    public virtual void OnGUI()
    {
        // For some strange reason Unity only disables the window border but not the content
        // so we save the enabled state and use it inside the window callback down below.
        enabled = GUI.enabled;
        position = GUI.Window(m_WinID, position, DrawWindow, title);
    }

    protected virtual void DrawWindow(int id)
    {
        // restore the enabled state
        GUI.enabled = enabled;
        if (OnWindow != null)
            OnWindow(this);
    }
    public virtual void Close()
    {
        ShouldClose = true;
    }
}

public class ModalSystem
{
    private List<ModalWindow> m_List = new List<ModalWindow>();
    public bool IsWindowOpen { get { return m_List.Count > 0; } }
    public ModalWindow Top
    {
        get { return IsWindowOpen ? m_List[m_List.Count - 1] : null; }
    }
    public void Draw()
    {
        // remove closed windows
        if (Event.current.type == EventType.Layout)
        {
            for (int i = m_List.Count - 1; i >= 0; i--)
                if (m_List*.ShouldClose)*

m_List.RemoveAt(i);
}
if (m_List.Count > 0)
{
// draw all windows
for (int i = 0; i < m_List.Count; i++)
{
GUI.enabled = (i == m_List.Count - 1); // disable all except the last
GUI.BringWindowToFront(m_List*.ID); // order them from back to front*
GUI.FocusWindow(m_List*.ID); // ||
m_List.OnGUI();
_}
}
}
public void Add(ModalWindow aWindow)
{
m_List.Add(aWindow);
}
}*_

public class EditorWindowTest : EditorWindow
{
[MenuItem(“Tools/TestWindow”)]
public static void Init()
{
GetWindow();
}

ModalSystem modalWindows = new ModalSystem();

void OpenPopup(string aTitle)
{
var win = new ModalWindow(new Rect(30, 30, position.width - 60, position.height - 60), aTitle, (w) =>
{
if (GUILayout.Button(“Open another popup”))
OpenPopup(aTitle + “->subwindow”);
if (GUILayout.Button(“Close”))
w.Close();
GUI.DragWindow();
});
modalWindows.Add(win);
}

void OnGUI()
{
GUI.enabled = !modalWindows.IsWindowOpen;
if (GUILayout.Button(“Open Popup”))
{
OpenPopup(“First”);
}
if (GUILayout.Button(“Some other GUI stuff”))
{
Debug.Log(“Doing stuff…”);
}
BeginWindows();
modalWindows.Draw();
EndWindows();
}
}
It’s just quickly hacked together. Some things could be done in a better way. Since windows in general have some very strange things ongoing in the background you might even drop them all together ^^. If you manually disabling all other content (like in my system above) there should be no issue with overlapping. So you could draw each “window” simply as layout group with a box / window style. Of course window dragging has to be done manually if needed, but that’s not that difficult to implement ^^.
This is just ment as example. The window ID generation is sufficient for most cases.
Keep in mind that those classes don’t support serialization at the moment. So when you change playmode or scripts are recompiled the ModalSystem will be recreated and all open windows would vanish.