- Home /
Center GUI wondow or box in windowed mode
I made a window using:
new Rect ( (Screen.width - (Screen.width/2) ), 100 , 600, 600);
But it doesnt center in windowed mode.
Im trying to make a main menu showing and hiding different windows. Everyone is using different resolution so its important.
Also a good idea would be to use dynamic size of a box and buttons.
Answer by sath · Jan 08, 2014 at 03:46 PM
for a GUI box(or Label) 600x600 size, you can say that 600 = Screen.height*0.6f so it will be
new Rect(Screen.width*0.5f-Screen.height*0.3f,Screen.height*0.5f-Screen.height*0.3f, Screen.height*0.6f,Screen.height*0.6f)
the start point of your GUI box is at top left corner so the center of it , will be in the center of screen for any screen size**
better than that is
//adjust your values here to select the desired size for your Box or Label
//in Play Mode you can see directly your changes after saving script first
float boxWidth=Screen.height*0.6f;
float boxHeight=Screen.height*0.6f;
//Now with the given values make the dot for draw start potision(top left corner)
float startBoxWidth=Screen.width*0.5f - boxWidth*0.5f;
float startBoxHeight=Screen.height*0.5f - boxHeight*0.5f;
//and finally
new Rect(startBoxWidth,startBoxHeight, boxWidth,boxHeight)
//this can be done also for GUI.Label ,Box , Button etc.
Answer by Piflik · May 16, 2012 at 02:47 PM
First, Screen.width - Screen.width/2 is the same as Screen.width/2...
Second, the position of a Rect specifies the position of its upper left corner. So to center a Rect on the screen, you'd have to subtract half of the Rect's width from the position.
new Rect(Screen.width/2 - 300, 100, 600, 600);
Answer by Rgalaxy · Jan 08, 2014 at 03:37 PM
i've trying to accomplish this.. i think its not Screen.width/2 - 300
i try to find the math but i comes up with this to make it center of the screen..
new Rect(Screen.width-(Screen.width-300))/2 , Screen.height-(Screen.height-300))/2 , 600,600);
this should makes the box which is 600x600 placed on the center of the screen. CMIIW..
Your answer
Follow this Question
Related Questions
GUI Window problem 2 Answers
GUI Window on GameObject location? 1 Answer
Stop clicking through a GUI window 2 Answers
Don't show window close button 1 Answer
GUI.Window only shows for one frame under all circumstances 2 Answers