TextRenderer
A class in Root.Township, the global namespace.
Draws 3D text in the world (signs, labels, dock counts) by building a mesh per letter from a TextFont. Client side visuals only: on a headless server setting Text stores the string but builds nothing.
- Listed in Global namespace: T to X
- Inherits or implements
MonoBehaviour,IBakeable,IBuildRendererBakeable,IBuildBakeable - Nested types
AndroidTextRendererQueue,TextRendererDistanceChecker,TextVerticalAlignment
Constructors
| Member | What it is |
|---|---|
TextRenderer() | Unity makes components itself; use AddComponent or spawn a prefab instead of new. |
Fields
43 private and internal fields
| Member | What it is |
|---|---|
private Color activeColor | Colour applied to letters as they're built; colour tags change it mid-line. |
private string actualText | The text currently built, so a refresh with the same text is skipped. |
private TextAlignment alignment | Backing field for Alignment. |
private Material androidMaterial | Material used on Android. |
private Color androidTintColor | Tint used on Android. |
private bool bestFitToWidth | Single-line only: scales the text to fit maxWidth, within minScale and maxScale. |
private Transform cachedTransform | Cached transform, used by the distance checker. |
private static readonly Dictionary<int, Material> coloredMaterials | Shared cache of tinted material copies, keyed by material and colour. |
private static readonly Dictionary<int, Mesh> coloredMeshes | Shared cache of vertex-coloured mesh copies, keyed by mesh and colour. |
private Vector2 currentCenter | Backing field for CurrentCenter. |
private TextFont font | Backing field for Font. |
private List<Mesh> generated | Meshes made by this renderer, cleaned on destroy. |
private bool hasLineLimit | Multi-line only: whether maxLines applies. |
private static readonly Regex htmlTagRegex | Regex that finds <c:colour> and closing tags in the text. |
private static readonly char[] ignoredChars | Characters that don't log a warning when the font lacks them. |
private bool isDelayingInEditor | Editor: makes the first refresh call wait, so edits are batched. |
private bool isMerging | Builds each line as one merged mesh instead of an object per letter. Always on for baked text. |
private bool isMono | True while inside a \u0002 monospace section. |
private bool isMultiLine | Wraps the text onto several lines at lineWidth. |
private bool isRefreshing | Inspector button that calls DebugRefresh. |
private bool isStatic | The text never moves, so the distance checker caches its position. |
private bool isStaticText | Bakes the text during prefab bake and deletes the component. |
private float lineSpacing | Vertical gap between lines. |
private float lineWidth | Wrap width for multi-line text, before scale. |
private static readonly Logger logger | NLog logger for this class. |
private Material material | Material used on non-Android platforms. |
private int maxLines | Most lines shown when hasLineLimit is on. |
private int maxLODLevel | How many LOD levels of staticBatchedLODGroup the baked text joins. |
private float maxRenderDistanceSquared | Squared distance from the local player beyond which the text hides; 0 means always shown. |
private float maxScale | Largest scale best fit may use. |
private float maxWidth | Width best fit scales the text to. |
private static readonly List<Color> meshColoring | Scratch list of vertex colours used by GetMeshForColor. |
private float minScale | Smallest scale best fit may use. |
private readonly HashSet<Renderer> renderers | Renderers created for the current text. |
private float scale | Backing field for Scale. |
private ShadowCastingMode shadowCastingMode | Shadow mode for the letter renderers. |
private float spacing | Extra space added to each character's advance. |
private LODGroup staticBatchedLODGroup | LOD group the baked text renderers are added to during static batching. |
private bool supportsHTMLTags | Backing field for SupportsHtmlTags; enables <c:colour> tags, which need a vertex colour material. |
private string text | Backing field for Text. |
private Color tintColor | Tint used on non-Android platforms. |
private bool useParentLayer | Puts the generated objects on this object's layer. |
private TextRenderer.TextVerticalAlignment verticalAlignment | Multi-line only: top, centre or bottom alignment of the block. |
Properties
| Member | What it is |
|---|---|
TextAlignment Alignment { get; set; } | Horizontal alignment (left, centre, right). Takes effect on the next refresh. |
Color Color { get; set; } | Tint colour (a separate value on Android). Setting a new colour forces a rebuild. |
Vector2 CurrentCenter { get; } | Centre of the rendered text in local space, worked out on the last rebuild. |
float CurrentHeight { get; private set; } | Height of the rendered text after scaling, from the last rebuild. |
float CurrentLineWidth { get; private set; } | Width of the rendered line before scaling, from the last rebuild. |
float CurrentLineWidthScaled { get; } | CurrentLineWidth times Scale. |
Material EditorMaterial { set; } | Write only. Sets both the normal and Android material. |
Color EditorTintColor { set; } | Write only. Sets both the normal and Android tint colour. |
TextFont Font { get; set; } | The TextFont used to build letters. |
bool IsDuringEditorBake { get; set; } | Set while the text is being baked into static meshes at build time. Clearing is skipped during it. |
bool IsQueued { get; private set; } | Android only: true while the text waits in the shared queue that spreads rebuilds over frames. |
bool IsVisible { get; private set; } | Whether the text is shown. With a max render distance set, it turns off when the local player is far away. |
Material Material { get; } | Material for the letters, with a separate one on Android. |
int MaxLines { get; set; } | Line limit for multi-line text. Unlimited unless the line limit option is on. |
float MaxScale { get; set; } | Largest scale best fit to width may use. |
float MaxWidth { get; set; } | Width best fit to width scales the text into. |
float MinScale { get; set; } | Smallest scale best fit to width may use. |
IEnumerable<Renderer> Renderers { get; } | The renderers of the generated letters. |
float Scale { get; set; } | Text scale. Takes effect on the next refresh. |
bool SupportsHtmlTags { get; set; } | When on, c colour tags in the text are applied. Needs a vertex colour material. |
string Text { get; set; } | The text to show. Setting it unescapes sequences such as \n and refreshes. |
2 private and internal properties
Events
| Member | What it is |
|---|---|
event Action OnFinishedRendering | Android only: raised when a queued rebuild finishes. |
event Action OnUpdatedText | Raised after each rebuild of the text mesh. |
Methods
25 private and internal methods
| Member | What it is |
|---|---|
private void AlignTextLine(float lineWidth, int lineNumber, int lineCount, Transform lineParent) | Scales and positions a finished line for the alignment, line number and vertical alignment; with bestFitToWidth it first sets scale to fit maxWidth. |
private void AlignTextLine(float lineWidth, int lineNumber, int lineCount, List<Vector3> vertices, int startIndex) | Scales and positions a finished line for the alignment, line number and vertical alignment; with bestFitToWidth it first sets scale to fit maxWidth. |
private void IBakeable.Bake() | Prefab bake: builds the text as merged static meshes, adds them to the static LOD group, then removes this component. |
private void IBuildRendererBakeable.Bake(bool isSaving) | Prefab bake: builds the text as merged static meshes, adds them to the static LOD group, then removes this component. |
private void IBuildRendererBakeable.Clean() | Empty build-bake step. |
private Vector3 ConvertLinearScale() | The line scale to use: uniform, but with z left at 1 on Android. |
private float CreateLetterMesh(string text, float currentLineWidth, int i, GameObject letter, Mesh mesh, Material material, Transform letterTransform, float characterWidth) | Adds one letter at the current line offset, either as its own renderer object or into the merged vertex lists, and returns the new line width. |
private float CreateLetterMesh(string text, float currentLineWidth, int i, LetterMesh letterMesh, List<Vector3> vertices, List<Vector3> normals, List<int> indices, float characterWidth) | Adds one letter at the current line offset, either as its own renderer object or into the merged vertex lists, and returns the new line width. |
private void CreateMultiline(Material instancedMaterial) | Wraps the text into lines no wider than lineWidth (breaking on spaces and new lines), draws up to MaxLines of them and works out height and centre. |
private float CreateTextLine(string text, int lineNumber, int lineCount, List<Vector3> vertices, List<Vector3> normals, List<int> indices, List<Color> colors) | Adds one line's letters to the merged vertex, normal, index and colour lists, applying colour tags when supportsHTMLTags is on. |
private void DebugRefresh() | Inspector button: forces a refresh. |
private void DrawGizmoLine(float maxWidth) | Draws the width guide line for the current alignment. |
private float DrawTextLine(string text, int lineNumber, int lineCount, Material material, out float height, out float maxHeight) | Builds one line of text under a new Line N child, as one object per letter or one merged mesh, and returns its width. |
private int GetColorObjectHash(Object targetObject, Color color) | Hash of an object and a colour, used as the key for the coloured material and mesh caches. |
private Material GetMaterialForColor(Material material, Color color) | Returns a cached copy of the material tinted to the colour, making it on first use. |
private Mesh GetMeshForColor(Mesh mesh, Color color) | Returns a cached copy of the mesh with all vertex colours set to the colour. |
private float GetWidth(char character) | Advance width of a character in the font plus spacing. Character \u0002 toggles monospace mode, in which every character takes a space's width. |
private void InternalClear() | Destroys the generated letter objects and forgets the rendered text. |
private void InternalRefresh() | Rebuilds the text meshes now, sets width, height and centre, and raises OnUpdatedText (and OnFinishedRendering after an Android queue wait). |
private void OnDestroy() | Cleans up generated meshes. |
private void OnDisable() | Clears the text objects and stops distance checks. |
private void OnDrawGizmosSelected() | Draws the best-fit or multi-line width guide. |
private void OnEnable() | Registers for distance checks when it has a render distance; otherwise refreshes right away. |
private void OnValidate() | Keeps maxWidth non-negative and turns off bestFitToWidth when multi-line. |
private void Reset() | Editor: fills in the default Vinque font and metal material. |
Used in
Members of other types that take, return or hold a TextRenderer:
AchievementBoard.title, AchievementItem.textRenderer, CaveTeleporter.costRenderer, CosmeticRotatingSlot.indexText, MainMenuShopInterface.accountBalanceText, MainMenuShopItem.costText, MainMenuShopItem.itemName, ShopInterface.balanceText, ShopInterface.totalCost, ShopInterfaceItem.costText, ShopInterfaceItem.nameText, CommunalStorage.localAllowance, DockVisual.countLabel3D, PooledDockVisual.countLabel3D, PooledDockVisual.CountLabel3D, InfoBoard.renderer, AssistedSpawnCommandAction.progressText, CustomScroller.textSides, CustomScroller.valueText, EnumMenuSettingBase.valueText, MenuGameSetting<T>.displayNameText, ScrollerEnumMenuSetting.textSides, ScrollerSelector.textSides, ServerCommandAction.buttonText, ServerMultiCommandAction.buttonText, StandardFloatAsEnum.textSides, StandardFloatAsEnum.valueText, UserToken.username, ConfirmPopupPanel.messageText, InputField.textVisuals, ListElementWithButtonAndExtraText.extraText, ListElementWithButtonAndExtraText.ExtraText, OptionsPanel.text, RecentPlayerPanelListElement.thirdLine, CreateServerPanel.canCreateText, FriendsInvitesPanel.emptyListText, FriendsListPanel.emptyListText, FriendsRequestsPanel.emptyListText, GroupInfoTab.infoText, GroupMembersPage.emptyListText, GroupMembersPage.tooManyMembersText, InboxPanel.infoText, IncomingGroupRequestPage.emptyListText, OculusFriendsListPanel.emptyListText, OutgoingGroupInvitesPage.emptyListText, PlayerReportInboxInfoPanel.actionTakenAtText, PlayerReportInboxInfoPanel.actionTakenByText, PlayerReportInboxInfoPanel.actionTakenText, PlayerReportInboxInfoPanel.reasonText, PlayerReportInboxInfoPanel.reportedUserText, PlayerReportInboxInfoPanel.reporterText, PlayerReportInboxInfoPanel.serverText, PlayerReportInboxInfoPanel.timeText, PlayerReportSummaryPanel.actionTakenAtText, PlayerReportSummaryPanel.actionTakenByText, PlayerReportSummaryPanel.actionTakenText, PlayerReportSummaryPanel.reasonText, PlayerReportSummaryPanel.reportedUserText, PlayerReportSummaryPanel.reporterText, PlayerReportSummaryPanel.serverText and 88 more.