The cover for Text Mesh Pro in Unity: Font Import Guide

Text Mesh Pro in Unity: Font Import Guide

May 26, 2023

What you will learn

This tutorial will cover the difference between Text Mesh Pro fonts and standard fonts in Unity. Then, this tutorial will explain how to import a font to Unity to use it with Text Mesh Pro. Finally, it will provide recommendations for where you can find custom fonts that you can add to Unity.

TL;DR

What is the difference between TMP Fonts and standard Unity fonts?

TMP fonts are better; use them.

How to Import a TMP Font

It is easy to import a Text Mesh Pro-compatible font. Here’s how:

  1. Import the .ttf or .otf file to your project
  2. Select the font file
  3. Press CTRL + SHIFT + F12 to create a new font asset
  4. Open the font asset
  5. Click Update Atlas Texture
  6. Change the Character Set
  7. Click Generate Font Atlas
  8. Assign the font to a TMP component.

Where to find custom fonts

You can download TMP-compatible fonts from the Unity Store or from Google Fonts.

Discover the Power of Text Mesh Pro Fonts in Unity

In Unity, choosing the right fonts for your project can have a significant impact on the visual appeal and readability of your text. While Unity’s default text renderer gets the job done, it may not provide the level of quality and versatility you desire.

That’s where Text Mesh Pro (TMP) comes in. TMP is an alternative text renderer in Unity that offers enhanced visual fidelity and advanced features for creating better text elements in your games or applications, especially compared to standard Unity fonts.

In this tutorial, we will explore the differences between Text Mesh Pro fonts and standard fonts in Unity, and explain why Text Mesh Pro is often the preferred choice. We’ll walk you through the process of importing a font into Unity and using it with Text Mesh Pro so that you can use this text rendering solution.

Additionally, we’ll provide recommendations on where to find custom fonts to add a unique touch to your Unity projects.

Whether you’re a game developer, UI designer, or simply interested in optimizing text rendering in Unity, this tutorial will equip you with the knowledge and tools to improve the visual impact of your text elements.

Text Mesh Pro Fonts vs. Standard Fonts vs. Unity fonts

Most fonts are stored in one of several formats: TrueType (.ttf), OpenType (.otf), and Web Open Font Format (.woff / .woff2). Under the hood, formats like TrueType describe each character using a series of line segments and curves - not pixels. In this context, a character is called a glyph.

Unity includes a text renderer by default (i.e., “Unity Fonts”), but it doesn’t look good and offers limited features. Unity’s default text renderer is directly compatible with TrueType and OpenType fonts.

You just drag and drop the font into the text renderer. Under the hood, Unity converts the font to a texture atlas and samples that texture atlas during rendering. Unity renders the character glyphs directly to the texture atlas, so the character glyph is scaled to a particular font resolution.

Text Mesh Pro is an alternative text renderer that looks much nicer. It is sometimes abbreviated as TMP. Text Mesh Pro is not directly compatible with these common font formats. Instead, Text Mesh Pro requires you to pre-generate an SDF font atlas, which is a special texture file that Text Mesh Pro can create using a standard font format - TrueType or OpenType.

In contrast to Unity’s default texture atlas, the TMP font atlas uses a distance field for each glyph. This approach enables TMP to render fonts more smoothly at varying sizes and also makes it easier to render outlines or other text effects.

In the next section, this tutorial will review how to use Text Mesh Pro to import a standard font format into Unity.

Importing Fonts to Unity using Text Mesh Pro

Overview

With Text Mesh Pro, you can use the power of Text Mesh Pro Fonts, which render as Signed Distance Fields (SDFs), to achieve smooth and high-quality font rendering in your Unity project.

Signed Distance Fields are a powerful tool in graphics and rendering for games. A Signed Distance Field is a texture or formula where each point describes the distance from that point to a surface.

It is fairly easy to import a font into Unity for use with Text Mesh Pro.

This section you will learn how to import a font to Unity, how to use Text Mesh Pro to create a font asset, and how to use Text Mesh Pro to generate a font atlas.

Then, we will cover how to create a Text Mesh Pro GameObject and how to apply the Text Mesh Pro font asset to that GameObject.

Install Text Mesh Pro

Text Mesh Pro is included with Unity - you do not need to install anything or import any packages. If you previously removed Text Mesh Pro from your project, you can import it from the Package Manager.

Setup

If you already have a font asset (a .ttf or .otf file) in your project, skip this step.

If you don’t have a font asset in your project, you need to download one. To follow along, download Roboto from Google Fonts now. Go to the Roboto page on Google Fonts: https://fonts.google.com/share?selection.family=Roboto.

