In this tutorial we will look at the TextMesh Pro GUI object. TextMesh Pro is used alongside normal GameObjects and placed in a scene in 3D space. It's ideal for XR applications where text is often part of the existing scene and not referenced in 2D menus.
TextMesh Pro is the ultimate text solution for Unity. It's the perfect replacement for Unity's UI text and the legacy text mesh. Powerful and user-friendly, it utilizes advanced text rendering techniques and a range of custom shaders. It offers significant improvements in visual quality and provides users with incredible flexibility in styling and texturing. TextMesh Pro offers enhanced control over text formatting and layout with features such as character, word, line, and paragraph spacing, kerning, justified text, links, over 30 available rich text tags, support for multiple fonts and sprites, custom styles, and more. It performs well because the geometry generated by TextMesh Pro uses two triangles per character, just like Unity's text components, so there's no additional performance cost for this improved visual quality and flexibility. In this tutorial, we'll explore the TextMesh Pro GUI object. TextMesh Pro is used alongside regular GameObjects and placed in a scene within 3D space. It is ideal for XR applications where text is often part of the existing scene and is not referenced in 2D menus.
Introduction to TextMesh Pro
TextMeshTextMesh Pro is an easy-to-use system for high-quality text. It has many options for displaying and formatting text, and is an easy way to add a professional touch to a project's user interface. The article shows how you can prepare fonts for use in TextMesh Pro, create new TextMesh Pro objects and modify these objects.
1. Create a TextMesh Pro text object:
1. Select GameObject from the drop-down list or with the right mouse button in the hierarchy window UI> TextMesh Pro - Text.
2. If you are using TextMesh Pro (TMP) for the first time in a project, Unity offers the import of the TMP Essentials and Examples & Extras packages (if you have not yet imported the TextMesh Pro Asset package). Click Import TMP Essentials and close the window.
TMP can also be imported as an included asset package when the project is created.
3. If the text is not visible on the "Scene" tab, highlight it in the hierarchy and press the "F" key on the keyboard while the mouse pointer is in the scene view.
4. Click the lock in the upper right corner of the Inspector to bring the focus to the TextMesh Pro Text object.
5. Set the size of the text area to 300 x 100 in the settings for the rectangle transformation.
The rectangle transform controls the position and size of the text area.
6. Enable text wrapping and overflow in the Font Settings section of the Text Mesh Pro UGUI inspector.
TextMesh Pro offers a variety of layout and display options.
7. Try changing the material preset and various font settings. You can set the color of the font either by changing the vertex color or in the material settings. Click the triangle in the Material at the bottom of the Inspector to see the Font, Outline, and Underlay (drop shadow) settings.
Click the gray bars outside the two check boxes to show or hide the properties for that particular font setting.
8. Select the check box in the right half of the “Outline” section and click anywhere else in the section to open the settings.
9. Adjust the color and thickness of the text.
Adding an outline to your text can make it easier to view in busy environments.
10. Try changing the Underlay and Foreground settings. Click the gear in the upper right corner and select Reset to return to the default settings.
In this menu you can return to the original material settings, transfer material properties and create material presets.
11. Experiment with the font material presets and adjust the font settings as desired.
TextMesh Pro comes with three material presets that you can use to quickly examine the effects of different settings.
2. Access to TextMeshPro objects in code:
Here we use the System namespace in our script to access date and time functions. TextMesh Pro only requires the TMPro namespace.
1. In the “GameObject” drop-down list, select the “Create empty” option and name it “ClockDisplay”.
2. Create a new script called "Clock" and attach it to ClockDisplay. Double-click Clock in the project window to open Visual Studio or MonoDevelop. Delete the Start () method as we won't be using it for this exercise.
3. Enter the following from line 4:
using TMPro;
using System;
4. Enter the following on line 8:
public TMP_Text clockText;
5. Enter the following in Update on line 12:
clockText.text = DateTime.Now.ToString();
6. The clock script is now complete.
Save the changes and return to Unity.

