Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change guilayout.label font size?

private void OnGUI()
    {
        GUIStyle myStyle = new GUIStyle();
        myStyle.fontSize = 20;

        GUILayout.BeginVertical(GUI.skin.box);
        GUILayout.Label("Replacing");
        GUI.Label(new Rect(650, 650, 300, 50), "HELLO WORLD", myStyle);
    }

I see the Replacing label but it's font very small. So I wanted to test changing the size using new Rect but I don't see the HELLO WORLD anywhere.

like image 610
Daniel Lip Avatar asked Nov 15 '25 05:11

Daniel Lip


2 Answers

I am using two options: this

GUIStyle headStyle = new GUIStyle();
headStyle.fontSize = 30; 
GUI.Label(new Rect(Screen.width / 3, Screen.height / 2, 300, 50), "HELLO WORLD", headStyle);

or this

GUI.skin.label.fontSize = 30;
GUILayout.Label("HELLO WORLD", GUILayout.Width(300), GUILayout.Height(50)))
like image 96
rusyaka84 Avatar answered Nov 17 '25 20:11

rusyaka84


The Text is showing but not visible to you because of the values you passed to the Rect struct. The value of 650 and 650 that is passed to the Rect struct in the x,y argument seem to be bigger than the actual screen size. Reduce it to about 100 and you should be able to see the Text:

GUI.Label(new Rect(100, 100, 300, 20), "Hello World!", myStyle);

If you want your UI display to be dynamic, I suggest you use Screen.height and Screen.height to determine where to place the GUI element and make sure that it works regardless the size of the screen.

For example:

GUI.Label(new Rect(Screen.height / 2, Screen.height / 2, 300, 20), "Hello World!", myStyle);

Finally, I assume this is an Editor code based on your other questions. If it's not then you should be using the new UI system with the Text component.

like image 34
Programmer Avatar answered Nov 17 '25 20:11

Programmer