简体   繁体   中英

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.

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)))

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM