Skip to content
This repository was archived by the owner on Jan 4, 2022. It is now read-only.
Porrith Suong edited this page Jan 13, 2021 · 3 revisions

Important!

Features that are missing include, right to left support in languages, italics, masking, and various options for overflowing text and cutting off overflowed text.

Font Face

Font Face is information which describes the font. This includes information about the dimension of a character, the bearings, and origin (where the character begins).

TextMeshPro

Font Face information is generated via TextMeshPro and converted to its entity format via the FontFaceInfo component. The reason TextMeshPro is used, is because it already generates the materials and bitmap textures used to display fonts.

Regular Unity Text components resample the bitmap texture, which makes it difficult to use just to regenerate the mesh across different threads.

How is text structured?

Text is mainly structured through a DynamicBuffer of CharElements. This is effectively a dynamic character array and allows you to resize the number of characters you would need.

Text entities also contain an entity reference to the LinkedTextFontEntity and a TextOptions component, which describe how the Text looks like.

LinkedTextFontEntity

  • Stores a DynamicBuffer of GlyphElement. Glyphs store information such as
    • Unicode
    • Advance
    • Bearings
    • Size
    • Scale
    • UV

TextOptions

  • Stores information about the style, the point size, and the anchor.

How is text built?

First, we count the number of lines that the text will take given the dimensions of the space. We iterate through each character and map them to the Unicode of a GlyphElement. Based on the alignment of the text, we calculate the starting position and generate the mesh for that specific character (filling in the positions, normals, color, UV mappings).

When we build the next character, we use the advance and the width of the glyph to figure out the next starting position of the character mesh. When we reach the end of the dimension, we figure out the next starting position based on the line height.

It is important to note that all characters are built into a single mesh, if text share the same material and font atlas.

For the details of the implementation of how Text is built, take a look at this OpenGL tutorial on text implementation.

When is text built?

Text is built on conversion via the HierarchyConversionSystem, when you resize the screen resolution, or when you update the text. Please see the page about Rebuilding UI.

When is text updated?

Text is marked to be rebuilt when the value changes. This is only true for dynamic text. This is done through the SignalRebuildOnTextChangeSystem. The SignalRebuildOnTextChangeSystem internally stores a hashmap of dynamic text entities and a hash of their DynamicBuffer<Char>.

If the hash of the text changes from the hash of the previous known text, the SignalRebuildOnTextChangeSystem marks the Canvas entity to be updated via a OnDynamicTextChangeTag.

When the Canvas entity is marked with an OnDynamicTextChangeTag component. This tells the BuildDynamicTextSystem to resize the Vertex and Index buffers. Static content is not touched manipulated and remains untouched during this process.

Clone this wiki locally