Click Download All, then extract the .zip file, and then drag and drop the Roboto-Regular.ttf file from the directory into your Unity project.

Import the .ttf file

You don’t really need to do anything with the .ttf file.

Use Text Mesh Pro to create a TMP Font Asset with the .ttf file

Once you have the font file (.ttf or .otf) imported into your Unity project, create a new Unity Font Asset with Text Mesh Pro.

To import a font into Unity for use with Text Mesh Pro, you need to create a Unity Font Asset using Text Mesh Pro. The Unity Font Asset serves as a container for your imported font, allowing you to leverage its features and apply it to Text Mesh Pro components in your project.

You can use the keyboard shortcut or the context menu to use Text Mesh Pro to create a Text Mesh Pro font asset from the .ttf file that you imported.

To use the keyboard shortcut: Click on the .ttf file, then press CTRL + SHIFT + F12.

To use the context menu: Right click on the .ttf file -> Create -> TextMeshPro -> Font Asset.

As mentioned previously, Text Mesh Pro uses a font atlas to render the glyphs to screen. When you create the Font Asset, Text Mesh Pro does not automatically create the font atlas.

So, you still need to build the font atlas within this Text Mesh Pro font asset in order to use the font with Text Mesh Pro.

Build the Font Atlas

Click on the font asset in your project. In the Inspector, click “Update Atlas Texture”.

Text Mesh Pro will open a new editor window called the Font Asset Creator.

The default settings are mostly fine for now, but you do need to change the Character Set from Unicode Range (Hex) to ASCII or Extended ASCII. The Unicode Range option expects you to provide a list of Unicodes that Text Mesh Pro should import. The ASCII and Extended ASCII options will automatically import all standard font glyphs that are included in the font file. Do that now.

Then, click “Generate Font Atlas”. Text Mesh Pro will quickly generate a font atlas for this font. Once it’s done, click Save. Your font asset is now configured and ready to use.

Create Text Mesh Pro GameObject

To use your new font asset, you need a Text Mesh Pro component in your project.

In your toolbar, GameObject -> UI -> Text - TextMeshPro.

Click on the new GameObject to view it in the Inspector.

Then, assign your Text Mesh Pro font asset to the Font Asset field in the Inspector.

Adding Custom Fonts to Unity

Overview on Finding Fonts

This section will provide you with some examples of websites where you can find fonts for your project. Keep in mind that each font may have a different license, and different websites offer different licensing terms for various fonts.

Google Fonts

https://fonts.google.com/

Google Fonts has a reliable and large library of open or open-ish fonts. You can use many (most) of these fonts for commercial use. Make sure to check the licenses for any attribution requirements. Google Fonts also has a great set of articles and FAQs on how to choose a good font, how to use your font appropriately, and how to set up hierarchy with fonts.

The League of Movable Type

https://www.theleagueofmoveabletype.com/

The League of Movable Type is an open-source font foundry. (A font foundry is an organization that creates new fonts). They have some good stuff. It’s open-source.

Font Squirrel

https://www.fontsquirrel.com/

Font Squirrel is a directory of licensed-for-commercial-use fonts. Many of these fonts are also available on Google Fonts. Pick whichever UI you prefer.

Font Licensing

This is not legal advice. In general, you will want to look for fonts that are licensed under the Open Font License (OFL). These fonts are open-source, so they are free to use for personal and commercial use. You can purchase fonts, but paid fonts can be expensive, and licensing restrictions can be tricky.

System Font Rendering

I briefly covered this during the article, but I’ll reiterate it here. Unity does not use the system font renderer to draw text in the engine. For example, in Windows, most applications use DirectWrite (DWrite), a Direct2D API, to rasterize and render text. Let’s call this the native API. In contrast, Unity has its own API (Text, Text Mesh Pro) that rasterizes and renders the font.

If your font in Unity looks blurry, weird, or different than you expect compared to how it appears on the web, on your desktop, or in the font file inspector, this could be the reason why.

It is possible that Unity does not use DWrite or other system-level font rasterizers for performance reasons or for cross-compatibility and consistency reasons.

Improve Your Unity Projects with Text Mesh Pro Fonts and Unity Font Assets

In this tutorial, we have explored the differences between Text Mesh Pro fonts and standard fonts in Unity, highlighting the advantages of using Text Mesh Pro for enhanced visual quality and advanced typographical options. We’ve walked you through the step-by-step process of importing a font into Unity and creating a Text Mesh Pro font asset, enabling you to use this powerful text renderer.

