Version: Unity 6.6 Alpha (6000.6)
Language : English
Work with text
Style text with USS

Get started with text

UI Toolkit uses Advanced Text Generator to render text, a text rendering module that uses Harfbuzz, ICU, and FreeType to deliver comprehensive Unicode support and text shaping capabilities.

Advanced Text Generator has the following limitations:

  • Doesn’t support static font assets. If your project uses static font assets, you must migrate them.
  • Customization of glyph metrics isn’t available. The recommended best practice is to use the font editing tools to adjust the metrics or trim the font as needed.

This example demonstrates how to style text in UI Builder, create and apply dynamic font assets, use rich text tags and custom style sheets to style text, and create a UITK Text Settings asset to manage the text settings for a panel.

Note: For demonstration purposes, this guide uses a runtime UI. The instructions to create font assets and a style sheet also apply to the Editor UI. However, you can’t change the default UITK Text Settings for Editor UI in the current release.

Prerequisites

This guide is for developers familiar with the Unity Editor, UI Toolkit, and C# scripting. Before you start, get familiar with the following:

Create a UITK Text Settings asset

Start with a runtime UI and create a UITK Text Settings asset that manages text settings for the panel. You also create a Resources folder and sub-folders to store font assets and custom style sheets.

  1. Follow the steps in Get started with Runtime UI to create a simple runtime UI.
  2. In the Assets folder, create a Resources folder.
  3. Right-click in the Assets\UI Toolkit folder, and select Create > UI Toolkit > Text > Panel Text Settings to create a Panel Text Settings.asset.
  4. In the Resources folder, create two folders named Fonts & Materials and Text Style Sheets.

Style text with UI Builder

Use UI Builder to style the Toggle text to be bold and italic, and have a font size of 12px. For more information, refer to Style text with USS.

  1. Double-click SimpleRuntimeUi.uxml to open it in UI Builder.
  2. In the InspectorA Unity window that displays information about the currently selected GameObject, asset or project settings, allowing you to inspect and edit the values. More info
    See in Glossary
    window for the Toggle control, select Text.
  3. In the Font Style field, select B and I.
  4. In the Size field, enter 12.

Create a dynamic font asset

Create a dynamic font asset and apply it to the TextField controlA TextField control displays a non-interactive piece of text to the user, such as a caption, label for other GUI controls, or instruction. More info
See in Glossary
. For a dynamic font asset, you must include the source font file in the build. Import the source font file directly to the path set for the font.

  1. Download the Roboto font package from Google Fonts.

  2. Unzip the font package file and place Roboto-Regular.ttf in your project’s Assets\Resources\Fonts & Materials folder.

  3. In the Inspector window for the font file, confirm the following:

    • Include Font Data is enabled
    • Character is set to Dynamic
  4. Right-click Roboto-Regular.ttf and select Create > UI Toolkit > Text > Font Asset > SDF. This creates a dynamic font asset called Roboto-Regular SDF.asset.

  5. Double-click SimpleRuntimeUi.uxml to open it in UI Builder.

  6. In the TextField control’s Inspector window, select Text > Font Asset > Roboto-Regular SDF.

Style with rich text tags

Use rich text tags to style text in the Label control.

Note: In the current release, rich text tags aren’t supported for TextField.

  1. Double-click SimpleRuntimeUi.uxml to open it in UI Builder.
  2. In the Inspector panel for the Label control, change Text to This is a <font-weight=700><size=2em><color=#FF0000>*Label*</color></size></font-weight>. This makes the word Label big, red, bold, and with an asterisk on either side of it.
  3. Make sure Enable Rich Text is selected.

The label text looks like the following in the ViewportThe user’s visible area of an app on their screen.
See in Glossary
window:

Label text preview shows the word Label as big, red, and with an asterisk to either side.
Label text preview shows the word “Label” as big, red, and with an asterisk to either side.

Style with style sheets

To apply the same style for Label to Button, create a custom style sheet, and apply the style to both words.

  1. Right-click in the Assets\Resources\Text Style Sheets folder and select Create > UI Toolkit > Text > Text StyleSheet. This creates a Text StyleSheet.asset in the path you set in the UITK Text Settings asset.

  2. In the Inspector window for Text StyleSheet.asset, do the following:

    • In Name, enter ExampleStyle.
    • In Opening Tags, enter <font-weight=700><size=2em><color=#FF0000>*.
    • In Closing Tags, enter *</color></size></font-weight>.
  3. In the Inspector window for UITK Text Settings.asset, from the Default Style Sheet list, select Text StyleSheet.

  4. Double-click SimpleRuntimeUi.uxml to open it in UI Builder.

  5. In the Label control’s Inspector window, change Text to <s>This is a <style="ExampleStyle">Label</style>.

  6. Make sure Enable Rich Text is selected.

  7. In the Button control’s Inspector window, change Text to <s>This is a <style="ExampleStyle">Button</style>.

  8. Make sure Enable Rich Text is selected.

  9. Enter Play mode and check the text in the runtime UI.

  10. In the text field, enter some random characters. For a dynamic font asset, the font atlas is dynamically generated as you enter text in the text field.

  11. In your project’s Assets\Resources\Fonts & Materials folder, select Roboto-Regular SDF.asset.

  12. In the Inspector window for Roboto-Regular SDF.asset, select Update Atlas Texture to open the Font Asset Creator window. If the atlas window is hidden, expand it. You can view the characters you entered in the atlas window.

Newly entered characters appear in the font atlas window.
Newly entered characters appear in the font atlas window.

Additional resources

Work with text
Style text with USS