- Home /
GUI.Window makes custom EditorWindow drag header unresponsive
I've made a custom EditorWindow that displays a lot of "GUI.Window"s and I've made them all draggable using the "GUI.DragWindow" method. Moving a draggable gui window around makes the overall EditorWindow header (the top horizontal strip of the window you can click on to drag the window around) unresponsive.
I believe this has something to do with how "GUI.DragWindow" processes the current event. I manually process all of the events that happen in the EditorWindow and use the current event through "currentEvent.Use()". I don't manually "Use" the current event when the user clicks on a "GUI.Window", because doing so would stop the "GUI.DragWindow()" from working (the windows are no longer draggable). However, using the event before GUI.DragWindow does stop the overall EditorWindow from becoming unresponsive.
Has anyone else come across this issue? And if so, could you advise as to how I might approach solving this (have both the GUI.DragWindow windows and the overall EditorWindow be responsive)
Thanks in advance. Matt
I've noticed that if I check the event type when the EditorWindow header strip is being unresponsive, the type is "Ignore".
I've also noticed that the GuiUtility.hotcontrol value isn't registering as 0 when I click on the EditorWindow after having dragged a GUI.window around. That's probably the issue.
Answer by MattSawrey · Nov 21, 2016 at 09:32 PM
Solved it.
If you're going to use GUI.DragWindow, but want to be able to resize and drag the custom EditorWindow around afterwards, make sure to reset the GuiUtility.hotcontrol value to 0 on the mouseup/dragexited event after that drag has finished.
I would have thought that this would have been handled by the DragWindow method, but I don't know exactly what happens inside that, so maybe there's something I'm missing.
Hope this is useful to others!
Thanks, Matt
Answer by Bunny83 · Nov 22, 2016 at 03:00 AM
Uhm, did you actually place your GUI.Window calls between BeginWindows and EndWindows? EditorWindows are handled a bit different.
Yes, I already knew to do this. Thanks for the response though.
Hmm, then you must do something else wrong as setting hotcontrol to 0 shouldn't be necessary. Are you filter some required events? $$anonymous$$aybe the layout event? The layout event is required for GUI.Windows, even when you don't use GUILayout. $$anonymous$$aybe you draw your windows "conditionally" and stop drawing it at the "wrong point". That's a general problem with the I$$anonymous$$GUI system. If something uses a generic passive control id it might get reassigned or moved to another control when you remove or add one in between.
GUI.DragWindow used to allocate a passive ID:
int controlID = GUIUtility.GetControlID(FocusType.Passive);
Unfortunately Unity moved the code of DragWindow into the native core recently, so we can't see what it does exactly. In the past (about Unity 3.3) it looked like this:
// UnityEngine.GUI
public static void DragWindow(Rect position)
{
GUIUtility.CheckOnGUI();
if (GUI._Window.current == null)
{
Debug.LogError("Dragwindow can only be called within a window callback");
return;
}
int controlID = GUIUtility.GetControlID(FocusType.Passive);
GUI._Window current = GUI._Window.current;
Event current2 = Event.current;
if (current == null)
{
return;
}
GUI.WindowDragState windowDragState = (GUI.WindowDragState)GUIUtility.GetStateObject(typeof(GUI.WindowDragState), controlID + 100);
switch (Event.current.GetTypeForControl(controlID))
{
case EventType.$$anonymous$$ouseDown:
if (position.Contains(current2.mousePosition) && GUIUtility.hotControl == 0)
{
GUIUtility.hotControl = controlID;
Event.current.Use();
$$anonymous$$atrix4x4 matrix = GUI._Window.current.matrix;
windowDragState.dragStartPos = GUIClip.GetAbsolute$$anonymous$$ousePosition() - matrix.$$anonymous$$ultiplyPoint(new Vector2(current.rect.x, current.rect.y));
windowDragState.dragStartRect = current.rect;
}
break;
case EventType.$$anonymous$$ouseUp:
if (GUIUtility.hotControl == controlID)
{
GUIUtility.hotControl = 0;
Event.current.Use();
}
break;
case EventType.$$anonymous$$ouseDrag:
if (GUIUtility.hotControl == controlID)
{
Vector2 vector = GUI._Window.current.matrix.inverse.$$anonymous$$ultiplyPoint(GUIClip.GetAbsolute$$anonymous$$ousePosition() - windowDragState.dragStartPos);
current.rect = new Rect(vector.x, vector.y, windowDragState.dragStartRect.width, windowDragState.dragStartRect.height);
current.moved = true;
Event.current.Use();
}
break;
}
}
Your answer
Follow this Question
Related Questions
EditorWindow not properly drawing 1 Answer
GenericMenu Renders Incorrectly On Second Monitor 0 Answers
EditorWindow.focusedWindow.SendEvent crashes Unity. 1 Answer
Is it possible to render a UnityAction field in a custom editor window? 0 Answers
Is there an event being fired off when the Inspector is being resized? 1 Answer