- Home /
Create a 3d text using c# (font problem)
Hello there !
I am trying to create a 3d Text using c#, but i have trouble with the fact that instead of text, it displays a pink square. The problem, in my opinion, is that it doesn't have any texture, because it doesn't have any font. Here is the code :
GOTroopsCountIn = new GameObject("TroopsCount");
GOTroopsCountIn.AddComponent("TextMesh");
GOTroopsCountIn.AddComponent("MeshRenderer");
TextMesh TMTroopsCountIn = (TextMesh) GOTroopsCountIn.GetComponent("TextMesh");
TMTroopsCountIn.font = ??????????? ;
So here is my question : how can i find a font to assignate it to my TextMesh (using c#) ? (The default font, Arial)
Have a nice day !
Answer by robertbu · Jul 13, 2013 at 06:52 AM
The pink squares are what you get when you don't have a material assigned, or if your shader has errors. I use EZGUI rather than TextMesh, so I had to play a bit to get something to work.
I dragged and dropped a TTF font into the project. In order to get the letters to appear on the texture associated with the font, I had to change the font import settings 'Character' property to 'Unicode'. Then I followed the instructions here to create a shader and material:
http://wiki.unity3d.com/index.php?title=3DText
Next I attached the following script to an empty game object:
var mat : Material;
var font : Font;
function Awake() {
var Text = new GameObject();
var textMesh = gameObject.AddComponent(TextMesh);
textMesh.font = font;
var meshRenderer = gameObject.AddComponent(MeshRenderer);
meshRenderer.material = mat;
textMesh.text = "Hello World!";
}
I dragged and dropped the font and the material onto the public variables and hit play.
Answer by shopguy · Sep 18, 2014 at 02:29 AM
This seems easier. No custom shader, unless you needed the shader for some other reason. A Font object includes Font.material, and when you add a TextMesh it includes the render, so you can skip some steps and just do:
var font : Font;
function Awake() {
var Text = new GameObject();
var textMesh = Text.AddComponent(TextMesh);
textMesh.font = font;
textMesh.GetComponent<Renderer>().material = font.material;
textMesh.text = "Hello World!";
}
Nice when you find your own answers a year later and they help you :) Anyway, just updated for Unity 5, since text$$anonymous$$esh.renderer is depreciated now.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How do I access built in shaders and fonts? 1 Answer
Distribute terrain in zones 3 Answers
How to access a Text Mesh Pro Font Asset's glyp info from c#? 1 Answer
TextMesh is not rendering Font correctly 0 Answers