Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Scripting the creation of Text Meshes ?

Discussion in 'Editor & General Support' started by seon, Oct 4, 2007.

  1. seon

    seon

    Joined:
    Jan 10, 2007
    Posts:
    1,441
    Clearly having a blonde moment.... is it possible to create text meshes via script on the fly with specific text in them?

    Like, if I wanted to script the creation of 10 text meshes each with a random letter or number as the text?

    Sure it's obvious, but cant for the life of me work out how!

    BTW.. I'm not interested in instantiating prefabs... this has to be totally scripted, though I know I could instantiate a prefab with a text mesh in it and set the text, but it's not kinda what I want to do. I want to create it all dynamically.

    help please :)
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    31,748
    Since the result would be the same, why not? Out of curiosity.

    --Eric
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    5,976
    Well I find creating simple objects like text meshes in pure script to be nicer and cleaner. The only problem would be setting the font - youhave to get the reference to the font.

    Code (csharp):
    1.  
    2. var go : GameObject = new GameObject("MyText");
    3. tm.text = "blargh";
    4.  
     
  4. seon

    seon

    Joined:
    Jan 10, 2007
    Posts:
    1,441
    Awesome, Thanks.... seems simple now that I see your script :(

    I didn't want a prefab because I will have many combinations of geo/textmesh that I want to randomly generate and figure this wold be a better option.

    Thats said, i do have a prefab version already working, and it does work fine, but I am still creating other elements through scripting so it made more sense to do it all at runtime through scripting.

    cheers :)
     
  5. runevision

    runevision

    Unity Technologies

    Joined:
    Nov 28, 2007
    Posts:
    1,238
    Do anybody have a complete working example of creation of 3d text at runtime? I can't seem to get it to work.

    Here is my code:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TextTest : MonoBehaviour {
    5.  
    6.     public Font myFont;
    7.     public Material myFontMaterial;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         // Create new object to display 3d text
    12.         GameObject myTextObject = new GameObject("myText");
    13.         myTextObject.AddComponent("TextMesh");
    14.         myTextObject.AddComponent("MeshRenderer");
    15.         //myTextObject.renderer.material.color = Color.black;
    16.  
    17.         // Get components
    18.         TextMesh textMeshComponent = myTextObject.GetComponent(typeof(TextMesh)) as TextMesh;
    19.         MeshRenderer meshRendererComponent = myTextObject.GetComponent(typeof(MeshRenderer)) as MeshRenderer;
    20.  
    21.         // Set font of TextMesh component (it works according to the inspector)
    22.         textMeshComponent.font = myFont;
    23.  
    24.         // Create an array of materials for the MeshRenderer component (it works according to the inspector)
    25.         meshRendererComponent.materials = new Material[1];
    26.  
    27.         // Set the text string of the TextMesh component (it works according to the inspector)
    28.         textMeshComponent.text = "Testing";
    29.  
    30.         // Test that we do indeed have a fontMaterial that is not null
    31.         Debug.Log("own fontMaterial name: "+myFontMaterial.name);
    32.         // prints out "own fontMaterial name: font material"
    33.  
    34.         Debug.Log("MeshRenderer Material name before assignment: "+meshRendererComponent.materials[0].name);
    35.         // prints out "MeshRenderer Material name before assignment: Default-Diffuse (Instance)"
    36.  
    37.         // Set the material of the MeshRenderer component (IT DOERSN'T DO ANYTHING according to the inspector)
    38.         meshRendererComponent.materials[0] = myFontMaterial;
    39.  
    40.         Debug.Log("MeshRenderer Material name after assignment: "+meshRendererComponent.materials[0].name);
    41.         // prints out "MeshRenderer Material name after assignment: Default-Diffuse (Instance)"
    42.     }
    43.  
    44.     // Update is called once per frame
    45.     void Update () {
    46.  
    47.     }
    48. }
    Everything works, except assigning the font material. It is as if the line where the font material is assigned is never executed. The log still prints the name of the default material, even though I have assigned a new material (the font material).

    I'd really appreciate some pointers.

    Rune
     
  6. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    In C# you need to use the typeof method in GetComponents, AddComponents etc to pass the actual Type.

    Code (csharp):
    1.  
    2. myObject.AddComponent(typeof(TextMesh));
    3.  
    Edit: I see in your code you had it for GetComponent, just not AddComponent. ;-)

    -Jeremy
     
  7. runevision

    runevision

    Unity Technologies

    Joined:
    Nov 28, 2007
    Posts:
    1,238
    The components were added just fine with my code, so apparently the typeof() method is optional for AddComponent.

    Anyway, after changing it to the AddComponent way the problem I described still prevails. Anyone have any working code to create 3d text at runtime?

    Rune
     
  8. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Sorry Rune, I completely missed the paragraph after your code post...wow, I need to learn how to sleep.

    Interesting that passing AddComponent a string actually worked, but in any event using typeof(...) should be faster. AddComponent returns the Component it just added, so you can skip the additional GetComponent calls and do it all in one go.

    I don't know what's going on with your code though. Perhaps setting the material after setting the font is causing some conflicts? I'd have to play with it, as that sounds weird.

    -Jeremy
     
  9. runevision

    runevision

    Unity Technologies

    Joined:
    Nov 28, 2007
    Posts:
    1,238
    Hey, no problem. :)

    The order doesn't make any difference.

    Playing a bit around with it, it seems I can't figure out how to change the material of *any* MeshRenderer at runtime. So it probably has nothing to do with fonts.

    Anything special about changing the material of a MeshRenderer component at runtime that could cause it not to work and just keep the old material instead?

    Rune
     
  10. runevision

    runevision

    Unity Technologies

    Joined:
    Nov 28, 2007
    Posts:
    1,238
    Okay, I found a solution...

    Code (csharp):
    1. meshRendererComponent.materials[0] = myMaterial;
    DOES NOT WORK! (yet gives no error)

    Code (csharp):
    1. myTextObject.renderer.material = myMaterial;
    WORKS!

    I still have no idea why...

    Rune
     
  11. shifugate

    shifugate

    Joined:
    Nov 26, 2012
    Posts:
    1
    using UnityEngine;

    using System.Collections;

    public class TextTest : MonoBehaviour
    {
    public Font font;

    public Material[] fontMaterials;

    void Start ()
    {
    GameObject textObject = new GameObject("text");
    textObject.AddComponent("TextMesh");
    textObject.AddComponent("MeshRenderer");

    TextMesh textMeshComponent = textObject.GetComponent(typeof(TextMesh)) as TextMesh;
    textMeshComponent.font = font;

    MeshRenderer meshRendererComponent = textObject.GetComponent(typeof(MeshRenderer)) as MeshRenderer;

    textMeshComponent.text = "Testing";

    meshRendererComponent.materials = fontMaterials;
    }
    }

    ;)
     
  12. stgu

    stgu

    Joined:
    Sep 4, 2017
    Posts:
    1
    are you aware that your "blonde moment" statement is actually racist? no kidding. don't do that. it hurts people's feelings. hair or skin colour is no indicator of intelligence.