So, there’s the old iPhone resolution (480x320) and then there’s the retina (960x640). 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(Vector3.zero, 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 960x640.
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 960x640 and 480x320.
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 :)
-
creative102 liked this
-
physicaleo4 liked this
-
longman09f liked this
-
available03 liked this
-
community788 liked this
-
techniquesui85 liked this
-
transientyou83 liked this
-
webcoderph liked this
-
purplelilgirl posted this


