GUI Window problems

I got a few issues with the GUI Window. The first being, it isn’t positioning itself correctly. Here is the code i am using for it:

private var windowRect : Rect = Rect (Screen.width/2, Screen.height/2, 250, 300)

function OnGUI () {
		windowRect = GUILayout.Window (2, windowRect, WindowFunction, npcName);
}

As you can see, i have it set so it should render dead center of the screen. Problem is, it doesn’t. On a 1024 x 768, it is in the top right corner, barley on screen. It is completely off screen in a Web build. My knowledge of Unity GUI is that by positioning the GUI at the Screen.Width( and Height)/2 should put it in the center regardless of the screen size. So please help with that.

Secondly, i want everything to be form fitting, in the sense that when i have all my buttons and labels using the GUILayout that the Windows will also do the same, yet they are the same size as they are defined in the windowRect function.

Rect (Screen.width/2, Screen.height/2, 250, 300)

This will create a rect which starts at the mid-point and extends from there, so it will go from screen.width/2 to screen.width/2 + 250

Use something like

Rect (Screen.width/2 - windowWidth/2, Screen.height/2 - windowHeight/2, windowWidth, windowHeight)

Also, setting this right in the definition of the rect has a tendency to run Screen.width and Screen.height before the players size is properly set. Try moving it to Awake and you shouldn’t have any more problems.

The default behavior for windows is to expand as needed, but not to contract if you define extra space (I believe). If you want the window to be the smallest required size, define it in a rect like (Screen.width + 1, Screen.height +1, 1, 1) and move it after the first frame. (you draw it off-screen at first so that the user doesn’t see the “flicker” when it grows and then moves). Something like

void Update(){
     if (windowNotResized){
          windowRect = new Rect(Screen.width / 2 - windowRect.width / 2, Screen.height / 2 - windowRect.height / 2, windowRect.width, windowRect.height);
     }
}

I wouldn’t do it in the update loop though, as there’s really no reason to check after it’s been resized initially. I’d just start a Coroutine in the void Start() function to handle it. Also, you’d need it to ensure that the window has actually been drawn for a frame (setting a bool at the end of your OnGUI call would work fine).

If i try to set it up like this, it simply deforms my window into a extortionately small shape, still in the wrong position.
Maybe i just defined windowHeight and Width wrong. How do you think i should do it?

windowRect.x = (Screen.width - windowRect.width) / 2;
windowRect.y = (Screen.height - windowRect.height) / 2;

This is the correct code required to display the window dead centre of the screen. If you can’t visualize it, I suggest you draw this out on paper (doing that helped me).

About window size, since you use GUILayout.Window, perhaps the window is small because of your content? Pass in added GUILayout.Width parameters to constraint the size or just use GUI.Window to see if that’s the problem.

Thanks Cor1312, that worked perfectly! I am said i didn’t think of that myself. Though, if someone else can still help me with the sizing issue…

Give me an example of what you’re displaying inside the window.

Did you try using a normal GUI.Window instead of GUILayout.Window?

Here is that example you wanted, this is my working, but only as a test, dialogue box.

static var finishedQuest : boolean = false;
static var gotQuest : boolean = false;
static var display : boolean = false;

var npcName : String;
var dialouge : String[];
var dataBase : GameObject;
private var dialougeNum : int = 0;
private var once : boolean = true;

private var windowRect : Rect = Rect (Screen.width/2, Screen.height/2, 200,150) ;
private var scrollViewVector : Vector2 = Vector2.zero;

function OnGUI () {
	if(display) {
		windowRect = GUILayout.Window (2, windowRect, WindowFunction, npcName);
	}
	if(once) {
		windowRect.x = (Screen.width - windowRect.width) / 2;
		windowRect.y = (Screen.height - windowRect.height) / 2; 
		once = false;
	}
}

function WindowFunction (windowID : int) {	

	scrollViewVector = GUILayout.BeginScrollView (scrollViewVector);
	GUILayout.Label (dialouge[dialougeNum]);
	
	if(finishedQuest) { 
		dialougeNum = 5;
		if(GUILayout.Button ("Gee, Thanks!")) {
		dataBase.gameObject.GetComponent(experienceSystem).tutorialOver = true;
		display = false;
		}
	} else {
		if(gotQuest) {
			dialougeNum = 4;
			if(GUILayout.Button ("Okay")) {
				display = false;
			}
		} else {
			if(dialougeNum == 0) {
				if(GUILayout.Button ("Hi James")) {
					dialougeNum = 1;
				}
			}	
			if(dialougeNum == 1) {
				if(GUILayout.Button ("Sure")) {
					dialougeNum = 2;
				}
			
				if(GUILayout.Button ("No Thanks")) {
					dialougeNum = 3;
				}		
			}
			
			if(dialougeNum == 2) {
				if(GUILayout.Button ("Sure")) {
					dialougeNum = 0;
					display = false;
					gotQuest = true;
				}
			
			}		
			if(dialougeNum == 3) {
				if(GUILayout.Button ("Actually...")) {
					dialougeNum = 2;
				}
			
				if(GUILayout.Button ("Bye [Exit]")) {
					dialougeNum = 0; 
					display = false;
				}		
			}	
		}
	}
	
	GUILayout.EndScrollView();
	GUI.DragWindow (Rect (0,0,10000,100));
		
}

I hope that helps you. Also trying to use a regular GUI.Window does not fix the problem, as in i want it to auto correct its size instead of me having to do it.

What exactly is wrong with the window? If I bypass your programming logic and display all the buttons, the window displays properly with the size tailored to the content.

All i know is, on some of the dialogue screens there is more of GUI window then i want to be shown. Also, if i set the the windows rect to a height and width of 1, it does not fix its self automatically like it should.