The finished watch script:
7. Drag the TextMesh Pro object you created in Exercise 1 into the area labeled Clock Text in the Clock Inspector.
8. Press Play to start the demonstration. The current date and time are displayed with the material settings specified in the TextMesh Pro Text Inspector.
9. When you're done, exit playback mode.
3. Create a font for use with TextMesh Pro
1. If you don't already have a resource folder in Assets, create one and then create a folder named "Fonts" within that folder. Save your created font asset here.
2. Drag a font file (OpenType or TrueType) into the Resources folder.
3. From the Window drop-down list, select TextMesh Pro> Font Asset Creator.
4. Click the circle next to the font source slot and select your font.
You can also drag a font directly from the project view into the slot. Font assets can be reused across projects.
5. Click on Generate Font Atlas.
6. Click on Save.
7. Navigate to Assets> Resources> Fonts, name your font and click Save.
8. If it still doesn't appear in the Inspector, click the TextMesh Pro Text object in the Hierarchy window to highlight it.
9. Click on the circle next to Font Asset and select the font asset you just created.
10. Activate playback mode to see your new font asset in action.
11. Exit playback mode.
TextMesh Pro is very versatile. It can be used for any situation where text is needed, including games, graphics applications, videos, and more.
TextMesh Pro: Basics
In this tutorial we will look at the TextMesh Pro and TextMesh Pro GUI objects. TextMesh Pro is used alongside normal GameObjects and placed in a scene in 3D space. It's ideal for XR applications where text is often part of the existing scene and not referenced in 2D menus. The TextMesh Pro UGUI component works with the canvas system and is intended to replace and expand the UI text component with improved functions.
The dark times
All text components in Unity use textures to store glyphs - the visual representation of characters. The older text components (TextMesh and UI-Text) use rasterized bitmaps to render these glyphs. TextMesh Pro primarily uses Signed Distance Fields (SDF) to approximate the shape of characters before rendering them into a texture. Imagine we're trying to render a shape in a 10x10 pixel grid without getting too technical. In a bitmap texture, each grid position would possibly contain a white, gray, or black pixel, depending on the color information at that particular position. In an SDF texture, each grid position would contain a distance to the next feature of the shape, which would result in a better rendering of the original shape within a fixed pixel grid.
If a bitmap shader draws this shape 1: 1, it is simply a texture search for the specified pixel position. If it contains a pixel, it will be drawn; Otherwise it is not. Textures contain a finite number of pixels. When zooming in or out, the shader must adjust the size of each pixel in relation to the display resolution of the device. The resulting shape becomes either blocky, blurry, or too sharp. An SDF shader does the same texture search, but this time it returns a distance example. Depending on the distance, the shader draws either a completely opaque or an alpha-mixed pixel. When zooming in or out, the shader interpolates between distance examples so that shapes (text) can be rendered cleanly in Unity regardless of screen resolution, zoom and scale. To render bitmap text cleanly, the glyphs must be drawn in a 1: 1 ratio, with each glyph having to be rescanned for each font size. However, SDF textures only need to contain a single representation of each glyph, which results in a much more efficient use of texture space. The following pictures show the biggest visual differences between the old and the new system. 
The nice thing about these examples is that despite the superior rendering quality of SDF, both SDF and rasterized text use the same number of polygons (quads) to render the text. This means that TextMesh Pro's performance will not be affected. We'll add both a TextMesh Pro and a TextMesh Pro GUI object to our scene and look at each object in the inspector.
Adding TextMesh Pro to your GameObjects


Both methods create a new GameObject with four new components:
Rec Transform: A container for your text object similar to those used in Microsoft Office or Photoshop. You can change the size, position and rotation of your container.
Mesh renderer: Renders and positions your text in 3D space.
TextMeshPro - Text: The Main component in which you can write text, change the font element, the material preset, the font size, change paragraph styles, activate colors / gradients and select a number of other elements.
Formatting and style options: Try to play around with it.
Material presetting: The material used by the font asset referenced in the TextMeshPro component. Materials define the appearance of your GameObjects. You can also define the appearance of your text here and find options that range from outlines to glow, bevels, textures and more.
Additional settings: These settings allow you, among other options, to adjust the margins of your text, assign sprite assets, and customize which layer the text appears in.
Before entering text, make sure that wrapping and overflow are enabled in the TextMesh Pro component. These options limit the text to the rect transform object.
The automatic size dynamically adapts the sample size of your font to the rectangle transformation. Be careful when using Auto Size. Turning it on will give you an optimal sample size (e.g. for mobile applications where it can be difficult to predict the target resolution for each device). However, it should be deactivated once you've decided on a good size to avoid unnecessary performance.
Adding TextMesh Pro to your user interface

Adding TextMeshPro to a user interface is a very similar process: navigate in the hierarchy to UI> Text - TextMeshPro. With the exception of the canvas renderer, it is identical to the previous version. This will create a new Canvas GameObject with a TextMesh Pro GameObject. Parenting means that the text is moved along the canvas. The components in UI TextMesh Pro are rendered using this canvas object instead of a Unity Mesh renderer.
Originally posted on 2020-03-30 12:45:25.
