Get started with text
Learn how to style text, create fonts, and style with rich text tags and style sheets by examples.
阅读时间5 分钟最后更新于 21 天前
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.
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 folder and sub-folders to store font assets and custom style sheets.
Resources- Follow the steps in Get started with Runtime UI to create a simple runtime UI.
- In the folder, create a
Assetsfolder.Resources - Right-click in the folder, and select Create > UI Toolkit > Panel Text Settings to create a
Assets\UI Toolkit.Panel Text Settings.asset - In the folder, create two folders named
ResourcesandFonts & Materials.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 . For more information, refer to Style text with USS.
12px- Double-click to open it in UI Builder.
SimpleRuntimeUi.uxml - In the window for the Toggle control, select Text.Inspector
?
- In the Font Style field, select B and I.
- In the Size field, enter 12.
Create a dynamic font asset
Create a dynamic font asset and apply it to the TextField control. 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.
-
Download the Roboto font package from Google Fonts.
-
Unzip the font package file and placein your project's
Roboto-Regular.ttffolder.Assets\Resources\Fonts & Materials -
In the Inspector window for the font file, confirm the following:
- Include Font Data is enabled
- Character is set to Dynamic
-
Right-clickand select Create > UI Toolkit > Text > Font Asset > SDF. This creates a dynamic font asset called
Roboto-Regular.ttf.Roboto-Regular SDF.asset -
Double-clickto open it in UI Builder.
SimpleRuntimeUi.uxml -
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.
- Double-click to open it in UI Builder.
SimpleRuntimeUi.uxml - In the Inspector panel for the Label control, change Text to . This makes the word
This is a <font-weight=700><size=2em><color=#FF0000>*Label*</color></size></font-weight>big, red, bold, and with an asterisk on either side of it.Label - Make sure Enable Rich Text is selected.
The label text looks like the following in the Viewport window:

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 to , create a custom style sheet, and apply the style to both words.
LabelButton-
Right-click in thefolder and select Create > UI Toolkit > Text > Text StyleSheet. This creates a
Assets\Resources\Text Style Sheetsin the path you set in the UITK Text Settings asset.Text StyleSheet.asset -
In the Inspector window for, do the following:
Text StyleSheet.asset- In Name, enter .
ExampleStyle - In Opening Tags, enter .
<font-weight=700><size=2em><color=#FF0000>* - In Closing Tags, enter .
*</color></size></font-weight>
- In Name, enter
-
In the Inspector window for, from the Default Style Sheet list, select Text StyleSheet.
UITK Text Settings.asset -
Double-clickto open it in UI Builder.
SimpleRuntimeUi.uxml -
In the Label control's Inspector window, change Text to.
<s>This is a <style="ExampleStyle">Label</style> -
Make sure Enable Rich Text is selected.
-
In the Button control's Inspector window, change Text to.
<s>This is a <style="ExampleStyle">Button</style> -
Make sure Enable Rich Text is selected.
-
Enter Play mode and check the text in the runtime UI.
-
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.
-
In your project'sfolder, select
Assets\Resources\Fonts & Materials.Roboto-Regular SDF.asset -
In the Inspector window for, 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.
Roboto-Regular SDF.asset

Newly entered characters appear in the font atlas window.