- Home /
Is this the correct way to scale my GUI?
I'm looking to make my game multi platform, and work on multiple resolutions/aspect ratios. For the GUI, I'm using onGUI.
Initially, I had made every element follow this pattern
Position.x = screen.width * something
position.y = screen.height * something
width = screen.width * something
height = screen.height * something
But I found that it wasnt scaling very well with different resolutions, and was also ruining the images texture quality.
So then I found a different way to do it.
Position.x = screen.width * something
position.y = screen.height * something
width = constant (say, 60)
height = constant (say, 60)
This seems to work fine for different resolutions, in the editor. But I am concerned whether this method is foolproof, and if it the correct way to go about things? Please let me know if this is correct or no. And if not, let me also know the BEST way to resize/scale/reposition GUI elements for multi resolutions
In our previous project, we figured out that this approach is valid as long as the different devices have a ratio pretty similar.
But in the end, we had to use some macros to have proper layout.
Fact is, a phone (appro 16:10 I think) and a tab (approx 5:4 I think) have total different ratio and what looks ok on the iPad will look compressed on the phone.
So this is ok as long as ratio remains pretty equivalent.
Please let me know more about the "macros" you are referring to. Because I have to go for something like say 1:1 as well as 9:16 as well.
Answer by MFen · Oct 08, 2013 at 02:14 PM
I personally like to use xRatio and yRatio like so
xRatio = screen.width / 960; //960 is default screen size
yRatio = screen.heigth / 600; //600 is default screen size
Rect guiRect = new Rect (0,0, 200 * xRatio, 200 * yRatio);
So what exactly is this bit of code doing? What I mean is, how is it scaling my stuff perfectly?
This method sets up your GUI so that it will always maintain the correct shape and will resize depending on size of the screen.
So lets say I set it to 1280/720 does that mean that my game will always work according to 1280/720 coordinates?
I tried this out, and it's not working as expected. I used 60 xRatio, 60 yRatio, using 720 and 1280 as my native coordinates. Now, the resolution set by me in the editor was 1280 x 720 And the GUI is scaling to about 50% of its desired size. When I'm printing out Screen.width, I'm getting 335. But I should be getting 720, no?
Still waiting for an answer.
If it helps, I'm using an orthographc camera