S
S
Surface-ketch2016-04-16 17:07:32
Unity
Surface-ketch, 2016-04-16 17:07:32

Resize/rescale text in unity 5?

Good day.
There is an account output in the center of the screen:

void OnGUI()
    {
        GUI.Label(new Rect(width*0.5f-50, 0, 100, 30), scores.score.ToString(), myStyle);
    }

The font size is 40. Everything is fine on a PC, but on a mobile device the text is very small. How to fix? Found a solution using matrix4x4
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);
    }
  

}

Here the text changes its size when the window changes, but the position in the center goes astray.
What are the solutions?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daniil Basmanov, 2016-04-17
@Surface-ketch

The unit has had a normal UI for two years already, which can scale depending on the screen size, with it this task is solved in an elementary way: Designing UI for Multiple Resolutions

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question