Search Unity

TextMesh Pro How to properly load everything TMP needs at runtime?

Discussion in 'UGUI & TextMesh Pro' started by jonahyakuto, Jun 21, 2019.

  1. jonahyakuto

    jonahyakuto

    Joined:
    Mar 9, 2017
    Posts:
    11
    Hello. We're creating all of our text at runtime. We have a loading period where we load all of the assets needed for a certain screen i.e. textures, fonts, etc. However, I notice that the first time we create a text takes way much longer than the second time or any time after. I wonder if we need to load something else other than the font we use to create the text?

    Code (CSharp):
    1. public class TMP_Test : MonoBehaviour
    2. {
    3.     async void Start()
    4.     {
    5.         var font = Resources.Load<TMP_FontAsset>($"Fonts & Materials/Bangers SDF");
    6.  
    7.         await Task.Delay(1000);
    8.  
    9.         var sw = Stopwatch.StartNew();
    10.  
    11.         var txt0 = CreateText("txt0", "Hello World!", font);
    12.  
    13.         Debug.Log($"txt0 - ms: {sw.ElapsedMilliseconds}  t: {sw.ElapsedTicks}");
    14.  
    15.         await Task.Delay(1000);
    16.        
    17.         Destroy(txt0);
    18.  
    19.         await Task.Delay(1000);
    20.  
    21.         sw.Restart();
    22.  
    23.         CreateText("txt1", "Hello World!", font);
    24.  
    25.         Debug.Log($"txt1 - ms: {sw.ElapsedMilliseconds}  t: {sw.ElapsedTicks}");
    26.     }
    27.  
    28.     GameObject CreateText(string name, string text, TMP_FontAsset font)
    29.     {
    30.         var go = new GameObject(name);
    31.         var tmp = go.AddComponent<TextMeshPro>();
    32.         tmp.font = font;
    33.         tmp.isOrthographic    = true;
    34.         tmp.rectTransform.sizeDelta = new Vector2(200, 50);
    35.         tmp.text = text;
    36.         return go;
    37.     }
    38. }
    I get these results running the code above:
    Screenshot 2019-06-21 at 14.43.31.png

    If we're going to create all our text dynamically at runtime, what are the best practices of going about doing this approach?

    Thanks in advance!
     
  2. jonahyakuto

    jonahyakuto

    Joined:
    Mar 9, 2017
    Posts:
    11
    Any ideas on this?
     
  3. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Creating / loading a text object the first time will indeed take more time as a result of having to load resources used by the text system.

    Additional resources are loaded in the Editor so make sure you profile this in a build.

    Creating a single / initial text object when the first scene is loaded will ensure these resources are loaded then when timing / the overhead might not be as crucial at that given time. Subsequent text object creation or instantiation should then be fine.

    I have not compared creating a new object and adding all the necessary components at runtime vs. using an existing prefab with the same components and just adjusting some of the values as needed. You might compare those two to see if using / instantiating a prefab might be more efficient.

    P.S. I will verify the loading of these resources on my end to make sure everything is behaving as expected.