Search Unity

TextMesh paint just squares

Discussion in 'Scripting' started by Dazzid, Jun 11, 2014.

  1. Dazzid

    Dazzid

    Joined:
    Apr 15, 2014
    Posts:
    61
    Hi All,
    I have a little problem
    I'm painting text with TextMesh. It is created using the position of a node in a 3D space. It is working well the position, but the text doesn't appears in the screen, I only see white squares that looks similar to the text in size and shape. What is my mistake?

    Thanks for the help
    Code (CSharp):
    1.    
    2. public void paintText ()
    3.         {
    4.    
    5.                 for (int i = 1; i < gm.nodeCount; i++) {
    6.                         if (gm.Nodes [i].weight_node > 1.5) {
    7.                                 TextMesh n_text = new GameObject ("Text_" + i).AddComponent ("TextMesh") as TextMesh;
    8.                                 n_text.transform.parent = gameObject.transform;
    9.                                 n_text.GetComponent<MeshRenderer> ().castShadows = false;
    10.                                 n_text.GetComponent<MeshRenderer> ().receiveShadows = false;
    11.                                 n_text.transform.localPosition = gm.Nodes [i].newPosition;
    12.                                 Material material = new Material (Shader.Find ("Particles/Alpha Blended"));
    13.                                 n_text.renderer.material = material;
    14.                                 n_text.renderer.material.SetColor ("_TintColor", nodeColor);
    15.                                 UnityEngine.Object.Destroy (n_text.GetComponent<Rigidbody> ());
    16.                                 UnityEngine.Object.Destroy (n_text.GetComponent<MeshCollider> ());
    17.                                 n_text.font = newFont;
    18.                                 n_text.text = gm.Nodes [i].currentRegionIndex;
    19.                         }
    20.                 }
    21.         }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Probably when you change the material of the textmesh, it is losing all the font's graphic data. Solution: don't do that. Change n_text.color instead of replacing the whole material.

    There was a bug where changing the font at runtime could cause similar issues. I don't think that's your problem here, though, plus I haven't (personally) seen that bug in a few versions.
     
  3. Dazzid

    Dazzid

    Joined:
    Apr 15, 2014
    Posts:
    61
    I still don't find the solution.
    Is Start() I call the font like this: newFont = Resources.Load("verdana", typeof(Font)) asFont;
    And the code compiles, then the object also show the font, but I need to manually change to none and then back to "verdana" and then it works! Don't get it
    I don't care about verdana font, I also tried with Helvetica, Arial, and the problem is the same.
    Thanks for the help

    Code (CSharp):
    1. public void paintText ()
    2.         {
    3.    
    4.                 for (int i = 1; i < gm.nodeCount; i++) {
    5.                         if (gm.Nodes [i].weight_node > 1.5) {
    6.                                 TextMesh n_text = new GameObject ("Text_" + i).AddComponent ("TextMesh") as TextMesh;
    7.                                 n_text.font = newFont;
    8.                                 n_text.transform.parent = gameObject.transform;
    9.                                 n_text.GetComponent<MeshRenderer> ().castShadows = false;
    10.                                 n_text.GetComponent<MeshRenderer> ().receiveShadows = false;
    11.                                 n_text.transform.localPosition = gm.Nodes [i].newPosition;
    12.                                 //Material material = new Material (Shader.Find ("Particles/Alpha Blended"));
    13.                                 //n_text.renderer.material = material;
    14.                                 //n_text.renderer.material.SetColor ("_TintColor", nodeColor);
    15.                                 //UnityEngine.Object.Destroy (n_text.GetComponent<Rigidbody> ());
    16.                                 //UnityEngine.Object.Destroy (n_text.GetComponent<MeshCollider> ());
    17.                                 n_text.fontSize = 12;
    18.                                 n_text.richText = true;
    19.                                 n_text.fontStyle = FontStyle.Normal;
    20.                                 n_text.text = gm.Nodes [i].currentRegionIndex;
    21.                         }
    22.                 }
    23.         }
     
  4. Dazzid

    Dazzid

    Joined:
    Apr 15, 2014
    Posts:
    61
    I have seen that is not importing the Font Material... How can I add it by code?
     
  5. Dazzid

    Dazzid

    Joined:
    Apr 15, 2014
    Posts:
    61
    Now I tried with
    Material material = (Material)Resources.Load ("verdana/Font Material",typeof(Material));
    n_text.renderer.materials[0] = material;

    And it creates the material but is not the correct one....
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    I've never tried setting its font by code, alas. So I'm not sure how it responds and can't really offer any further help.
     
  7. Dazzid

    Dazzid

    Joined:
    Apr 15, 2014
    Posts:
    61
    Resolved with:
    n_text.transform.parent = gameObject.transform;
    n_text.renderer.material = n_text.font.material;