Now you can Subscribe using RSS

Submit your Email

Monday

Show GUI components in OnGUI method and change Font, Size and Color | Unity Tutorial

Vishal Shrestha
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:
gui-properties-unity
GUI Components properties Unity
1. 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);

2. 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);

3. 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.
4. 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!

Vishal Shrestha / Author & Founder

A developer by profession, a born Adventurer. I mainly do Android but like to get my hands dirty with web development and a little bit of Python. I would't rather go on a Trek than a party and you can find me having a few rounds with the heavy bag to let out the steam ;)

For Business info : My Portfolio Site.

0 comments:

Post a Comment

Coprights @ 2017 | The Code City by Vishal Shrestha Vishal Shrestha