Auto resize/rescale ongui
Hi there. I have a label in center of screen. Font size = 30. But on mobile devices, this text is very small.
void OnGUI()
{
GUI.Label(new Rect(width*0.5f-50, 0, 100, 30), scores.score.ToString(), myStyle);
}
I found one way to solve
public class Score1 : MonoBehaviour {
public ElipsController scores;
public GUIStyle myStyle;
float width;
void Update()
{
width = Screen.width;
}
public static void AutoResize(int screenWidth, int screenHeight)
{
Vector2 resizeRatio = new Vector2((float)Screen.width / screenWidth, (float)Screen.height / screenHeight);
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(resizeRatio.x, resizeRatio.y, 1.0f));
}
void OnGUI()
{
AutoResize(1024,768);
GUI.Label(new Rect(width*0.5f-50, 0, 100, 30), scores.score.ToString(), myStyle);
}
}
But now my text is not in the center of the screen. How fix it?
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612074941im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make gameobjects exact sizes regardless of image size,Game Object uses full width of screen 0 Answers
OnGUI() problems in unity 5.6, worked with Unity 4. 0 Answers
Duplicate GUI.Label Instances 1 Answer
On Trigger Display Text but centered not centered? 1 Answer
GUI Labels doesn't center anymore 0 Answers