Additionally, we have provided recommendations on where to find custom fonts, including reliable sources like Google Fonts, The League of Movable Type, and Font Squirrel. These resources offer a wide range of open or open-ish fonts, allowing you to add a personalized touch to your Unity projects.

By understanding the distinctions between Text Mesh Pro and standard fonts, and by leveraging the capabilities of Text Mesh Pro, you can improve the visual impact and readability of text elements in your games or applications.

Remember, while Unity’s default text renderer serves its purpose, Text Mesh Pro provides a superior alternative with its smooth font rendering and extensive features. So don’t hesitate to dive into the world of Text Mesh Pro fonts and unlock the full potential of text in your Unity creations.

Thank you for joining me on this tutorial, and I hope it has empowered you to create visually stunning text elements in your Unity projects. If you have any further questions or would like to delve deeper into game development, feel free to join our game dev Discord server.

Happy text rendering and game development!

Further Reading

A Brief Review of Font Rendering

https://www.smashingmagazine.com/2012/04/a-closer-look-at-font-rendering/

It’s not a requirement, but it is good to understand what is happening when you render a font glyph - both on your OS and in Unity. Smashing Magazine has written a fairly solid introduction to font rendering.

More on Font Features

https://developer.mozilla.org/en-US/docs/Web/CSS/font-feature-settings

Text Mesh Pro offers you the option to enable Font Features. Font Features are advanced typographical options like small caps, ligatures, and tabular (mono-spaced) numerals.

Unity Font Assets Documentation

https://docs.unity3d.com/2021.3/Documentation/Manual/class-Font.html

Unity has good documentation on their manual that covers how to import and use font assets for the default text renderer. I recommend that you never use the default text renderer.

Text Mesh Pro Documentation

https://docs.unity3d.com/Packages/com.unity.textmeshpro@4.0/manual/index.html

Unity has good documentation on their Text Mesh Pro package that covers a whole lot more than what I addressed in this article. If you are going to use Text Mesh Pro (and you should use Text Mesh Pro), then bookmark the documentation and spend some time reading through it.

More on Font Licensing

Overview on Font Licensing

Font licensing is an essential aspect to consider when using fonts in your projects, whether they are for personal or commercial use. Fonts, like any other creative work, are protected by copyright law, which grants exclusive rights to the creator or copyright holder. Understanding font licensing helps ensure that you use fonts legally and respect the rights of font designers and creators.

When selecting fonts for your Unity projects, it’s important to consider the following aspects.

License Types

Fonts are often distributed under various license types, each with specific terms and conditions. The most common font license types include open-source licenses like the Open Font License (OFL), commercial licenses, and freeware licenses. It’s crucial to review the license terms associated with the fonts you use to determine if they align with your intended usage.

Usage Restrictions

Font licenses may have specific restrictions on usage, such as limitations on the number of installations, geographical restrictions, or restrictions on commercial use. Make sure to carefully review the license terms to ensure your usage complies with the font’s license restrictions.

Attribution Requirements

Some fonts require attribution to the font designer or copyright holder as a condition of use. This means you need to credit the font creator in your project, typically by including their name or a designated attribution statement. Be sure to check if the font you want to use has any attribution requirements and fulfill them accordingly.

Redistribution and Modification

Fonts may have restrictions on redistribution or modification. Some fonts may allow redistribution, enabling you to share the font files with others, while others may restrict redistribution entirely. Similarly, modification rights vary depending on the font’s license. It’s crucial to respect these restrictions and only redistribute or modify fonts within the boundaries of the license terms.

Commercial Use

If you intend to use fonts for commercial purposes, ensure that the font’s license permits such usage. Some fonts are specifically licensed for non-commercial or personal projects only. Verifying the font’s license compatibility with your commercial use is crucial to avoid legal complications down the line.

Conclusion

By being mindful of font licensing and adhering to the terms and conditions set by font creators, you can ensure that you use fonts legally and respect the rights of the font design community. When in doubt, review the license information provided with the font or contact the font designer or copyright holder directly for clarification.

Remember, font licensing is important to protect the intellectual property rights of font creators and promote fair usage of their work. So, as you explore and integrate fonts into your Unity projects, be sure to pay attention to font licenses and use them in accordance with the applicable terms and conditions.

Please note that this explanation provides general guidance and is not legal advice. For specific legal concerns regarding font licensing, you should always consult a legal professional or refer to the specific license agreements associated with the fonts you intend to use.

Want to build better games?

© 2024