- Home /
Procedurally generate a TextMesh
I am trying to display 3d text in the world and TextMesh seems to be the way to go. Here is what I have so far:
var wallTxt = new GameObject("WallText");
wallTxt.AddComponent(TextMesh);
wallTxt.AddComponent(MeshRenderer);
wallTxt.GetComponent (TextMesh).text = "Hello World";
var myFont : Font = (Resources.Load("Courier") as Font);
wallTxt.GetComponent(TextMesh).font = myFont;
wallTxt.transform.position.y = 10;
GameObject.CreatePrimitive(PrimitiveType.Cube).transform.position.y = 10;
It displays something but the text is all black blocks instead of letters. Any ideas how to do this properly?
Answer by Eric5h5 · Mar 22, 2010 at 07:04 PM
It's simpler if you create a prefab of a TextMesh, and then instantiate the prefab.
var textMeshPrefab : TextMesh;
function Start () { var wallTxt : TextMesh = Instantiate(textMeshPrefab, Vector3.up*10, Quaternion.identity); wallTxt.text = "Hello World"; }
0
The answer to this question is to instantiate a Text$$anonymous$$esh from a prefab. But is that the most elegant solution? I ran into the same roadblock when trying to build a Text$$anonymous$$esh dynamically from code. And I'm not finding any other answers.
Is there a reason why a dynamically generated Text$$anonymous$$esh does not have the same default material and font as one that you create in the editor?
Answer by wrongkiddied · Aug 29, 2011 at 03:16 AM
You were on the right track. After some futzing I got this to work:
var wallTxt = new GameObject("TextField"); wallTxt.AddComponent(TextMesh); wallTxt.AddComponent(MeshRenderer); var meshRender: MeshRenderer = wallTxt.GetComponent(MeshRenderer); var material: Material = meshRender.material; meshRender.material = Resources.Load("Arial", Material); wallTxt.GetComponent (TextMesh).text = "Hello world"; var myFont : Font = Resources.Load("Arial",Font); wallTxt.GetComponent(TextMesh).font = myFont;
Just be sure to create a Resources folder in the Assets folder and put the font you want to load in there.
Answer by forest j · Mar 22, 2010 at 06:34 PM
Try doing the same procedure in the editor and compare the result of your script with the working textmesh from the editor.
Answer by Reiv3r · Sep 24, 2011 at 11:11 PM
OLD ANSWER. But you need to attach the font material to the Mesh Renderer.
Thanks for co$$anonymous$$g our timberland online store! Just enjoy yourself here! As a developping company, timberland boots always can give us some surprise .I belive timberland shoes Sale can make your feet more comfortable, make your life more stylish!If you want to have a try ,just click here: discount timberland boots Free delivery
Answer by Reiv3r · Sep 24, 2011 at 11:11 PM
Old solution. You need to attach the font material to the Mesh Renderer you instantiate.
Your answer

Follow this Question
Related Questions
Creating an outline around the opaque pixels on a plane. Outline should extend outside of plane. 0 Answers
Procedurally combine (merge) two Texture2D 2 Answers
Procedural Shader Cost/Draw Calls 1 Answer
How to make this low poly terrain look better 1 Answer
Asset store categories - How to determine best fit for my asset 0 Answers