- Home /
Unable to apply material to text procedurally
Hello,
I wish to apply 3D text to an object procedurally. I have successfully managed to do everything except apply the material correctly.
I created a Text Mesh and applied a new material to its renderer with:
void Attach3DText ()
{
// Find font resource
Font ArialFont = (Font) Resources.GetBuiltinResource (typeof (Font), "Arial.ttf");
// Add text mesh and mesh renderer components to menu object
gameObject.AddComponent("TextMesh");
// Declare variables for TextMesh and MeshRenderer
TextMesh textMesh = gameObject.GetComponent (typeof (TextMesh) ) as TextMesh;
MeshRenderer meshRenderer = gameObject.GetComponent (typeof (MeshRenderer) ) as MeshRenderer;
// Define attributes for text mesh
textMesh.text = name;
textMesh.font = ArialFont;
textMesh.fontSize = TEXT_SIZE;
textMesh.anchor = TextAnchor.MiddleCenter;
// Attach material to renderer
Material textMaterial = new Material (Shader.Find ("GUI/Text Shader") );
renderer.material = textMaterial;
}
However, at this time my text appears as a series of white blocks, rather than letters. Obviously, I am missing some attribute, but I don't know what.
Can anybody help me understand what I am missing here?
Thank you in advance,
Peter
I would seem that this is not a trivial thing. Whilst I am sure that this, and indeed almost anything, is possible to be done procedurally, Unity seems to be built in a way that discourages it.
I have opted to build a prefab for the text display, then simply instantiate it. This IS a trivial option. However, for future reference, I would be interested to hear if anyone found another solution to this.
Your answer
Follow this Question
Related Questions
Part of text with different material 1 Answer
Changing Font material variables in script 1 Answer
Material doesn't have a color property '_Color' 4 Answers
UI Text - changing material instance 1 Answer
Lens Flare & Auto Hide 0 Answers