Mini Tutorial: How to create only one set of GUI for different iOS resolutions in Unity3D?

So, there’s the old iPhone resolution (480×320) and then there’s the retina (960×640). Question is, how do you just create one set of GUI that can fit both resolutions? Because you know scaling and adjusting the GUI elements is such a bother.

Code bit:

public float screenWidth = 960;
public float screenHeight = 640;

private Matrix4x4 tMatrix;

void Awake ()
{    RotateDevice();
void RotateDevice()
{    // Calculate the transformation matrix
     // for the actual device screen size
     tMatrix  = Matrix4x4.TRS(, Quaternion.identity, new Vector3((float)1.0 * Screen.width/screenWidth, (float) 1.0 * Screen.height/screenHeight, 1.0f));


screenWidth and screenHeight is the resolution that you are designing your GUI to, I prefer to scale down, that’s why I set it to 960×640.

In OnGUI add:

GUI.matrix = tMatrix;

GUI.matrix will do all the scaling and transformations and whatnots for you.

And now in your GUI code anywhere you want to refer to Screen.width or Screen.height use screenWidth or screenHeight instead:

if(GUI.Button(new Rect(screenWidth-50, screenHeight-30, 50, 30), “Click Button”))
{   // some code

So now, your GUI works for both 960×640 and 480×320.

I got this tip from “Unity iOS Essentials” book :). Although I couldn’t get the code in the book to work as is, so I had to search the forums and stuff to fix the code.

That’s it 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s