x


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!

more ▼

asked Oct 19 '11 at 08:43 PM

regis.ramillien gravatar image

regis.ramillien
1 1 1 1

If I understood correctly, you want to create a Window inside a Window? Window-ception. I think there isn't an automatic drag function, you'll have to move it manually.

Oct 19 '11 at 08:56 PM Veehmot

Hello,

Thanks for your response.

You're right, I try to create a Window inside the EditorWindow. Only because I want to move a group a components in one drag.

Do you know if there is another container than a Window that can be draggable ?

Oct 20 '11 at 07:38 PM regis.ramillien

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

Oct 20 '11 at 09:19 PM Veehmot

Hello,

Ok, thanks, but I already done it without success...

Thanks again for your help.

Oct 21 '11 at 05:36 AM regis.ramillien
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Oct 21 '11 at 12:22 PM

CHPedersen gravatar image

CHPedersen
6k 13 22 61

(comments are locked)
10|3000 characters needed characters left

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...

more ▼

answered Oct 21 '11 at 07:34 PM

regis.ramillien gravatar image

regis.ramillien
1 1 1 1

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), DoMyWindow, "My Window");

is returning new rect to nothing so everything you change in it does nothing

SomeWindowRect = GUI.Window (0, DoMyWindowRect , DoMyWindow, "My Window");

now when you'll want to drag your window you actually won't drag My window but SomeWindowRect

Feb 27 at 12:12 PM sdgd
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5070
x3680
x1672

asked: Oct 19 '11 at 08:43 PM

Seen: 2077 times

Last Updated: Feb 27 at 12:12 PM