Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by regis.ramillien · Oct 19, 2011 at 08:43 PM · guieditor

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Sep 26, 2013 at 04:32 PM 0
Share

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sdgd · Feb 27, 2013 at 12:12 PM 0
Share

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

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges