Using Icon Fonts in Unity

Posted on

in

,

In this example I’m using Font Awesome. First you download it and then drag the .ttf font file into your Resources folder. Now, there are 2 ways to add text: in the UI and in code. Let’s go through both.

Adding icons in the UI

Say you had a button in the hierarchy with a text field in it. Go to the text object and drag the .ttf font file into the ‘font’ space and then go to the cheatsheet and copy the actual icon character and paste it in ‘text’ space.

Adding icons in the code

An example of this would be for a GUI button. For this you would need to add a GUI style. Here’s an example:

public GUIStyle styleFontAwesome;

styleFontAwesome.font = (Font)Resources.Load("fontawesome-webfont");;
styleFontAwesome.normal.textColor = Color.white;
styleFontAwesome.alignment = TextAnchor.MiddleCenter;

Note, the string is the name of the file in your Resources folder and not the font name.

Using the cheatsheet again, grab the 4 characters after ‘&#x‘ and make a new character with it like so:

char leftArrow = '\uf060';

In this example, the 4 characters is ‘f060’.

Then apply it to your button like so:

if (GUI.RepeatButton (new Rect (buttonX, buttonYbuttonWidthbuttonHeight), leftArrow.ToString(),styleFontAwesome)) {
// do stuff when your button is pressed
        }

Leave a Reply

Your email address will not be published. Required fields are marked *

About me

Mark Wong is a front end developer with 10+ years experience. Most of his knowledge of HTML5, CSS and Js is self taught.

Calendar

April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930