Murphy
March 22, 2010, 3:43pm
1
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?
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";
}
system
March 22, 2010, 6:34pm
2
Try doing the same procedure in the editor and compare the result of your script with the working textmesh from the editor.
system
August 29, 2011, 3:16am
4
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.
Reiv3r
September 24, 2011, 11:11pm
5
OLD ANSWER. But you need to attach the font material to the Mesh Renderer.
Reiv3r
September 24, 2011, 11:11pm
6
Old solution. You need to attach the font material to the Mesh Renderer you instantiate.
Slimmed down Version.
C#
public Font guiFont;
public Material guiFontMaterial;
GameObject _textGo = new GameObject("Text");
TextMesh _text = (TextMesh)_textGo.AddComponent( typeof(TextMesh) );
_textGo.AddComponent( typeof(MeshRenderer) );
_textGo.renderer.material = guiFontMaterial;
_text.text = "Hello world";
_text.font = guiFont;
Same answer, but I made a few tweaks to it
public static GameObject Create3DText(string text)
{
GameObject textObject = new GameObject();
TextMesh textMesh = textObject.AddComponent(typeof(TextMesh)) as TextMesh;
textObject.AddComponent(typeof(MeshRenderer));
textMesh.text = text;
MeshRenderer meshRenderer = textObject.GetComponent(typeof(MeshRenderer)) as MeshRenderer;
meshRenderer.material = Resources.Load("Arial", typeof(Material)) as Material;
//You need to create a folder called "Resourses" in the assets
//To create a font, drag it from c:Windows\Fonts\ to the Unity-assets-Resourses folder
Font font = Resources.Load("ARIAL", typeof(Font)) as Font;
textMesh.font = font;
return textObject;
}