Ian094
November 1, 2013, 9:35am
1
I want to position my pause GUI at the top center of ANY screen. I have this code that works when i’m using HVGA 480x320, but when i switch to any other resolution like Free Aspect, it’s centered but not at the top.
Code :
//make a pause button
GUI.skin = pauseButton;
GUI.Box(Rect(Screen.width / 2, Screen.height - Screen.height,248,46),"");
}
Does anyone know how to go about this?
Try multiplying the values in:
Exemple:
GUI.Box(Rect(Screen.width/2-100, Screen.height+50 , 248, 46),"");
Try change this values… : -100 and +50 for to the position you want.
hpjohn
November 1, 2013, 11:34am
3
the TOP of the screen is zero, not screen.height
Ian094
November 1, 2013, 11:55am
4
Try multiplying the values in:
Exemple:
GUI.Box(Rect(Screen.width/2-100, Screen.height+50 , 248, 46),"");
Try change this values… : -100 and +50 for to the position you want.
Thanks for the reply. I’ve been trying out your code and it works fine, but i can’t seem to make the GUI stick at the top-center regardless of the resolution!
I want it to always be at the top center regardless of the screen size resolution. Any ideas?
Ian094
November 1, 2013, 12:02pm
5
Just tried replacing screen.height with 0 and the GUI got lower.
dkozar
November 2, 2013, 5:20pm
6
GUI.Box(Rect(Screen.width/2-100, 50 , 200, 46),""); // x, y, width, height (assuming the box width is 200, and it's 50 pixels from top)
Note that the y coordinate doesn’t need to vary (is fixed ad 50).
hpjohn
November 2, 2013, 5:32pm
7
Then you are absolutely doing something way off kilter (wrong)
docs.unity3d.com/Documentation/Components/gui-Basics.html
Ian094
November 2, 2013, 5:48pm
8
dkozar:
GUI.Box(Rect(Screen.width/2-100, 50 , 200, 46),""); // x, y, width, height (assuming the box width is 200, and it's 50 pixels from top)
Note that the y coordinate doesn’t need to vary (is fixed ad 50).
Awesome! Finally got it to stick to the Top Center regardless of the resolution! I edited the GUI on Photoshop so i had to tweak the width height.
Here’s the code I’m using for anyone going through the same problem i had :
GUI.Box(Rect (Screen.width /2 - 50,0,80,50),"");
dkozar
November 2, 2013, 6:16pm
9
Ian094:
Awesome! Finally got it to stick to the Top Center regardless of the resolution! I edited the GUI on Photoshop so i had to tweak the width height.
Here’s the code I’m using for anyone going through the same problem i had :
GUI.Box(Rect (Screen.width /2 - 50,0,80,50),"");
As you can see, your GUI isn’t perfectly centered. It’s a bit to the left.
That’s because you are using the width of 80 and a half-width of 50.
You should really get used to variables instead of the hard-coded values.
Your script could also expose some variables so you can tweak them in the editor:
var BoxWidth = 100;
var BoxHeight = 50;
var PaddingTop = 0;
function OnGUI() {
GUI.Box(Rect (Screen.width/2 - BoxWidth/2, PaddingTop , BoxWidth, BoxHeight),"");
}
Ian094
November 2, 2013, 6:35pm
10
Yeah, It actually is a little to the left. Your keen.
I’ll take your advice and tweak the values whilst in game mode. Thanks
goat1
November 2, 2013, 6:40pm
11
Looks like that should work but you need to not use ‘Screen.height’ use 50 (or 20 or …) for the vertical position and for the horizontal position instead of -100 it should be -(your GUI width/2)…
…if your GUI width is variable then calculate the GUI width before place to use in formula…