- Home /
Get Area to Spread Equally with Screen.Height or Screen.Width
Hey guys, I am trying to build an Area that works with the screen width (so it adjusts to resolution), but the Area expands to the right instead of staying in the center and spreading out equally. Any idea of how to do this correctly?
GUILayout.BeginArea(new Rect((Screen.width/2), (Screen.height/2), Screen.height * (3f/5f), Screen.width * (0.5f/5f)), GUIContent.none, "box");
Thank you in advance!
That's because first two values of new Rect represent the top left coordinates from where the Rectangle starts and the other two the dimensions of it.
http://docs.unity3d.com/Documentation/ScriptReference/Rect-ctor.html http://docs.unity3d.com/Documentation/ScriptReference/Rect.html
You'll also have to adjust the first two to change the position accordingly to the the size.
Okay I'll look into it, although this didn't really help me that much into getting it perfectly into the center. But thanks!
If you have something of some width (w) and some height (h), you want to center using GUI, you build the Rect as:
new Rect(Screen.width/2f - w/2, Screen.height/2 - h/2, w, h)
Answer by tw1st3d · Oct 03, 2013 at 07:29 PM
Here, try this. Instead of using 3f/5f, try using a direct fraction.
GUILayout.BeginArea(new Rect((Screen.width/2), (Screen.height/2), Screen.width * 0.6f, Screen.height * 0.1), GUIContent.none, "box");
Nope has to be a divided thing or its not gonna work I believe, at least I get compiler errors while the other method has always worked.
Oh whoops, I missed an f after 0.1. Try doing Screen.height * 0.1f
Answer by Jamora · Oct 03, 2013 at 07:50 PM
If you want the Rect for your Area to be perfectly centered at all times, do
aRect.center = new Vector2(Screen.width/2,Screen.height/2);
GUI.Box(aRect,"CenteredBox");
Now you can edit aRect.width and aRect.height while keeping the Rect centered.
Interesting...but will this work just like GUI Layout (that buttons will adjust to it, etc.)?
It will work for GUILayout, too. Just remember to set the center before drawing the area and you're set.
aRect.center = new Vector2(Screen.width/2,Screen.height/2);
GUILayout.BeginArea(aRect,"","Box");
GUILayout.EndArea();
Okay so I did this, but I get compiler errors. First, even with the variable aRect defined, the console spits out "aRect" does not exist in the current context. Also, the GUILayout.BeginArea doesnt really seem to harmonize with aRect as well. But thanks for your answer :D If you want I can post a bigger code sample maybe Im doing it wrong.
Please do post your code. There must be something wrong because the code works for me.