Scaling unity OnGUI to aspect ratio


Author: Aaron King


This week the only thing that was more challenging to figure out was getting custom the custom GUI for the cheat code window to scale correctly with the current aspect ratio. The initial was I was setting things up was hard coding the boarders that the GUI could take up, and this worked fine for setting it up and getting everything working. But the issue came up when we added the ability to change the aspect ratio in game. Since it was all hard coded to be the correct size for just 16:9 aspect when you changed to the larger formats we are offering it became almost to small to even see.  And when I added buttons for using the cheats via the same OnGUI call, they where impossible to click on while also knowing what button it was you where clicking on.


The solution that I found was really simple but worked really well in my personal testing of it. I was able to take the current screen dimensions and divide them by a constant factor to set the borders and size of all the GUI. This pertained to not only the display boxes but the text, scroll view for the cheat buttons and the buttons them selfs. This way even if we end up adding in more aspect ratio in the future this side of the code should be able to accommodate for  any new aspects with out any more work on my end.

Leave a comment

Log in with itch.io to leave a comment.