Search Unity

What is TextGenerator good for?

Discussion in 'Scripting' started by IfItsPeeLetItBe, Oct 24, 2014.

  1. IfItsPeeLetItBe

    IfItsPeeLetItBe

    Joined:
    Jul 31, 2014
    Posts:
    4
    I've been pouring over the API and I can understand what most of the classes are good for. Except this one escapes me. The API doesn't seem to be specific about what it does. Does it work like TextMesh?

    What does TextGenerator do?

    Can someone post a simple example of how I can use it. I see the code example listed inside the API. I attach a font to the Font variable and I get it to fire inside of a C# script in Unity, but then I don't see anything different inside the world. What is this good for?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ExampleClass : MonoBehaviour {
    5.     public Font font;
    6.     void Start() {
    7.         TextGenerationSettings settings = new TextGenerationSettings();
    8.         settings.anchor = TextAnchor.MiddleCenter;
    9.         settings.color = Color.red;
    10.         settings.extents = new Vector2(500.0F, 200.0F);
    11.         settings.pivot = Vector2.zero;
    12.         settings.richText = true;
    13.         settings.font = font;
    14.         settings.size = 32;
    15.         settings.style = FontStyle.Normal;
    16.         settings.wrapMode = TextWrapMode.Wrap;
    17.         TextGenerator generator = new TextGenerator();
    18.         generator.Populate("I am a string", settings);
    19.         Debug.Log("I generated: " + generator.vertexCount + " verts!");
    20.     }
    21. }
     
    Last edited: Oct 24, 2014
  2. PrefabEvolution

    PrefabEvolution

    Joined:
    Mar 27, 2014
    Posts:
    225
    this class is used to generate text meshes, and create your own text render system. just generate mesh and assign mesh to the meshfilter and proper font materials to the mesh renderer.
     
  3. IfItsPeeLetItBe

    IfItsPeeLetItBe

    Joined:
    Jul 31, 2014
    Posts:
    4
    How would I get from that code above to a rendered mesh? I've tried this

    Code (CSharp):
    1. Gameobject myMesh02 = GameObject.Find("Cube")
    2. myMesh02.GetComponent<MeshFilter>().Mesh = generator;
     
  4. PrefabEvolution

    PrefabEvolution

    Joined:
    Mar 27, 2014
    Posts:
    225
    Code (csharp):
    1.  
    2. static public Mesh TextGenToMesh(TextGenerator generator, Mesh mesh = null)
    3. {
    4.     if (mesh = null)
    5.         mesh = new Mesh();
    6.  
    7.     mesh.vertices = generator.verts.Select(v => v.position).ToArray();
    8.     mesh.colors32 = generator.verts.Select(v => v.color).ToArray();
    9.     mesh.uv = generator.verts.Select(v => v.uv).ToArray();
    10.     mesh.triangles = new int[generator.vertexCount * 6];
    11.     for(var i = 0; i < mesh.triangles.Count; )
    12.     {
    13.         var t = i;
    14.         mesh.triangles[i++] = t;
    15.         mesh.triangles[i++] = t + 1;
    16.         mesh.triangles[i++] = t + 2;
    17.         mesh.triangles[i++] = t;
    18.         mesh.triangles[i++] = t + 2;
    19.         mesh.triangles[i++] = t + 3;
    20.     }
    21.     return mesh;
    22. }
    23.  
    This function will generate valid mesh form TextGen. Material for the MeshRenderer you can get from settings.font.material.
     
  5. keithAtPlay

    keithAtPlay

    Joined:
    Feb 5, 2015
    Posts:
    4
    I know this is a now 9 year old post, but it's the best response when searching about using the TextGenerator.

    But, PrefabEvolutions conversion script just flat out won't work. It's attempting to use UIVertices (Vector4s) in a standard Mesh uv (Vector2s).

    I'm not adept enough at this to know the solution here (although I'm trying at the moment).
     
  6. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,108
    We all use Text Mesh Pro (TMPro) for a while now. I'm not sure if anyone really uses Unity's vanilla text system anymore.