Search Unity

GUI.DragWindow ... doesn't work?

Discussion in 'Immediate Mode GUI (IMGUI)' started by podperson, Nov 13, 2007.

  1. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    Code (csharp):
    1. var confirmationMessage : String = "Are you really sure you want to do that? I'm not sure it's such a great idea.";
    2.  
    3.  
    4. function OnGUI () {
    5.     // Make a background box
    6.    
    7.     var windowRect : Rect = Rect (Screen.width - 340, Screen.height - 140, 320, 120);
    8.    
    9.     windowRect = GUI.Window (0, windowRect, winConfirm, "Confirmation");
    10. }
    11.  
    12. function winConfirm (windowID : int) {
    13.     GUI.Label (Rect (10, 20, 300, 60), confirmationMessage);
    14.    
    15.     // Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
    16.     if (GUI.Button (Rect (140,90,80,20), "Cancel")) {
    17.         print( "Cancel" );
    18.     }
    19.  
    20.     // Make the second button.
    21.     if (GUI.Button (Rect (230,90,80,20), "OK")) {
    22.         print( "OK" );
    23.     }
    24.    
    25.     // Make window draggable
    26.     GUI.DragWindow (); // (Rect (0,0, 10000, 20));
    27. }
    Finally started playing with the new GUI code. It looks great ... but I can't make my windows draggable. No errors, but no dragging.

    Bug or wetware error?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You're explicitly setting windowRect every GUI call. Just define it once (make it a global var and move that line out of OnGUI into Start or something), so the dragging has a chance to update windowRect.

    --Eric
     
    jaasso and Melang like this.
  3. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    Isn't that how windows work?

    Here's the sample code from the documentation. It doesn't work either.

    Code (csharp):
    1. var windowRect = Rect (20, 20, 120, 50);
    2.  
    3. function OnGUI () {
    4.   // Register the window.
    5.   windowRect = GUI.Window (0, windowRect, DoMyWindow, "My Window");
    6. }
    7.  
    8. // Make the contents of the window
    9. function DoMyWindow (windowID : int) {
    10.   // Make a very long rect that is 20 pixels tall.
    11.   // This will make the window be resizable by the top title bar - no matter how wide it gets
    12.   GUI.DragWindow (Rect (0,0, 10000, 20));
    13. }
    Edit:

    I tried moving the call as you suggested anyway -- it causes errors and still doesn't work.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Nope...think of OnGUI like Update. If you assign windowRect in OnGUI, then it's reset every frame (or twice a frame; however often the GUI is updated). So naturally dragging a window isn't going to work like that, since you're setting it back to the original position every GUI call.

    Yes it does. :) What isn't working for you? Is it just not dragging? Can't see why...I cut pasted that into a script and it works fine.

    Hmm...you're not naming the scripts the same thing as a Unity class, are you? (Like "GUI" or something.)

    --Eric
     
  5. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    Ah -- wetware error on my part :)

    I misinterpreted your original response.

    Thanks again!
     
  6. Bikebreck

    Bikebreck

    Joined:
    Aug 4, 2010
    Posts:
    112
    Here you go it has a text field and button.
    Code (csharp):
    1.  
    2. var stringToEdit : String = "Hello World";
    3. var windowRect : Rect = Rect (20, 20, 665, 50);
    4.  
    5.  
    6. function OnGUI () {
    7.      //Register the window.
    8.     windowRect = GUI.Window (0, windowRect, DoMyWindow, "Text");
    9.    
    10. }
    11.  
    12. // Make the contents of the window
    13. function DoMyWindow (windowID : int) {
    14. //text field
    15.   stringToEdit = GUI.TextField (Rect (5, 20, 600, 20), stringToEdit, 500);
    16.     //Button
    17.     if (GUI.Button (Rect (610,20,50,20), "text")) {
    18.     }
    19.    
    20.     GUI.DragWindow (Rect (-611, 1, 600, 20));
    21. }
    22.  
    Bikebreck
     
    Dealzu-The-Wikid likes this.
  7. cavemaker

    cavemaker

    Joined:
    Aug 16, 2015
    Posts:
    1
    Took me longer than I'd like to figure this out here in 2020, so I'll add what I missed.

    GUI.Window returns the new, potentially dragged position of the window, and you need to actively assign it to the windowRect variable you created, or dragging doesn't work!

    Code (CSharp):
    1. windowRect = GUI.Window(0, windowRect, DoMyWindow, "My Window");
    Obvious on a close reading of the code, but it took me quite awhile.
     
  8. unity_2897B49A6712C1195C9E

    unity_2897B49A6712C1195C9E

    Joined:
    Aug 29, 2021
    Posts:
    1
    Thank you so much!