Dragging a GUI Window - Why Doesn't This Work?

Any idea why I can’t drag this window, or any GUI Window I’ve tried this with?

function DataTableWindow (windowID : int) {
	
	GUI.Label (Rect (18, 22, 200, 20), "Delta t =");
	dataTable13 = GUI.TextField (Rect (95, 25, 60, 16), dataTable13, 10, customMenu);
	GUI.Label (Rect (160, 22, 200, 20), "sec");
	GUI.Label (Rect (18, 42, 200, 20), "Puck Mass =");
	dataTable14 = GUI.TextField (Rect (95, 45, 60, 16), dataTable14, 10, customMenu);
	GUI.Label (Rect (160, 42, 200, 20), "kg");
	GUI.Label (Rect (18, 62, 200, 20), "Theta =");
	dataTable15 = GUI.TextField (Rect (95, 65, 60, 16), dataTable15, 10, customMenu);
	GUI.Label (Rect (160, 62, 200, 20), "deg");
	
	GUI.Label (Rect (18, 85, 200, 20), "Point");
	GUI.Label (Rect (75, 85, 200, 20), "t (s)");
	GUI.Label (Rect (140, 85, 200, 20), "x (m)");
	
	var y = 105;
	for (var i=0; i<13; i++){
		GUI.Label (Rect (20, y, 30, 20), i.ToString());
		dataTable11[i] = GUI.TextField (Rect (55, y, 60, 16), dataTable11[i], 10, customMenu);
		dataTable12[i] = GUI.TextField (Rect (125, y, 60, 16), dataTable12[i], 10, customMenu);
       	y += 18;
	}
	
    if (GUI.Button (Rect (65,350,80,20), "Done")) {
		    for (var i : GameObject in gSparkGen) {
				i.renderer.enabled = false;
    		}
    	Camera.main.transform.position = gCameraPositions[0];
    	Camera.main.transform.rotation.eulerAngles = gCameraRotations[0];
    	gCameraLoc = 0;
    	showTableWindow = false;
    }

	GUI.DragWindow();
}

Hello,
Could I get a look at your OnGUI() function?

-Lincoln Green

Sure. It’s kind of long. Is there some flag that needs to be set to make the window dragable?

function OnGUI () {
	
	// change view
	if (GUI.Button (Rect (Screen.width-100,12,80,20), GUIContent("View", "Go to a wide or close-up view."))) {
		gMoveCamera = true;
		
		// stop analyzing data
		if (gAnalyzeData) {
			for (var i : GameObject in gPaperTex) {
				i.active = false;
    		}
			gAnalyzeData = false;
			return;
		}
		if (showTableWindow) {
			return;
		}
		
		// move camera
		switch (gCameraLoc) {
		case 0:
			Camera.main.GetComponent(CameraControl).MoveCamera (gCameraPositions[1], gCameraRotations[1], 3.0);
		    for (var i : GameObject in gSparkGen) {
				i.renderer.enabled = true;
    		}
    		GameObject.Find("Puck1").GetComponent("Puck").ResetPuck();
   			gCameraLoc = 1;
			break;
		case 1:
			Camera.main.GetComponent(CameraControl).MoveCamera (gCameraPositions[0], gCameraRotations[0], 3.0);
		    for (var i : GameObject in gSparkGen) {
				i.renderer.enabled = false;
    		}
			gCameraLoc = 0;
			break;
		}
	}
	
	
	// data table
	if (GUI.Button (Rect (20,40,100,20), GUIContent("Data Table", "Enter data in data table."))) {
		if (!showDataWindow) {
			showTableWindow = true;
		}
	}
	
	if (showTableWindow) {
		Camera.main.transform.rotation.eulerAngles = gCameraRotations[2];
		tRect = Rect(20, 80, 210, 380);
		tableWindowRect = GUI.Window (1, tRect, DataTableWindow, "Data 5.1.1 - 5.1.2");	
	}
	
	// analyze data
	if (GUI.Button (Rect (20,12,100,20), GUIContent("Analyze Data", "Select a data set and analyze it."))) {
		if (!showTableWindow) {
			showDataWindow = true;
		}
	}
	
	if (showDataWindow) {
		tHeight = gMenuItems.length * 18 + 60;
		tRect = Rect((Screen.width-200)/2.0, 90, 200, tHeight);
		dataWindowRect = GUI.Window (0, tRect, DataSelectWindow, "Select A Data Set");	
	}
	
	// info box
	GUI.Box (Rect (10,Screen.height-30,200,24), "", customButton);
	if (gAnalyzeData) {
		GUI.Label (Rect (18, Screen.height-28, 200, 20), gMenuItems[gCurrentDataSet]);
	}
	else
	{
		GUI.Label (Rect (18, Screen.height-28, 200, 20), "Table Inclination: " + gTableRot.ToString("F2") + " deg");
	}
}

Hey, as you can read in the doc’s

The window is made dragable by giving a drag area, like so:

// Make the contents of the window
function DoMyWindow (windowID : int) 
{
    if (GUI.Button (Rect (10,20,100,20), "Hello World"))
        print ("Got a click in window " + windowID);
    // Make the windows be draggable.
    GUI.DragWindow (Rect (0,0,10000,10000));
}

[Edit] nvm, didn’t see you where using the no argument one… :blush: is there any part of the window left to grab?

I tried both with and without the argument, and tried defining various areas, and it made no difference.

Hello,

Don’t see anything wrong with your code – to clarify:

  1. The window does show up, it’s just not draggable
  2. Try commenting out all your controls so you get an empty window. If this is draggable, then you probably just don’t have anywhere left to drag in the window.
  3. I’m guessing it’s not one of the above, and I’m out of ideas, maybe an uploaded project would be in order?

Best wishes,

-Lincoln Green

  1. Yes.
  2. Makes no difference with or without window content.
  3. The project is posted here: http://www.polyhedronlearning.com/cengage/lab_5_unity_sim.html. Click either of the buttons in the upper-left to open a window. Both of those windows should be draggable.

GUI.Window returns a rect value, which is the new location and size of the window. You need to store the returned value and then pass it back into the function again the next time it is called. Your code is currently setting the rectangle to a fixed value each time.

Yep, that did the trick. Thanks.