How To Scale UI Across Multiple Resolutions?

I have done some looking around and have often come across this code snippet

  • float virtualWidth =1920.0f;
  • float virtualHeight =1080.0f;
  • voidOnGUI()
  • {
  • matrix =Matrix4x4.TRS (Vector3.zero,Quaternion.identity,Vector3(Screen.width/virtualWidth,Screen.height/virtualHeight,1.0f));
  • GUILayout.Label(“Woo”);
  • }

I am keen to give this a try, but I not been able to get it to work. On which object in my scene to I place this? Do I have to have a canvas set to screen space - camera or something? Thanks in advance for the help.

Better to use the new UI system rather than Legacy GUI. For Legacy GUI you’re going to have to mess around with Rendertargets and rescaling the GUI / input to fit. Not nice.

As a starter, check out this manual reference and then move on to the other UI tutorials.
http://docs.unity3d.com/Manual/HOWTO-UIMultiResolution.html

Not saying Legacy GUI is bad but it does have some severe limitations in this area.

I have played around with canvas scalers but I dislike how you are left with either having portions of the screen cut off or wasted screen space. Currently I use anchors in my project, having the anchors of almost every object set to the bounds of their rect transforms (I am writing a 2D project). I even wrote myself a script to allow me to set the with and heights of objects, as though the anchors are all in a single place, to make consistent sizing of objects easier. There are a few issues with this script currently and I find myself wondering if there is a better way. As of yet though, I haven’t come up with anything.

Not really and anchors / scallers only get you so far. If the dimensions are too different (landscape to portrait for example) it’s actually better to have 2 UI scenes and flip between them. With the new multi-scene setup, that is a lot simplier and means you can share the same scripts for both.

Here is the 5 minutes guide to design a responsive UI for multiple resolutions or aspect ratios.