Font assets act as containers for fonts. With font assets, you can import fonts into your projects and create variations of a font from one file and not change the original font. Another reason to create font asset is to deal with languages with massive character sets, such as Chinese. You can include only the characters you need, rather than the full set of characters.
You can convert TrueType (.ttf
), OpenType (.otf
), or TrueType Collection(.ttc
) fonts to font assets. A font asset includes a font atlas texture file that contains all of the characters. There are several atlas populate modes and render modes to choose when you create a font asset.
TextCore supports the Static, Dynamic, and Dynamic OS atlas population modes.
Static font assets pre-bake characters into the atlas texture during conversion. Use static font assets to contain known text in your project, such as labels and section titles. When you create a static font asset, the font atlas is empty by default. You must manually generate the font atlas and include all the characters required for the project. The project build doesn’t require you to include the font source file, so it’s fast and efficient.
See instructions on how to create a Static font asset.
Dynamic font assets start with an empty atlas texture. Dynamic font assets look for characters in the source font file and adds the characters dynamically as you use them in a UI. Use Dynamic font assets for unknown text in your project, such as any text input fields. Dynamic fonts assets are flexible, but they have additional performance overhead because the project build requires to include all source font files.
See instructions on how to create a Dynamic font asset.
Dynamic OS font assets are a dynamic font asset that references the font file on the operating system instead of the source font file. Compared to the Dynamic font asset, dynamic OS font assets have less memory overhead. The project build doesn’t require to include the font source files. Make sure that the fonts are on the operating system of the targeted platforms. Dynamic OS font assets are great candidates for fallback font assets.
To create a dynamic OS font asset, import the font from the operating system to your project, and use it to create a dynamic font asset and change the Atlas Population Mode to Dynamic OS.
TextCore supports atlas render modes for bitmap and Signed Distance Field (SDF).
Bitmap rendering generates font assets that perfectly aligns each pixel of the font with scene and GUI pixels. Use the following render modes for pixel art:
SDF rendering generates font assets that look crisp when you transform or magnify them. SDF rendering supports effects such as outlines and drop shadows.
Unlike bitmap font textures, SDF font assets contain contour distance information. In font atlases, this information looks like grayscale gradients that run from the middle of each glyph to a point past its edge. The gradient’s mid-point corresponds to the edge of the glyph.
The images below show bitmap and SDF font assets and the rendered text they produce. The bitmap fonts produce text whose edges are more or less jagged and blurry, depending on how far the text is from the camera, and how it’s transformed and distorted. The SDF font produces text with smooth edges regardless of the distance from the camera.
Use the following render mode for SDF fonts:
If you want to make changes only to a font asset’s metrics, instead of creating a new font asset, create a font asset variant based on another font asset. A font asset variant can have styling that distinguishes it from the original font asset , without consuming extra space for textures. The variant stores its own Face Info, Character, Glyph, and Adjustment Pair data, but still refers to the original atlas.
To create a font asset variant, right-click the font asset and select Create > Text> Font Asset Variant.
Characters in the font texture need padding between them so they can be rendered separately. This padding is specified in pixels. Padding also creates room for the SDF gradient. The larger the padding, the smoother the transition, which allows for higher-quality rendering and larger effects, like thick outlines. Use a padding of 5
for a 512x512
texture. A good rule of thumb is to have a sampling to padding ration of 1:10
.
For most fonts, use a 512x512
texture resolution if you include all ASCII characters. To support thousands of characters, use large textures and enable Multi Atlas Textures in the font asset’s Inspector window. This automatically creates new textures of the same size once the main texture is full.
You can also enable Multi Atlas Textures if you design for mobile devices, where an imposed maximum texture size prevents an entire set of glyphs from fitting in a single atlas of sufficient quality.
To use a font asset, in UI Builder, select the font from Font Asset in the Inspector window.
Note: The Font field is for backward compatibility. If you want to use a font from the Font list for your legacy projects, you must select None from Font Asset. Otherwise, the font you selected from Font won’t take effect.
You can apply a font asset to a text element in USS with the following syntax:
-unity-font-definition: <resource> | <url>
例
Label {
-unity-font-definition: url("/Assets/UI Toolkit/Resources/Fonts & Materials/LiberationSans SDF.asset");
}
For more information, see USS text properties.
In summary, consider the following when you create a font asset:
1:10
.