Introduction to TextMesh Pro in Unity
- Getting Started
- Installing TextMesh Pro
- What Is TextMesh Pro?
- How Does TextMesh Pro Work?
- Setting up the Starter Project
- Getting Started With TextMesh Pro
- Setting Font Color and Gradient
- Adding Rich Text in TextMesh Pro
- Indenting Lines
- Using Overflow Settings
- Adding Inline Images to Text
- Adding Color to Inline Sprites
- Using Additional Rich Text Tags
- TextMesh Pro Font Assets
- Creating Font Assets
- Customizing TextMesh Pro Font Assets
- Creating Material Presets
- More Material Preset Settings
- Using Cardboard Presets
- Using Bevel Presets
- Using Glow Presets
- Where to Go From Here?
Learn about TextMesh Pro in Unity by creating a spooky text-based adventure game where the style of the text changes based on what happens in the story.
Have you ever looked at the UI in your Unity project and thought to yourself, “Why does this look so plain?” Many games have a great-looking UI but still use the default Unity text. BORING!
If you want a solution to bland-looking text, look no further than TextMesh Pro. In this tutorial, you’ll create a spooky text-based adventure game where the text style and format change based on what’s happening in the story. As you go on this adventure, you’ll learn about:
- TextMesh Pro’s basic use and styling options.
- Using Rich Text tags.
- How to create Font Assets.
- How to customize Material Presets.
By the time you’re done, you’ll see how TextMesh Pro gives your project the best-looking text, no matter what style you’re aiming for.
Download the starter project by clicking the Download Materials button at the top or bottom of the tutorial. Open the project in Unity 2020.3 or later.
Browse Assets/RW to find the assets used in this project. The subfolders contain the following:
- Fonts: Standard font files, TextMesh Pro Font Assets and Font Presets.
- Scenes: Starter scene for the text-based adventure game.
- Scripts: Helper script to advance the screens in the text-based adventure game.
- Sprites: Images and an icon used in the text-based adventure game.
- Textures: A cardboard texture you’ll apply to a font in the tutorial.
Now that you’ve had a look at the project, it’s time to get started with TextMesh Pro.
Installing TextMesh Pro
When Unity acquired TextMesh Pro in 2017, they bundled it right into their game engine, so you don’t need to download or install any extra files to use it.
If you don’t see the TextMesh Pro tools and components, try enabling the package in the Package Manager. To do this, navigate to Window ▸ Package Manager and follow these steps:
- On the Packages drop-down, select Unity Registry.
- Scroll down to TextMeshPro and select it.
- With the package selected, click Install to install the package to your project.
Next, import the TextMesh Pro Essential Resources into your project by selecting Window ▸ TextMeshPro ▸ Import TMP Essential Resources. Finally, import all the files in the Unity Package into your project. You’ll now see a subfolder called TextMesh Pro under Assets.
What Is TextMesh Pro?
In the early days of the Unity game engine, one method of adding text to your game was to use TextMesh. TextMesh, which came prepackaged with Unity, used bitmap fonts to display text on the screen.
TextMesh offered some basic styling and configuration options, but you were very limited regarding what you could create with it. Because the text was rendered using bitmaps, it began to appear fuzzy when enlarged. Also, the game’s frame rate dropped when you had many TextMesh objects on the screen at once.
To make up for the lack of styling options in TextMesh, Stephan Bouchard created a tool called TextMesh Pro, which he sold in the Unity Asset Store.
TextMesh Pro contained powerful styling tools that ran efficiently in Unity. This tool was so useful that, in 2017, Unity acquired TextMesh Pro and hired Stephan Bouchard. Ever since, they’ve included TextMesh Pro in all versions of Unity.
There are many reasons to use TextMesh Pro. The top three are:
- It looks great: The text looks good at any size.
- It’s infinitely configurable: You can make your text look exactly how you want it to.
- It’s easy to use: TextMesh Pro is built right into the Unity game engine. The styling options are intuitive, so you can learn them quickly.
How Does TextMesh Pro Work?
Throughout this tutorial, you’ll see how to set up and configure each of TextMesh Pro’s parts. But first, here’s a high-level overview to help you piece things together as you move along.
At the core of TextMesh Pro is the Font Asset. You create a Font Asset from a standard font file. All TextMesh Pro objects using that font will reference that Font Asset.
Each Font Asset generates a Texture Atlas that contains all the characters in each font. Material Presets you create and text you draw to the screen both reference the single Texture Atlas.
TextMesh Pro uses a Signed Distance Field to render text on the screen. How this feature works is out of the scope of this tutorial, but the result is that text appears sharp regardless of its relative size and distance from the camera. Traditional bitmap textures appear blurry at close draw distances and large sizes.
One last thing to note before you get started with TextMesh Pro is that there are two types of TextMesh Pro components, TextMeshPro and TextMeshProUGUI. TextMeshPro works with the MeshRenderer. It’s best used with text that should exist in world space, like an in-game sign that could be added to a scene.
TextMeshProUGUI works with the CanvasRenderer and the Canvas system, and is best for text that exists in screen space. It replaces the built-in Unity UI.Text component.
Other than their intended uses, there are no differences between the two types of components — they share the same configuration options.
In this tutorial, you’ll learn how to create and work with the TextMeshProUGUI component. You can find information about using the TextMeshPro component in Unity’s TextMesh Pro User Guide.
Setting up the Starter Project
Open the starter scene at Assets/RW/Scenes/IntoTheAttic-Starter.
This game runs in portrait mode, so change the aspect ratio of the Game View to 9:16. To do this, click the existing resolution or aspect ratio drop-down in the Game View, then click the + icon to add a new aspect ratio preset. Change the type to Aspect Ratio and enter a width of 9 and height of 16. Finally, click OK. Now, the game window will display in portrait mode.
The sample project, Into the Attic, is a text-based adventure game where you’ll change the styling of the text based on what’s happening in the story. Each screen of dialog has a TextMeshProUGUI component to display the text and a button to advance to the next screen.
These screens are child objects of the Screens GameObject in the scene. The screens’ names have incrementing numbers indicating the order they appear in the story.
On each screen, you’ll learn a new way to style text. To edit another screen, disable the current one then enable the desired screen. Make sure you leave both ScreenFader and Background active.
Getting Started With TextMesh Pro
Before you continue, switch from the Game view to the Scene view so you can see the Text and RectTransform areas on your screen. To match the screenshots, press the 2D button in the Scene view to switch to 2D mode and focus on the Screens GameObject.
Set 2-Intro as the active screen. To add a new TextMeshProUGUI object, right-click 2-Intro and select UI ▸ Text – TextMeshPro. This gives 2-Intro a child object called Text (TMP).
Select Text (TMP) and make the text area larger by changing the Anchor Presets on the Rect Transform component to stretch horizontally and vertically. Next, set left, right and top to 50, then bottom to 350.
To add text to the TextMeshProUGUI component, you need to type in the Text Input field. Add some introduction text to set the scene for the game: “One rainy night in the middle of January…”
The text now displays in the text area in the scene.
By default, the text is a bit plain. To fix this, you’ll change the standard styling options in Main Settings.
First, change the font by clicking the bullseye icon next to the existing Font Asset, then select NewTegomin-Regular SDF. The font immediately updates in the scene.
Next, perform the following steps:
- Make the text larger by changing Font Size to 150.
- Set Line Spacing to -50 to decrease the spacing between each line.
- Finally, center the text by selecting the Center and Middle alignment options.
The text now looks much more interesting. Ominous, even…
Setting Font Color and Gradient
Moving on, disable 2-Intro, then enable 3-Bang.
Select the child Text (TMP) and make the text stand out by:
- Changing the Font Style to bold and uppercase.
- Increasing Font Size to 300.
- Changing Vertex Color to yellow by clicking the field and choosing yellow in the color selector window.
That should wake a person up!
Next, set 5-Wind as the active screen.
Select Text (TMP) and make the text feel cold by adding a color gradient. Check Color Gradient, then change Color Mode to Four Corners Gradient.
Set the top two corners to white, the bottom left to cyan, and the bottom right to blue. Notice how the gradient applies across each individual character. Brrrr!
Adding Rich Text in TextMesh Pro
So far, the styling options you’ve worked with all apply to the entire text block. However, there are many cases where you only need to apply styling to a portion of the text. Maybe you need one sentence, word, or even character to look different than the rest. To do this, you can use Rich Text tags.
Rich Text tags work like HTML tags. Angle brackets —
<> — indicate an opening tag. Angle brackets with a forward slash
</> indicate a closing tag. The tag affects all text between the opening and closing brackets.
A common use of Rich Text tags is to italicize a portion of text. Test this out on screen 6-Crash.
In the Text Input field, place an italic opening tag
<i> before the word sharp and a closing tag
</i> after the word crash. The words sharp crash are now italicized, putting emphasis on those words.
Another common Rich Text tag is the color tag. This tag, along with several others, has properties that you can set within the tag.
On screen 7-Broken, make the words need to see red by adding the
</color> tags around those words.
<color=#ff0000ff>to specify the alpha value.
Another thing to note about Rich Text tags is that they support nesting. Try this out by putting the bold
</b> tags around the word need. Once complete, your text should look like this:
What could have possibly made that noise?
Activate screen 8-DownTheHall.
<line-indent=15%> tag at the beginning of the text. This will indent each line by 15% of the width of the text area. In this case, you don’t need a closing tag because you want to indent both lines.
Using Overflow Settings
Activate screen 9-Vase.
Notice here that the text covers part of the image. To avoid this, you can specify how to handle long text in the Overflow settings.
Select Text (TMP) and notice how the text continues past the lower boundary of the Rect Transform. That’s because the Overflow setting is set to Overflow, meaning the text continues past the edge of the container.
Also, note that the text does not overflow to the left or right of the container. That’s because Wrapping is set to Enabled. If you disable it, all the text appears on a single line that extends to the left and right of the container.
To get the text off the main part of the image, use the Linked overflow mode. This mode overflows extra text into a second TextMeshProUGUI component.
Right-click Text (TMP) and select duplicate. This makes the text settings and styling options identical on both objects.
Select the new object and name it Text (TMP) Overflow.
Set Top to 1200 and Bottom to 350 on the Rect Transform.
Clear the Text Input field.
Select the original Text (TMP), set Overflow to Linked and drag the new Text (TMP) Overflow into the open field.
The text will now overflow from the top Text (TMP) to the lower Text (TMP) Overflow.
Phew, just a vase! Better grab that broom and tidy up.
Adding Inline Images to Text
There may be a time where you need to add an icon or an image inline with your text. You could use this to give a player information about an item they just picked up or to let them know what a specific button does in your game.
With TextMesh Pro, you can reference specific sprites on a sprite sheet and add them inline with other text using a Rich Text tag.
To see this in action, you’ll add an icon of a flashlight after the word flashlight. Start by setting 13-Stumble as the active screen, then select the icon at Assets/RW/Sprites/FlashlightIcon.png.
In Import Settings, change Texture Type to Sprite (2D and UI).
Right-click on FlashlightIcon.png and select Create ▸ TextMeshPro ▸ Sprite Asset to create a Sprite Asset in the same folder.
Select Text (TMP) and click the Extra Settings header to expand the additional settings. Next, drag the FlashlightIcon Sprite Asset into the Sprite Asset field.
<sprite index=0> tag after the word flashlight in Text Input. The flashlight icon now displays inline.
Adding Color to Inline Sprites
To make this stand out a bit more, you’ll color the word “flashlight” and the flashlight icon yellow using Rich Text tags. Place the
<color=#FFEB04> tag before the word flashlight and the
</color> tag after the flashlight icon tag.
You’ll see that the word flashlight is yellow, but the icon is still white. That’s because the Rich Text color and Vertex Color properties don’t affect sprite assets.
Luckily, the sprite has its own color property, which you can set in the Rich Text tag. Update the sprite tag to
<sprite index=0 color=#FFEB04>. The sprite now displays a yellow color.
Using Additional Rich Text Tags
The next few dialog screens use some handy Rich Text tags.
On screen 14-Knock, use the align tag to align each line left, center, and right. Each new tag overrides the previous one, so you don’t need any closing tags.
On screen 15-WhosThere, use the
<s> tag to strike through and
<u> tag to underline the text.
On screen 17-Investigate, use the
<uppercase> tag to make all the letters uppercase. Also, use the
<smallcaps> tag to make everything uppercase, but the letters that were previously in lowercase will be a bit smaller.
On screen 19-Climb, use the
<voffset> tag to set a vertical offset. The numerical value you set is in font units and the closing tag ends all vertical offset.
These are just a few of the Rich Text tags you can use with TextMesh Pro. Be sure to check out the TextMesh Pro documentation for the full list of over 30 available Rich Text tags.
There’s one question the documentation can’t answer, though:
What. Is in. The attic?
TextMesh Pro Font Assets
Up to this point, you’ve only used the Font Asset bundled with the starter project. Remember, Font Assets are generated from a standard font file and control the font your text uses.
The starter project also includes the Dela Gothic One font, which has a large face to best display your font customizations. In the next section, you’ll learn how to turn this into a Font Asset.
Creating Font Assets
Create a new Font Asset at Window ▸ TextMeshPro ▸ Font Asset Creator. Set the Source Font File to DelaGothicOne-Regular.
To get the best results from your Font Asset, pay attention to these properties:
- Padding: The spacing between each character in the Font Atlas Texture. A value of 5 will give you great results for most text in your project. Here, though, you need large and detailed title text, so set Padding to 7.
- Atlas Resolution: The pixel size of the Font Atlas Texture that you’re generating. For most English character sets, a size of 512×512 is plenty, but you can increase it for higher resolution results. Set the Atlas Resolution to 1024×1024 for this font.
- Character Set: Set to ASCII, unless you’re using another character set.
- Redner Mode: For most applications, the default of SDFAA (Signed Distance Field Anti-Aliasing) will give you the best results.
Once you’ve set these properties, click Generate Font Atlas and you’ll see a preview of the Font Atlas. You can expand the window for a larger preview.
Before saving, look in the output box. The generator displays any errors or characters missing from the font file here.
Also, note the SP/PD ratio. The generator calculates this by dividing the Sampling Point Size by the Padding Value. For best results, this value should be under 10%. If it’s above 10%, either increase the Atlas Resolution or decrease the Padding value, then regenerate the font until the value is below 10%.
Now, it’s time to use your new font asset. Click Save as… and save the Font Asset to Assets/RW/Fonts/Font Assets. Activate screen 21-UpHere, select Text (TMP) and set Font Asset to DelaGothicOne-Regular SDF.
Customizing TextMesh Pro Font Assets
By now, you can see how much power you have to create beautiful fonts by creating Font Assets and using Rich Text tags in TextMesh Pro. However, using TextMesh Pro Material lets you customize your text even more.
Customizing the TextMesh Pro Material gives you granular control over the text face, outline, shadow, bevel, texture and lighting properties. With these effects, you can make your text look more professional — or even go in-depth to turn your text into a piece of art.
Set screen 22-Crawl as the active screen in the sample project.
On this screen are two separate text objects that will demonstrate an important characteristic of TextMesh Pro Materials. Select Text (TMP) Top and Text (TMP) Bottom and set Font Asset to DelaGothicOne-Regular SDF.
Now, select only Text (TMP) Top and scroll down to its Material settings.
Go to the Outline section and use the slider to increase the Thickness. You’ll see that a black outline begins to form around the text. But something else interesting is happening.
Did you notice it? Although you only modified the Material Properties for Text (TMP) Top, the effect also applies to Text (TMP) Bottom.
That’s because both objects reference the Default Material. When you change the Default Material, the changes occur on all other objects that reference it. Go all the way back to screen 21-UpHere and you’ll see that the same outline applies to the text.
Because of that, it’s not a good idea to modify the Default Material. Instead, you’ll create separate Material Presets for each of the text effects you want.
Now, reset the Default Material to its original settings. You’ll learn how to create Material Presets in the next section.
Creating Material Presets
Material Presets store settings for a TextMesh Pro Material. They’re associated with one font, and you can easily swap between any Material Presets you create for that font.
To create a Material Preset, select the TextMesh Pro object you want to apply the Material Preset to. In this case, select Text (TMP) Top.
Click the three vertical dots icon on the Material, then select Create Material Preset to create a new Material Preset in the directory of the current Font Asset. It automatically applies to the selected TextMesh Pro object.
Set the Outline Thickness to 0.25 for a subtle outline that makes the text stand out from the background.
The final important step to take is to name the new Material Preset properly. Not only do descriptive names help you identify Material Presets, but incorrectly named Material Presets won’t appear in the list of available Material Presets for that font.
When you create a Material Preset, it has the same name as the Font Asset it applies to. You must keep the full Font Asset name as the prefix for the Material Preset name. You can append any details to the name after that prefix.
Name this Material Preset DelaGothicOne-Regular SDF — Black Outline because it’s a simple black outline for the Dela Gothic One font.
Now, select Text (TMP) Bottom and create a new Material Preset for it. Set the Face Color to brown and the Outline Color to light gray with a Thickness of 0.25 to make the text look like dusty boxes.
Name the Material Preset DelaGothicOne-Regular SDF — Dusty Boxes.
Now that you’ve created two Material Presets, you can toggle between them and the Default Material using the Material Preset drop-down on the TextMeshProUGUI component. Remember, if the Material Preset’s name doesn’t have the Font Asset as a prefix, it won’t show up in this list.
More Material Preset Settings
By this point, you’re ready to create more Material Preset settings. First, this tutorial will walk you through the creation process, then you’ll have the chance to create some cool effects on your own.
Using Cardboard Presets
Set screen 24-LargeBox as the active screen. Here, you’ll apply a cardboard texture and set it to move across the font face.
Select Text (TMP) and set Font Asset to DelaGothicOne-Regular SDF.
Then set Horizontal and Vertical Mapping to Paragraph. This stretches the texture across all the characters in each paragraph of text.
Create a new Material Preset named Dela Gothic One-Regular SDF – Cardboard and set:
- Face Texture to Assets/RW/Textures/Cardboard.
- Speed X to 0.1.
- Outline Color to Brown.
- Outline Thickness to 0.15.
Now, enter Play Mode and preview the effect.
Using Bevel Presets
Now, try creating your own effect by adding a bevel and some lighting settings. Start by setting 26-Stopped as the active screen.
When you’re done, or if you get stuck, click the Reveal button below to see the solution.
Select Text (TMP) and set Font Asset to DelaGothicOne-Regular SDF. Then create a new Material Preset named Dela Gothic One-Regular SDF – Stopped.
Set the Face color to red.
In the Underlay section, set Offset X to 1, Offset Y to -1, and Dilate to 1.
In the Lighting section, under Bevel, set Amount to 1 and Width to 0.5. Tweak the values under Local Lighting to your artistic liking.
Did you succeed? If not, here’s another chance. :]
Using Glow Presets
Start by setting 28-Skull as the active screen.
Now, on your own, recreate the glowing outline effect.
When you’re done, or when you get stuck, click the Reveal button below to see the solution.
Select Text (TMP) and set Font Asset to DelaGothicOne-Regular SDF. Then, create a new Material Preset named Dela Gothic One-Regular SDF – Golden.
In the Face section, set the Color to a nice gold and Dilate to -0.75.
Check the Glow section and set the Color to a similar gold, the Offset to 1, and Outer to 0.25.
Also note the Face Dilate setting can be turned down to shrink the text. This gives the text an appearance of materializing before your eyes, while making the glowing, offset outline more prominent.
You can use the image below as a reference for the colors of the Face and the Glow.
Now, finish the story by adding the Font Assets and Material Preset to the remaining screens. Then click Play and enjoy your very own text-based adventure game!
Where to Go From Here?
For reference, you can download the completed project files by clicking the Download Materials button at the top or bottom of this tutorial.
If you want to learn all the Rich Text tags in TextMesh Pro, look at the official documentation.
Another great way to learn more about TextMesh Pro is through the TextMesh Pro Examples & Extras, which contains several sample scenes with more text enhancements you can create with TextMesh Pro. Import them into your project from Window ▸ TextMeshPro ▸ Import TMP Examples and Extras.
We hope you enjoyed this tutorial. If you have any questions or comments, please join the forum discussion below!