- Home /
How do I access built in shaders and fonts?
I'm trying to dynamically create a TextMesh in one of my scripts. I want to use Unity's standard Arial font and Font Material. However, when I add a TextMesh component via script, all of the material and font references are blank and I must load them in manually. Here's a really basic example of what I have:
public class MenuElement : MonoBehaviour
{
public TextMesh textMesh;
void Start()
{
textMesh = gameObject.AddComponent<TextMesh> ();
textMesh.anchor = TextAnchor.MiddleCenter;
textMesh.fontSize = 30;
textMesh.characterSize = 0.1f;
textMesh.text = gameObject.name;
}
}
So from what I understand, I also have to initialize the font and its material (via MeshRenderer). This would be fine, but I'm not sure how I can reference Unity's pre-made materials and fonts. I'm assuming I have to use Resources.Load(), but I believe that only works with my project's resources. Is there a way to use the same font and material that the standard TextMesh component uses when you spawn it via Component > Create Other > 3D Text? If not, is there a better way of creating Text Mesh's procedurally?
Answer by tanoshimi · Oct 03, 2014 at 08:16 AM
Try this:
using UnityEngine;
using System.Collections;
public class SetTextMeshFont : MonoBehaviour {
private TextMesh textMesh;
void Start () {
// Grab the textmesh component
textMesh = GetComponent<TextMesh>();
// Now get the built in font
Font ArialFont = (Font)Resources.GetBuiltinResource (typeof(Font), "Arial.ttf");
// Apply the font and material to the textmesh
textMesh.font = ArialFont;
textMesh.renderer.material = ArialFont.material;
}
}
This worked. I missed the BuiltinResource function when I was browsing the documentation.
Thanks!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to access a Text Mesh Pro Font Asset's glyp info from c#? 1 Answer
Distribute terrain in zones 3 Answers
TextMesh is not rendering Font correctly 0 Answers