var confirmationMessage : String = "Are you really sure you want to do that? I'm not sure it's such a great idea.";
function OnGUI () {
// Make a background box
var windowRect : Rect = Rect (Screen.width - 340, Screen.height - 140, 320, 120);
windowRect = GUI.Window (0, windowRect, winConfirm, "Confirmation");
}
function winConfirm (windowID : int) {
GUI.Label (Rect (10, 20, 300, 60), confirmationMessage);
// Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
if (GUI.Button (Rect (140,90,80,20), "Cancel")) {
print( "Cancel" );
}
// Make the second button.
if (GUI.Button (Rect (230,90,80,20), "OK")) {
print( "OK" );
}
// Make window draggable
GUI.DragWindow (); // (Rect (0,0, 10000, 20));
}
Finally started playing with the new GUI code. It looks great … but I can’t make my windows draggable. No errors, but no dragging.
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.
Here’s the sample code from the documentation. It doesn’t work either.
var windowRect = Rect (20, 20, 120, 50);
function OnGUI () {
// Register the window.
windowRect = GUI.Window (0, windowRect, DoMyWindow, "My Window");
}
// Make the contents of the window
function DoMyWindow (windowID : int) {
// Make a very long rect that is 20 pixels tall.
// This will make the window be resizable by the top title bar - no matter how wide it gets
GUI.DragWindow (Rect (0,0, 10000, 20));
}
Edit:
I tried moving the call as you suggested anyway – it causes errors and still doesn’t work.
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.)
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!
windowRect = GUI.Window(0, windowRect, DoMyWindow, "My Window");
Obvious on a close reading of the code, but it took me quite awhile.