Search Unity

Resolved ArgumentException on v2.1.0 / Unity 2019.4.4f1 - Identified Bug

Discussion in 'UGUI & TextMesh Pro' started by Jazza4Lyf, Jul 20, 2020.

  1. Jazza4Lyf

    Jazza4Lyf

    Joined:
    Aug 13, 2012
    Posts:
    9
    I'm creating a card game in Unity and trying to use TextMesh Pro with the font Baloo. I have created a regular version plus 4 others with an outline of different colours used to highlight important information via <font>. Using these in the editor, there seems to be no issue, however the first time I set text at runtime that includes <font="Baloo-O[colour]">, I get the following errors:
    Code (CSharp):
    1. ArgumentException: An item with the same key has already been added. Key: -324971240
    2. System.Collections.Generic.Dictionary`2[TKey,TValue].TryInsert (TKey key, TValue value, System.Collections.Generic.InsertionBehavior behavior) (at <fb001e01371b4adca20013e0ac763896>:0)
    3. System.Collections.Generic.Dictionary`2[TKey,TValue].Add (TKey key, TValue value) (at <fb001e01371b4adca20013e0ac763896>:0)
    4. TMPro.MaterialReferenceManager.AddFontAssetInternal (TMPro.TMP_FontAsset fontAsset) (at Library/PackageCache/com.unity.textmeshpro@2.1.0/Scripts/Runtime/MaterialReferenceManager.cs:56)
    5. TMPro.MaterialReferenceManager.AddFontAsset (TMPro.TMP_FontAsset fontAsset) (at Library/PackageCache/com.unity.textmeshpro@2.1.0/Scripts/Runtime/MaterialReferenceManager.cs:41)
    6. TMPro.TMP_Text.ValidateHtmlTag (TMPro.TMP_Text+UnicodeChar[] chars, System.Int32 startIndex, System.Int32& endIndex) (at Library/PackageCache/com.unity.textmeshpro@2.1.0/Scripts/Runtime/TMP_Text.cs:8665)
    7. TMPro.TextMeshPro.SetArraySizes (TMPro.TMP_Text+UnicodeChar[] unicodeChars) (at Library/PackageCache/com.unity.textmeshpro@2.1.0/Scripts/Runtime/TMPro_Private.cs:1051)
    8. TMPro.TMP_Text.ParseInputText () (at Library/PackageCache/com.unity.textmeshpro@2.1.0/Scripts/Runtime/TMP_Text.cs:1893)
    9. TMPro.TextMeshPro.OnPreRenderObject () (at Library/PackageCache/com.unity.textmeshpro@2.1.0/Scripts/Runtime/TMPro_Private.cs:1521)
    10. TMPro.TextMeshPro.Rebuild (UnityEngine.UI.CanvasUpdate update) (at Library/PackageCache/com.unity.textmeshpro@2.1.0/Scripts/Runtime/TextMeshPro.cs:274)
    11. TMPro.TMP_UpdateManager.DoRebuilds () (at Library/PackageCache/com.unity.textmeshpro@2.1.0/Scripts/Runtime/TMP_UpdateManager.cs:171)
    12. UnityEngine.Canvas.SendWillRenderCanvases () (at <4af5bea1b7b2442494fd73f6f9b3d7f4>:0)
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. TMPro.TextMeshPro.GenerateTextMesh () (at Library/PackageCache/com.unity.textmeshpro@2.1.0/Scripts/Runtime/TMPro_Private.cs:1985)
    3. TMPro.TextMeshPro.OnPreRenderObject () (at Library/PackageCache/com.unity.textmeshpro@2.1.0/Scripts/Runtime/TMPro_Private.cs:1553)
    4. TMPro.TextMeshPro.Rebuild (UnityEngine.UI.CanvasUpdate update) (at Library/PackageCache/com.unity.textmeshpro@2.1.0/Scripts/Runtime/TextMeshPro.cs:274)
    5. TMPro.TMP_UpdateManager.DoRebuilds () (at Library/PackageCache/com.unity.textmeshpro@2.1.0/Scripts/Runtime/TMP_UpdateManager.cs:171)
    6. UnityEngine.Canvas.SendWillRenderCanvases () (at <4af5bea1b7b2442494fd73f6f9b3d7f4>:0)
    If I rerun the function that sets the text a second time, it works fine! It's just the first time it's called. I have tried:
    • Deleting the TextMesh Pro folder from the Assets folder, reimporting it and scanning for fonts (which had no results as it was created with the same version 2.1.0)
    • Deleting and recreating the font asset through both the right-click -> Create -> TextMeshPro -> Font Asset and the Font Asset Creator window
    • Using static or dynamic font options (not sure what it would do but worth a try) including fallbacks
    Using the default LiberationSans SDF works fine so I can only assume there must be , but I don't know what else I can try. I'm not classifying this as a bug just yet in case I'm doing something wrong.
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Can you submit a bug report with the project and steps to reproduce for me to look at? This will make it easier to quickly figure out what is going on.
     
  3. Jazza4Lyf

    Jazza4Lyf

    Joined:
    Aug 13, 2012
    Posts:
    9
    Submitted, turns out the problem was worse than I thought as when setting the scene for the bug it wouldn't display anything when there was a TextMeshPro game object that just had the <font="Outlined Font"> on it, even the default Unity UI text wasn't displaying!
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Can you provide me with the case # for the bug report?
     
  5. Jazza4Lyf

    Jazza4Lyf

    Joined:
    Aug 13, 2012
    Posts:
    9
    Sure: 1264596_i8bopsqlgbuladh6
     
  6. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Just wanted to let you know that I have identified the source of the issue.

    Thank you for taking the time to submit this bug report as I would not have been able to find this without it.

    The fix will be included in the next release of the TMP package which I will try to have available within the next 5 - 7 days.
     
  7. Jazza4Lyf

    Jazza4Lyf

    Joined:
    Aug 13, 2012
    Posts:
    9
    Ok, so it wasn't something I was doing wrong then :D No problem, glad I could help make it better by breaking it for ya! Thanks for the quick response and fix!
     
  8. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Forgot something important...

    Is there a reason you are using 3 different font assets where the only difference between them is material properties / colors? That is instead of using a single font asset with 3 different Material Presets.

    This would reduce your build size as you only need 1 font asset with different material presets where these all reference the same atlas texture contained in the font asset.
     
  9. Jazza4Lyf

    Jazza4Lyf

    Joined:
    Aug 13, 2012
    Posts:
    9
    The reason I was doing that was because I couldn't find any way of changing the outline colour and density rather than the text colour. If there is a way to do that then that would be amazing as I did see there was an option for the material preset but wasn't too sure how to actually use them. I'll have to check the documentation on that again I think but this was the solution I had just to get the basic concept working first, would have tried to find better methods for it later
     
  10. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Here is a quick summary of the process to switch to using Material Presets.

    (1) Baloo-Regular SDF will continue to be your primary font asset where its default material will remain unmodified.
    (2) Select the Shader used by this primary font asset to the Mobile Distance field.
    (3) Create a Material Presets using the Context Menu of the material inspector as seen below.

    upload_2020-7-22_13-51-44.png

    This will create a new material preset and automatically assign it to your text object. This is not what we want right now but we will address that shortly.

    Rename this Material Preset to add the suffix "- Orange".

    (4) Select the material child of your Baloo-OOrange font asset as seen below

    upload_2020-7-22_13-54-12.png

    with that material selected, use the context menu to copy the material properties.

    upload_2020-7-22_13-57-18.png

    Select the Material Preset we previously created in step 3 and use the context menu Paste Material Properties options.

    You now have a Material Preset for the Baloo-Regular SDF that matches your Orange variant but you just saved on resources since this material preset uses the same font asset and shares the atlas texture.

    Repeat the process for the Blue and Green.

    Next create a new dynamic Fallback font asset that uses the same Generation Settings or the same Sampling Point Size to Padding ratio.

    You should then have the following

    upload_2020-7-22_14-3-52.png

    Which is (1) Primary font asset Baloo-Regular SDF and (1) dynamic fallback for it and 3 material presets.

    Now looking at your text object, let's make sure we assign our Baloo-Regular SDF font asset and select the default material preset. Also notice our 3 new material presets are now visible in the Material Preset drop down list.

    upload_2020-7-22_14-6-22.png

    To use these material presets in the text, you will continue to use the <font> tag but with the material attribute as follows:

    "This has <font="Baloo-Regular SDF" material="Baloo-Regular SDF - Orange">orange, <font="Baloo-Regular SDF" material="Baloo-Regular SDF - Blue">blue and <font="Baloo-Regular SDF" material="Baloo-Regular SDF - Green">green<font="default"> outlined fonts."

    The format is <font="Font Asset name" material="Material Preset Name">.

    Notice I removed the </font> since we can switch between those. If you wanted the first comma to not be orange, you would use </font>. I am also using <font="default"> to switch back after green.

    In short instead of having 8 font assets to manage, you now have 2 with 3 material presets.
     
  11. Jazza4Lyf

    Jazza4Lyf

    Joined:
    Aug 13, 2012
    Posts:
    9
    That is SO much better! Thank you for that, I've gotten it switched over and no errors to be seen :)
     
    Stephan_B likes this.
  12. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Not only did we end up bypassing the initial bug (which I have now fixed) but we also reduced the number of font assets from 8 to 2 resulting in lower memory overhead and build size.

    One of the indirect benefits of submitting a bug report with project is that in the process of resolving the reported issue, I also get to look at the project where I can provide potential suggestion to further optimize things where appropriate.
     
    Jazza4Lyf likes this.