Hi all
I’m making TextMeshes for some of my objects by duplicating the GameObject and adding a TextMesh component. This is my method:
private void __buildTextDuplicate()
{
this.__textDuplicate = Instantiate(this.gameObject,
this.gameObject.transform.position,
this.gameObject.transform.rotation) as GameObject;
this.__textDuplicate.transform.parent = this.gameObject.transform;
this.__textDuplicate.name = "Text";
// Remove our components
Destroy(this.__textDuplicate.GetComponent<Element>());
Destroy(this.__textDuplicate.GetComponent<MeshCollider>());
// Set the text material
Material textMaterial = ResourceManager.getResource<Material>("Materials", "text");
this.__textDuplicate.GetComponent<MeshRenderer>().material = textMaterial;
TextMesh textMesh = this.__textDuplicate.AddComponent<TextMesh>();
// Setup the text options
textMesh.characterSize = 0.1f;
textMesh.fontSize = 100;
textMesh.richText = false;
textMesh.offsetZ = 0.01f;
Font font = (Font)Resources.GetBuiltinResource(typeof(Font), "Arial.ttf");
textMesh.font = Instantiate(font) as Font;
}
My result looks like this:
Now, there are a couple of issues here:
-
The text isn’t alpha’d correctly. If I go into the inspector and set the font to None and then back to Arial again, sometimes it will correct itself.
-
The text is flipped horizontally. Why does this happen? The original geometry is Y up, Z towards camera.
I guess it’s pretty clear that my approach of programatically adding text to objects is wrong. Can anyone suggest a stable way of doing this?
Thanks,
Ves