- Home /
Mysterious Rect problem
Hello. I have a crazy problem with Rect().
When I use
GUILayout.Window(0, Rect(Screen.width/2-75,Screen.height/2-75, 150, 100) , LoginWindow, "Loginversuch");
The window is in the center of the screen.
But when I define a variable with the same rect
private var windowRect = Rect(Screen.width/2-75,Screen.height/2-75, 150, 100);
GUILayout.Window(0, windowRect , LoginWindow, "Loginversuch");
Then only the width is centered but not the height (its over the top of the screen).
I cant find the problem. Please help, thanks again for your great help :)
EDIT. When I use (Debug.Log(windowRect) I get
(left:551,00, top:344,00, width:150,00, height:100,00)
Which seems to be fine, but why it doesnt apply the correct top value?
Answer by Ezzerland · Jun 15, 2010 at 08:52 PM
Try:
var windowRect : Rect = Rect((Screen.width/2)-75, (Screen.height/2)-50, 150, 100);
windowRect = GUILayout.Window(0, windowRect, LoginWindow, "Loginversuch");
In this case, I only added a space before Screen.height, parenth, and followed the basic unity example of a window found here: http://unity3d.com/support/documentation/Components/gui-Controls.html
also removed a space after 0, windowRect ,
Also, to be a bit more precise in center from top, it should be half your rect's height. so it should be Screen.height/2-50 I did this above, and also (not that its necessary) added in the additional parenth to find scrn height/width.
Give it a shot, lemme know :)
Thank but the space doesn't fix the problem.=(
Also thanks for my little mistake with 75 ins$$anonymous$$d of 50.
Any other idea how to fix the problem?
Oh, yes. The second line should be:
windowRect = GUILayout.Window(0, windowRect , LoginWindow, "Loginversuch");
Also tried this (Copied from example).. doesnt work =/ Thanks anyway
Edited the original answer post for a fuller example of code. I'm not at home and in turn unable to test the code myself, but I see no reason it really should not work. If the above attempt doesn't work, then without fiddling with it I'll leave you to find someone more knowledgeable in js. I don't really use js myself :p
Not that this should matter, but pending where the variable is located, is it necessary to make the variable private? if it's inside a function or etc, it already will be private. This really shouldn't matter, but, just curious of the use.