For those who are still searching how to use the TextGenerator class:
I personally had some problems setting up the TextGenerationSettings. When you create your own TextGenerationSettings, it has no sensible default values
(probably something to do with it being a struct, but on the same note, not sure why it was decided to make this fairly large container into a struct even though you can end up passing it to the TextGenerator a lot of times, forcing a copy of all the values inside).
Bellow is just an example of the settings I ended up using in my tests but some important ones to not miss are lineSpacing and scaleFactor as the default 0 values can mess up the vertex positioning a lot.
var m_TextSettings = new TextGenerationSettings();
m_TextSettings.textAnchor = TextAnchor.MiddleCenter;
m_TextSettings.color = Color.red;
m_TextSettings.generationExtents = new Vector2(100, 100);
m_TextSettings.pivot = new Vector2(0.5f, 0.5f); ;
m_TextSettings.richText = true;
m_TextSettings.font = m_Font;
m_TextSettings.fontSize = 14;
m_TextSettings.fontStyle = FontStyle.Normal;
m_TextSettings.verticalOverflow = VerticalWrapMode.Overflow;
m_TextSettings.horizontalOverflow = HorizontalWrapMode.Wrap;
m_TextSettings.lineSpacing = 1;
m_TextSettings.generateOutOfBounds = true;
m_TextSettings.resizeTextForBestFit = false;
m_TextSettings.scaleFactor = 1f;
As mentioned in one of the comments, if you are using a CanvasRenderer, you need to set material and mesh:
m_CanvasRenderer.SetMaterial(m_Font.material, null);
m_CanvasRenderer.SetMesh(m_Mesh);
If you are using a MeshRenderer, you probably want a MeshFilter component as well and assign a new mesh to it:
GetComponent<MeshFilter>().mesh = m_Mesh;
m_MeshRenderer.sharedMaterial = m_Font.material;
To generate a mesh from TextGenerator, I made this extension method:
public static class TextExtensions
{
static public void GetMesh(this TextGenerator i_Generator, Mesh o_Mesh)
{
if (o_Mesh == null)
return;
int vertSize = i_Generator.vertexCount;
Vector3[] tempVerts = new Vector3[vertSize];
Color32[] tempColours = new Color32[vertSize];
Vector2[] tempUvs = new Vector2[vertSize];
IList<UIVertex> generatorVerts = i_Generator.verts;
for (int i = 0; i < vertSize; ++i)
{
tempVerts _= generatorVerts*.position;*_
tempColours = generatorVerts*.color;*
tempUvs = generatorVerts*.uv0;*
}
o_Mesh.vertices = tempVerts;
o_Mesh.colors32 = tempColours;
o_Mesh.uv = tempUvs;
int characterCount = vertSize / 4;
int[] tempIndices = new int[characterCount * 6];
for(int i = 0; i < characterCount; ++i)
{
int vertIndexStart = i * 4;
int trianglesIndexStart = i * 6;
tempIndices[trianglesIndexStart++] = vertIndexStart;
tempIndices[trianglesIndexStart++] = vertIndexStart + 1;
tempIndices[trianglesIndexStart++] = vertIndexStart + 2;
tempIndices[trianglesIndexStart++] = vertIndexStart;
tempIndices[trianglesIndexStart++] = vertIndexStart + 2;
tempIndices[trianglesIndexStart] = vertIndexStart + 3;
}
o_Mesh.triangles = tempIndices;
//TODO: setBounds manually
o_Mesh.RecalculateBounds();
}
}
Hope this helps some people;