- Home /
GUI scaling on mobile
Hello. I'm using a standard Unity GUI. I am trying to scale it to make my game playable on devices with different resolution. The problem is I can not figure out how to scale GUI properly according to the DPI of device's screen.
I tried this method https://gist.github.com/darktable/2018687 but for some reason it doesn't work for me on my mobile phone but on PC it does work (but if I change the resolution at runtime it stops working).
I am attaching two screenshots. The first one is from my mobile phone and the second is from my PC!
Answer by perchik · Sep 06, 2013 at 01:56 PM
What I've always done is to use relative sizes in my GUI. Instead of saying this box is 50px wide, I say this box is 1.2% of Screen.Width or something similar. That way, when the screen size changes, the box changes too. Although, this is quite a painful way to do things..I usually start with my GUI laid out exactly the way I want it, absolutely, then go through and convert it to relative (by dividing the absolute numbers by the current Screen.Width)
Perhaps you could use Screen.dpi to get a better result though.
Thank you for your reply. However, if I use this method, I will have to scale the GUI's fontSize. Is it possible at runtime?
I think you have to change the font size for each element, ie GUI.skin.label.fontSize = 18
, GUI.skin.box.fontSize = 10` etc.
If I remember correctly, the trick with scaling font size is that you have to use $$anonymous$$athf.RoundToInt to round any floating point numbers to an integer, since fontSize takes an integer.
A better approach would be to create a second GUI Skin for mobile, and then switch out the whole skin when you're on a mobile device (with all your new font sizes written in the GUISkin)
I kinda hoped someone would post a better answer because relative size GUI's are nasty.
You know,
Another option is to use GUILayout ins$$anonymous$$d of GUI. Then you don't have to worry about fixed sizes, but you would still need a different font size for mobile
Could you please provide a little example of how to use the GUILayout correctly to solve my problem with scaling?