- Home /
Scaling GUI.Labels to screen
Hi all,
I only just managed to get most of my game to correctly scale on different Android devices. GameObjects and such scale fine, but GUI.Labels fail to shrink in size along with them, making them clutter much more of the screen than they need to.
The POSITION of the text is fine. It's just the text size that won't adjust.
Here is a sample of my code:
void OnGUI()
{
GUIStyle myStyle = new GUIStyle();
myStyle.font = myFont;
myStyle.fontSize = 40;
myStyle.normal.textColor = Color.white;
myStyle.alignment = TextAnchor.UpperLeft;
GUI.Label (new Rect (10, textYPos, 500, 20), "TIME : " +
timer.ToString("F2"), myStyle);
}
Anyone have a solution?
As everyone has pointed out, just download 4.6 http://unity3d.com/unity/beta/4.6 and it's all trivial (thank goodness) at last
Answer by zharik86 · Sep 02, 2014 at 07:38 AM
For your case best of all the following code will approach. For example, you have LandScape orientation and for the size screen 1920 the size of the text is equal 40. Then:
void OnGUI() {
GUIStyle myStyle = new GUIStyle();
myStyle.font = myFont;
myStyle.fontSize = (int)(40.0f * (float)(Screen.width)/1920.0f); //scale size font
myStyle.normal.textColor = Color.white;
myStyle.alignment = TextAnchor.UpperLeft;
GUI.Label (new Rect (10, textYPos, 500, 20), "TIME : " + timer.ToString("F2"), myStyle);
}
Probably, it is better for you to use GUI.matrix for scaling of all GUI elements. I hope that it will help you.
Thanks, this seemed to do the trick! Only problem is now my text IS moving around a bit with the size changes. I'll mess around a bit and see if I can fix it myself.
Your answer
Follow this Question
Related Questions
Can't draw GUI.Label text on subpixel values 0 Answers
Problem with centering text 1 Answer
GUI Label Width & Height 0 Answers
GUI.Label or any text with GUIStyle as Texture 1 Answer
OnGUI button changes size for resolution, but text doesnt? 1 Answer