- Home /
Unity3d GUI Element Scaling
I'm looking for the best way to scale GUI elements in unity. In particular, I need to scale GUIText and GUITexture elements so they are able to be seen in higher resolutions.
Answer by Ashkan_gc · Apr 27, 2011 at 03:16 AM
the best approach is to design the gui in a high resolution and then scale it down in lower ones. using GUI.matrix you can move/rotate/scale the gui. the code is available in 3d platformer tutorial of unity too. my gui for the game was designed for 600 X 450 so the first line in OnGUI was
GUI.matrix = Matrix4x4.TRS(Vector3.zero,Quaternion.identity,new Vector3(Screen.width/600f,Screen.height/450f,1f));
you can replace 600 and 450 with native resolutions of your gui. this line should be the first one in OnGUI to affect all gui calls after it. if you want to scale some of the elements then you need to set the matrix to something like this before calling them (for example before GUI.TextField) and after calling the element's function then scale the matrix to a normal 1X1X1 scale.
So the size of GUI elements were made to the size you wanted them to be at 600 x 450 and then were scaled up/down based on that size and what the new resolution is?
yes the GUI will scale to the new resolution so it's the same size in your screen compared to other items. for example if you made a button big enough to fill half of the screen then in all resolutions it will be half of the screen and gui will become resolution independent. screen/width/600 means the aspect of the new width compared to the 600 so if the new width is 800 then the gui will scale %30 (1.333) in x axis and the same is true for height (y axis). take a look at 3d platformer tutorial in unity's website for more info.
Your answer
Follow this Question
Related Questions
GUITEXT over a GUIButton 3 Answers
How to make gui texture go behind gui text? 1 Answer
Drawing a GUI onto a 3D object 0 Answers
Drawing UI text on GUI.DrawTexture , How to do it ? 1 Answer
GUIText and GUITexture? 3 Answers