- Home /
SOLVED - 3D Text - How to apply new font type in Runtime
Hello Guys,
Please help. I have the following code to assign a new font type to a TextMesh, the font type is assigned to the TextMesh, but only blocks are appearing instead of the text. If still in Runtime I change to the default built in Arial, everything is ok. What can be the problem? As always, thank you in advance for any suggestion.
var tm : TextMesh = TheText.GetComponent(TextMesh);
tm.text = "Some string";
tm.fontSize=80;
tm.font= Resources.Load("calibrili") as Font;
Thanks to this question, fixed a problem I was having for a few weeks. Please up vote this!
Answer by Owen-Reynolds · Oct 02, 2013 at 01:23 PM
Also change the Material.
You can see this not in run-time, with manual changes. Sometimes changing just the font will accidentally look fine, and Ariel+no material usually works. But more often than not you get that same blockiness if you don't create and use a proper Font Material (or using the wrong one produces shifted, rotated letter parts.)
Yes, this is causing the problem. $$anonymous$$y only problem is, that I need to run it in runtime and assign the font material in code. I tried it the below way, but apparently it is not working. Can you give an idea how to do it? Thank you for the answer in advance.
tm.renderer.material = Resources.Load("calibrili") as $$anonymous$$aterial;
Just to be sure, might need to hand-make a font material (the default may not load properly in run-time.)
Otherwise, I don't use Resourses.Load
and have never swapped a font while running. But, the standard "Unity" method might avoid some glitch:
public $$anonymous$$aterial[] Font$$anonymous$$ats;
, load them up in the Inspector by hand, in the usual way. Don't need to use an array, but may as well. Then tm.renderer.material=Font$$anonymous$$ats[0];
.
Owen, absolutely clear and serves my needs!!! Thank you a lot!
Answer by Lertulo · Jul 22, 2014 at 04:15 AM
Just came across this: was adding a TextMesh dynamically and all I got out was boxes--capital letters were bigger boxes, sure, but just boxes. As suggested above, the solution was adding the tagged line below:
TextMesh tm = obj.AddComponent<TextMesh>() as TextMesh;
tm.characterSize = 0.5f;
tm.text = "xXx";
tm.color = Color.green;
tm.anchor = TextAnchor.MiddleCenter;
tm.transform.position = new Vector3(0,0,4);
tm.font = GuiFont;
MeshRenderer rend = obj.GetComponentInChildren<MeshRenderer>();
rend.material = tm.font.material; /* ADDED THIS */
For your text needs, I would look into using Text$$anonymous$$esh Pro which is an incredible alternative to Text$$anonymous$$esh. The quality of the text rendering is just amazing plus the improved text layout options are great.
If you use text in your games or projects, you should seriously check Text$$anonymous$$esh Pro out. I realize it is not free but it is pretty amazing.
text$$anonymous$$esh.font=newFont; text$$anonymous$$esh.GetComponent().material=newFont.material;
works for me. Thank you for the hint Lertulo!
If I don't add the 2nd line Unity renders distorted characters after setting the new font.
Your answer
Follow this Question
Related Questions
Jagged and pixelated fonts in text mesh 3d (iphone) 4 Answers
How to Get a Char from his name ? 1 Answer
Changing the font of a textmesh? 1 Answer
3d text with z axis depth 1 Answer