Version: 5.4 beta (switch to 5.3)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

TextGenerator

class in UnityEngine

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

Class that can be used to generate text for rendering.

Caches vertices, character info, and line info for memory friendlyness.

#pragma strict
public var font: Font;
function Start() {
	var settings: TextGenerationSettings = new TextGenerationSettings();
	settings.textAnchor = TextAnchor.MiddleCenter;
	settings.color = Color.red;
	settings.generationExtents = new Vector2(500.0F, 200.0F);
	settings.pivot = Vector2.zero;
	settings.richText = true;
	settings.font = font;
	settings.fontSize = 32;
	settings.fontStyle = FontStyle.Normal;
	settings.verticalOverflow = VerticalWrapMode.Overflow;
	var generator: TextGenerator = new TextGenerator();
	generator.Populate("I am a string", settings);
	Debug.Log("I generated: " + generator.vertexCount + " verts!");
}
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { public Font font; void Start() { TextGenerationSettings settings = new TextGenerationSettings(); settings.textAnchor = TextAnchor.MiddleCenter; settings.color = Color.red; settings.generationExtents = new Vector2(500.0F, 200.0F); settings.pivot = Vector2.zero; settings.richText = true; settings.font = font; settings.fontSize = 32; settings.fontStyle = FontStyle.Normal; settings.verticalOverflow = VerticalWrapMode.Overflow; TextGenerator generator = new TextGenerator(); generator.Populate("I am a string", settings); Debug.Log("I generated: " + generator.vertexCount + " verts!"); } }

Variables

characterCountThe number of characters that have been generated.
characterCountVisibleThe number of characters that have been generated and are included in the visible lines.
charactersArray of generated characters.
fontSizeUsedForBestFitThe size of the font that was found if using best fit mode.
lineCountNumber of text lines generated.
linesInformation about each generated text line.
rectExtentsExtents of the generated text in rect format.
vertexCountNumber of vertices generated.
vertsArray of generated vertices.

Constructors

TextGeneratorCreate a TextGenerator.

Public Functions

GetCharactersPopulate the given List with UICharInfo.
GetCharactersArrayReturns the current UICharInfo.
GetLinesPopulate the given list with UILineInfo.
GetLinesArrayReturns the current UILineInfo.
GetPreferredHeightGiven a string and settings, returns the preferred height for a container that would hold this text.
GetPreferredWidthGiven a string and settings, returns the preferred width for a container that would hold this text.
GetVerticesPopulate the given list with generated Vertices.
GetVerticesArrayReturns the current UILineInfo.
InvalidateMark the text generator as invalid. This will force a full text generation the next time Populate is called.
PopulateWill generate the vertices and other data for the given string with the given settings.