- Home /
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!
Bizarrely, the best way to do it ........ seems to be like this:
private Rect windowRect;
void OnGUI()
{
windowRect = GUILayout.Window(0, windowRect, SomeButtons, "Title:");
}
private void TestButtons(int windowID)
{
GUILayout.BeginVertical();
if (GUILayout.Button("Explode")) yourFunction();
if (GUILayout.Button("Destroy")) yourFunction();
if (GUILayout.Button("Obliterate")) yourFunction();
if (GUILayout.Button("Crush")) yourFunction();
GUILayout.EndVertical();
GUI.DragWindow();
}
private void YourFunction() { Debug.Log("Holy crap it works."); }
Note that you just drop the GUI.DragWindow() code "magically at the end" and it works. What the hell, eh?
to set an initial position, just do this with the windowRect variable. (Or of course, set it in your "Start" - or whatever is relevant to you.)
private Rect windowRect = new Rect(0f,0.2f*Screen.height,0f,0f);
Cheers!!
Answer by CHPedersen · Oct 21, 2011 at 12:22 PM
Here's a shot in the dark, feel free to downvote or whatever if it doesn't help you. :)
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.
Answer by regis.ramillien · Oct 21, 2011 at 07:34 PM
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...
please don't post answer as comment OFC it will not work any more
that's simple let me show you:
//GUI.Window (0, new Rect (25, 25, 100, 100), Do$$anonymous$$yWindow, "$$anonymous$$y Window");
is returning new rect to nothing so everything you change in it does nothing
SomeWindowRect = GUI.Window (0, Do$$anonymous$$yWindowRect , Do$$anonymous$$yWindow, "$$anonymous$$y Window");
now when you'll want to drag your window you actually won't drag $$anonymous$$y window but SomeWindowRect
Answer by Veehmot · Oct 20, 2011 at 09:19 PM
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
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Setting Scroll View Width GUILayout 1 Answer
How can I make a custom Time Range Selector (with sliders)? 1 Answer
How do I implement Draggable Properties with custom Labels in Editor or PropertyDrawer? 1 Answer
Texture2D not showing in build, but shows up in editor 1 Answer