How To Show GUI components in OnGUI method and change Font, Size and Color in Unity

Unity provides a very helpful method, OnGui that can be used to create UI components easily and you can easily remove them my just a simple condition checking, you have probably done that and just want want the title describes but I can’t resist from showing how simple it is to just show/hide window components and register button click. You just create components like this in the onGUI method:

void onGUI(){
if (GUI.Button (Rect (20,20,250,200), "I am that Button"))
    print("And I just got pressed!");
}

And now, let’s say you want to hide the button when your player is dead or any other event, you just do this:

void onGUI(){
if(!isUserDeadOrAnyOtherEvent) 
  if (GUI.Button (Rect (20,20,250,200), "I am that Button"))
    print("And I just got pressed!");
}

Now, the button will simply be shown only when the condition above is met, that is only when user is not dead or any other condition that needs to be satisfied.
Alright, that was a brief intro to how you can create GUI components, now we will see how we can change their look via their properties:

  1. Change Text Color of Labels in OnGUI
  2. Changing Font Size in OnGUI in Unity
  3. Changing Font in OnGUI in Unity
  4. Changing Text Alignment in OnGUI in Unity
GUI Components properties Unity

Changing Text color of Labels in OnGUI in Unity

GUIStyle guiStyle = new GUIStyle(GUI.skin.label);
guiStyle.normal.textColor = Color.white;
GUI.Label(new Rect(0, 0, 800, 300), "I am the White Label", guiStyle);  

Changing Font Size in OnGUI in Unity

GUIStyle guiStyle = new GUIStyle(GUI.skin.label);
guiStyle.fontSize = 36;
GUI.Label(new Rect(0, 0, 800, 300), "I am the White Label", guiStyle);  

Changing Font in OnGUI in Unity

// You may put a label to show a message to the player
GUIStyle guiStyle = new GUIStyle(GUI.skin.label);
// Load and set Font
Font myFont = (Font)Resources.Load("Fonts/AMERICANCAPTAINWEBFONT", typeof(Font));
guiStyle.font = myFont;  
GUI.Label(new Rect(0, 0, 800, 300), "I am the White Label", guiStyle);  

Note that you need to put you fonts in a Fonts folder inside Assets folder in order for this to work.

Changing Text Alignment in OnGUI in Unity

GUIStyle guiStyle = new GUIStyle(GUI.skin.label);
guiStyle.alignment = TextAnchor.MiddleCenter;
GUI.Label(new Rect(0, 0, 800, 300), "I am the White Label", guiStyle); 

Now, you know how you can change Text color, size, font and Alignment in Unity. You can include all these properties like this:

GUIStyle guiStyle = new GUIStyle(GUI.skin.label);
guiStyle.fontSize = 36;
guiStyle.normal.textColor = Color.white;
//Load and set Font
Font myFont = (Font)Resources.Load("Fonts/AMERICANCAPTAINWEBFONT", typeof(Font));
guiStyle.font = myFont;
guiStyle.alignment = TextAnchor.MiddleCenter; 

This it it, it’s all pretty simple. Feel free to drop any comments or queries you have below. Happy Coding!

Don’t miss these tips!

We don’t spam! Read our privacy policy for more info.

You’ve been successfully subscribed to our newsletter! See you on the other side!

Sharing is caring!

1 thought on “How To Show GUI components in OnGUI method and change Font, Size and Color in Unity”

Leave a Comment

Your email address will not be published.

Exit mobile version