- Home /
Using TextGenerator
There is little to no information about the TextGenerator, I am trying to use it to allow me to create 3d text and word wrap to set a bounds, however it apparently generates Vertices but what am I supposed to do with them.
Again I have found no information on this, and am wondering what the heck this is good for ?
Did you ever find an answer to your question? I'm also generating Vertices but nothing more and I really don't know what I'm supposed to do.
EDIT: the solution is "simply" that you need to assign to the CanvasRenderer the material needed by your font. It'd be nice if the example in the documentation could show this.
$$anonymous$$g. canvasRenderer.Set$$anonymous$$aterial(font.material, null);
Answer by MrFloat · Sep 04, 2016 at 12:36 PM
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[i] = generatorVerts[i].position;
tempColours[i] = generatorVerts[i].color;
tempUvs[i] = generatorVerts[i].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;
Has anyone been able to use this on a $$anonymous$$eshRenderer? I can't seem to convert IList to an array.
What does your code look like? The Get$$anonymous$$esh() function should populate a mesh for you if you're using that code.
I'm a bit of a noob to this, I think the issue is getting the IList UIVertex generatorVerts = i_Generator.verts; data in order to calculate vertex positions for the mesh. I'm just not sure UIVertex is compatible with $$anonymous$$eshRenderer. Thanks for your help on this so far.
Well you are correct at assu$$anonymous$$g that UIVertex data is not compatible with $$anonymous$$eshRenderer. That data structure is a more efficient/propper way of storing per vertex data but most things like $$anonymous$$eshRenderer uses the old OpenGL format of seperate array lists of data and that's why Get$$anonymous$$esh() function goes through UIVertex and seperates it out into seperate vertex, color and uv lists. The UIVertex data that you get back from the generator is also already optimised for text purposes and defines every character with 1 quad (4 points) rather than the generic format of 2 triangles (6 points). So the wierd looking for loop that goes through every character creates a list of indices that maps 4 actual points in the vertex data list to 6 entries.
Not sure what problems you're running into but if it's not rendering propperly for you try having a breakpoint and checking the UIVertex list that the generator gives you. I mentioned making sure to set "lineSpacing" and "scaleFactor" values on my post because leaving them at 0 will give you very odd values from the generator but I might have missed some other important properties that need to be set. Just wondering if the data your working with from the generator is actually correct.
Got it working! Thanks for your info and help with the code. UpVoted.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Adjusting the transform width to match wrapped text (TextMeshPro) 0 Answers
UnityEngine.Input.GetMouseButton(1)) issue 1 Answer
Change Textmesh.text from script 2 Answers