How to make an GUILayout.Window draggable ?

Hello,

I’ve got a problem trying to make a draggable container into the unity editor.

My need is simple, I want to create a window inside Unity editor. This window will contain draggable containers, and containers will contains text, label, etc.

I found only the GUILayout.window as container. Perhaps is it my first mistake ?
In any circumstances, I created the editor window, the GUILayout.Window (the container) and added a label to the container.

Here is the sample

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

public class TestDraggable : EditorWindow
{
	[MenuItem("Window/Draggable...")]
	static void ShowWindow ()
	{
		EditorWindow.GetWindow (typeof(TestDraggable));
	}

	void OnGUI ()
	{
		BeginWindows ();
		GUILayout.Window (0, new Rect (25, 25, 100, 100), DoMyWindow, "My Window");
		EndWindows ();
	}

	void DoMyWindow (int windowID)
	{
		GUILayout.Label("Test label...");
	}
}

Now, I want this container to be draggable… No way…

I spend some time on the reference documentation, some times on google, found potential solution (GUI.DragWindow(), EventType.mouseDrag, etc). But I can’t make it work !

When logging EventType.mouseDrag, I see a drag event is happening, but how to drag the window automatically ?
I suppose I could set the window position manually, but I’m sure something already exists to drag a window automaticcaly…

Please !!! Help me, Obi-Wan Kenobi. You’re my only hope.

Best regards!

Here’s a shot in the dark, feel free to downvote or whatever if it doesn’t help you. :slight_smile:

I don’t know if windows act entirely the same when they’re in the editor, but when drawn in games, you need to call the function GUI.DragWindow in the window function before the window responds to mouse drags. I’m guessing the same applies to a GUILayout.Window when in the editor.

Hey !

I already tried this, but now, it works !

I found the “issue”. Even it seems to be a bug.

For information, if other peoples are in trouble too. Here is a working code:

using UnityEngine;
using UnityEditor;

public class DialogEditor : EditorWindow {
public Rect windowRect = new Rect (20, 20, 120, 50);

[MenuItem("Window/Dialogs")]
static void ShowWindow () {
	EditorWindow.GetWindow (typeof(DialogEditor));
}

void OnGUI () {
	BeginWindows ();
	//GUI.Window (0, new Rect (25, 25, 100, 100), DoMyWindow, "My Window");
	windowRect = GUI.Window (0, windowRect, DoMyWindow, "My Window");
	EndWindows ();
}

void DoMyWindow (int windowID) {
	GUI.DragWindow (new Rect (0, 0, 10000, 20));
}

}

Now, just uncomment the “//GUI.Window (0, new Rect (25, 25, 100, 100), DoMyWindow, “My Window”);” and comment the next line and it will not work anymore !

It seems that a windowRect variable must be declared, used and affected with the return value of the “GUI.Window”.
If the variable is not used in the function or is not affected with the return value, drag will not work…

No i don’t, but if I were you, I’d check the ShowXXX family of functions of http://unity3d.com/support/documentation/ScriptReference/EditorWindow.html