Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

TextMesh Pro Maximum text length and material error

Discussion in 'UGUI & TextMesh Pro' started by galloper, Jan 28, 2020.

  1. galloper


    Aug 5, 2017
    Hello, I'm working on a project that has some really long texts and I noticed that using TMP gives me some limitations. So what exactly is the maximum text length for TMP and can we fix this problem?

    I have a TMP input field (it has to be editable) where I import the text, when the text is rather small (around 1000 words) it's all fine.
    Then, not changing a thing, I try to import some big text and I get such errors:

    IndexOutOfRangeException: Index was outside the bounds of the array.
    TMPro.MaterialReference.AddMaterialReference (UnityEngine.Material material, TMPro.TMP_FontAsset fontAsset, TMPro.MaterialReference[] materialReferences, System.Collections.Generic.Dictionary`2[TKey,TValue] materialReferenceIndexLookup) (at Library/PackageCache/com.unity.textmeshpro@2.0.1/Scripts/Runtime/MaterialReferenceManager.cs:596)
    TMPro.TextMeshProUGUI.SetArraySizes (TMPro.TMP_Text+UnicodeChar[] chars) (at Library/PackageCache/com.unity.textmeshpro@2.0.1/Scripts/Runtime/TMPro_UGUI_Private.cs:1358)
    TMPro.TMP_Text.ParseInputText () (at Library/PackageCache/com.unity.textmeshpro@2.0.1/Scripts/Runtime/TMP_Text.cs:1716)
    TMPro.TextMeshProUGUI.OnPreRenderCanvas () (at Library/PackageCache/com.unity.textmeshpro@2.0.1/Scripts/Runtime/TMPro_UGUI_Private.cs:1637)
    TMPro.TextMeshProUGUI.ForceMeshUpdate () (at Library/PackageCache/com.unity.textmeshpro@2.0.1/Scripts/Runtime/TextMeshProUGUI.cs:483)
    TMPro.TMP_InputField.UpdateLabel () (at Library/PackageCache/com.unity.textmeshpro@2.0.1/Scripts/Runtime/TMP_InputField.cs:3032)
    TMPro.TMP_InputField.SetText (System.String value, System.Boolean sendCallback) (at Library/PackageCache/com.unity.textmeshpro@2.0.1/Scripts/Runtime/TMP_InputField.cs:549)
    TMPro.TMP_InputField.set_text (System.String value) (at Library/PackageCache/com.unity.textmeshpro@2.0.1/Scripts/Runtime/TMP_InputField.cs:479)

    NullReferenceException: Object reference not set to an instance of an object
    TMPro.TextMeshProUGUI.GenerateTextMesh () (at Library/PackageCache/com.unity.textmeshpro@2.0.1/Scripts/Runtime/TMPro_UGUI_Private.cs:1986)
    TMPro.TextMeshProUGUI.OnPreRenderCanvas () (at Library/PackageCache/com.unity.textmeshpro@2.0.1/Scripts/Runtime/TMPro_UGUI_Private.cs:1656)
    TMPro.TextMeshProUGUI.Rebuild (UnityEngine.UI.CanvasUpdate update) (at Library/PackageCache/com.unity.textmeshpro@2.0.1/Scripts/Runtime/TextMeshProUGUI.cs:209)
    UnityEngine.UI.CanvasUpdateRegistry.PerformUpdate () (at M:/HubInstalls/2019.2.15f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/CanvasUpdateRegistry.cs:198)
    UnityEngine.Canvas:SendWillRenderCanvases() (at C:/buildslave/unity/build/Modules/UI/ScriptBindings/UICanvas.bindings.cs:72)

    Can something be done about this limitation?
  2. Quentin_MM


    Sep 20, 2017
    I do have the same issue here.
    I've investigate on my side, and the TMP font atlas i use has reference to other font atlas for bold, bold italic and regular italic font weight.

    When I remove the reference I have for bold weight it's working well.
    In play mode, since I add any reference in bold weight, it create the exception.

    Anyone has an idea to fix this?
  3. galloper


    Aug 5, 2017
    Well my friend, we're stuck.
    If you have long texts, you probably need something nice to have scrolling and in general TMP is so nice, why use anything else? However, it isn't made for displaying very long text. Native TextView in Android (let's say we coded in Java) has literally no limits in displaying text, it can be of any length. TMP is working inside the Unity engine and every letter it produces is a "graphic" in terms of said engine, vertices and all that.
    On top of that add the font atlas problem - if you want many different letters, for example, not just latin, you'll end up with a font atlas that is quite big in size. It will slow even the loading of your app on lower end devices (I tested it) and so on...
    Hope Unity UI guys will someday make a reasonable Text UI component without offloading it all to the TMP author.
  4. Stephan_B


    Unity Technologies

    Feb 26, 2017
    I continue to work on TextCore which TMP will be using in the future. TextCore is a new text parser and layout engine that will replace TMP's own parser and layout. The switch to TextCore will result in increased performance and reduction in memory footprint and open up the door for even more functionality being added.

    Using a combination of static font asset(s) and dynamic font asset(s) as fallback, should reduce the need for these larger atlas textures as the atlas texture of the dynamic font asset should only end up containing the glyphs needing to be displayed.

    Just for clarity, these static font asset(s) should contain the known characters used in the project for a given language or group of languages. These are the characters contains in menus, UI, dialogues, etc but not those characters unknown that will be coming from user input as those will be handled by the dynamic font asset used as fallback. Of course for Latin text, most of the time it makes sense to include all the extended ascii set in the primary since the number of characters is limited but for CJK, having the static primary only contain the known / required characters makes sense and to reply on the dynamic fallbacks to handle the reset.

    It is also important to remember that changes to dynamic font assets in the editor are persistent but not in a build. As such, these dynamic font asset always reset back to their initial state when a new play session starts where if these dynamic font assets were empty (when the build was created) which they should be, it means their atlas size will be 0.

    In terms of loading times / stuff specifically related to the use of dynamic font asset and potentially complex fallback structure, I am aware of inefficiencies in the current system which I will be addressing. These will also yield some very nice additional performance improvements.

    Please continue to provide feedback and when appropriate to forward projects my way as this always makes it easier to figure where some of those improvements can be made. It also allows me to see how things are structured on your end which may not be how I expected it. This allows me to gain more insight on workflows, etc. where my goal remains to provide our users with the best text system for their needs.