Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

HELP!! - Getting an ArgumentException thrown by TextMeshPro UI control

Discussion in 'UGUI & TextMesh Pro' started by Gohloum, Aug 28, 2019.

  1. Gohloum

    Gohloum

    Joined:
    Nov 29, 2018
    Posts:
    12
    Hello,

    I'm not exactly sure when this issue started, but I am getting an error with TextMesh Pro in my current project. I am on 2019.2.0f1 build, running on Mac OS X 10.14.1. The only recent library I have added is unity-ui-extensions, but I don't believe this is the cause since it's been running for several weeks and I can not reproduce the issue in a different project.

    The first thing I noticed is I was using the color tags like so: <#336699>some color text</color> . The text was just coloring to the default color (black in my setting). However the other tags like size were working fine.

    After a lot of testing, replacing UI controls, etc. I deleted the font I had setup and re-created with the Font Asset Creator. After doing this, the color tag started working again. However now I am seeing the error below. Sometimes on different controls when they are rendered. Also it seems if I have several controls on the screen, only 1 throws the error.

    Here is the error:

    ArgumentException: An item with the same key has already been added. Key: 0
    System.Collections.Generic.Dictionary`2[TKey,TValue].TryInsert (TKey key, TValue value, System.Collections.Generic.InsertionBehavior behavior) (at <a8ed250850854b439cedc18931a314fe>:0)
    System.Collections.Generic.Dictionary`2[TKey,TValue].Add (TKey key, TValue value) (at <a8ed250850854b439cedc18931a314fe>:0)
    TMPro.TMP_FontAsset.InitializeDictionaryLookupTables () (at Library/PackageCache/com.unity.textmeshpro@2.0.1/Scripts/Runtime/TMP_FontAsset.cs:589)
    TMPro.TMP_FontAsset.ReadFontAssetDefinition () (at Library/PackageCache/com.unity.textmeshpro@2.0.1/Scripts/Runtime/TMP_FontAsset.cs:607)
    TMPro.TMP_FontAsset.get_characterLookupTable () (at Library/PackageCache/com.unity.textmeshpro@2.0.1/Scripts/Runtime/TMP_FontAsset.cs:139)
    TMPro.TextMeshProUGUI.LoadFontAsset () (at Library/PackageCache/com.unity.textmeshpro@2.0.1/Scripts/Runtime/TMPro_UGUI_Private.cs:539)
    TMPro.TextMeshProUGUI.OnValidate () (at Library/PackageCache/com.unity.textmeshpro@2.0.1/Scripts/Runtime/TMPro_UGUI_Private.cs:302)

    Any ideas on how to resolve this? Maybe there is an old reference to the font I deleted? If so, how would I dump/clean that data?

    --- Edit ---

    I looked at the source and I did see a key value of 0 being passed in through the loop twice. So I am thinking that quite possibly FontFeatureTable's GlyphPairAdjustmentRecords does indeed still has a reference to the deleted font. The one I deleted and then recreated came from the same font source. Here is the code from the TMP_FontAssets class a few lines below 589:

    List<TMP_GlyphPairAdjustmentRecord> glyphPairAdjustmentRecords = m_FontFeatureTable.m_GlyphPairAdjustmentRecords;
    if (glyphPairAdjustmentRecords != null)
    {
    for (int i = 0; i < glyphPairAdjustmentRecords.Count; i++)
    {
    TMP_GlyphPairAdjustmentRecord record = glyphPairAdjustmentRecords;

    long key = new GlyphPairKey(record).key;

    m_FontFeatureTable.m_GlyphPairAdjustmentRecordLookupDictionary.Add(key, record);
    }
    }
     
    Last edited: Aug 28, 2019
  2. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    What version of the TMP package are you using?

    Assuming you are using version 2.0.1, there is an issue related to the conversion of previously existing font assets where converting the kerning data when certain glyphs were synthesized and use the same index for a glyph, would result in this issue.

    I did address this in the next release. In the meantime, you could make the following change which is to simply add a check for existing adjustment pairs.

    if (!m_FontFeatureTable.m_GlyphPairAdjustmentRecordLookupDictionary.ContainsKey(key))
    m_FontFeatureTable.m_GlyphPairAdjustmentRecordLookupDictionary.Add(key, record);

    This won't resolve the issue where synthesized characters / glyphs most likely using incorrect adjustment values but given they were synthesized, they would not have had kerning data from the font file anyway so you should be fine unless you manually added kerning for those.
     
    Twyker_gp likes this.
  3. Gohloum

    Gohloum

    Joined:
    Nov 29, 2018
    Posts:
    12
    Thanks for the info.

    Yes, I am using 2.0.1. I added TextMeshPro about 3 months ago, but did not use the font Creator until 2 weeks ago. So I only recently created my first fonts with it.

    OK, so I'm gonna try your code to see if I can get rid of the error. However, I have a font that we REALLY want to use and it was working, supporting the color tags up until this past weekend. I'm not really sure what changed and I haven't taken in any changes, however Unity did hang up on me and I had to force close. I tried deleting the font SDF asset as well as the font ttf in the package as well and reloading them into the project. It will render colors for some time, but at some point stop. Also, sometimes objects get created as children of my TextMeshPro UI object. They are generally named TMP_SubMenuUI and seem to have duplicate components for the fonts and TextMeshPro/Sprite. This seems to happen when I enter text into the Text box in the editor and hit enter.

    Another thing I noticed is if I use sprite tags nested in size tags with text between the sprites, the second sprite renders a different sprite and it looks to be lower pixel resolution.

    Example: <size=60><sprite=1>some text is here<sprite=1></size>

    If I omit 'some text is here' from the above line, the sprites look fine. But the minute I type any character inbetween, the second sprite immediately renders incorrectly.

    If need be, I can shoot a screen cast of some of these issues and send you a link.
     
  4. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    If you don't have a bunch of pre-exiting font assets created, re-creating the one that you Really want to use should be trivial and work correctly. I would also suggest creating the font asset from the Context Menu create option. This will create a dynamic font asset which can then be switched to static later.

    Please do provide me with some screenshot or video of the other issues and steps for me to reproduce those.
     
  5. Gohloum

    Gohloum

    Joined:
    Nov 29, 2018
    Posts:
    12
    So that code snippet fixed the error in the console, Kudos!

    Yes, I will shoot a screencast and get you a link. It will likely be sometime tomorrow, but I want to walk through the different steps so it makes sense and is easy to follow.
     
    Stephan_B likes this.
  6. Gohloum

    Gohloum

    Joined:
    Nov 29, 2018
    Posts:
    12
    Hi Stephen_B, I sent you a video link in a private conversation.
     
  7. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    I saw it yesterday and thank you. I'll take a look later today.
     
  8. zackblack

    zackblack

    Joined:
    May 17, 2015
    Posts:
    73
    I am getting this same error. When I add the line of code that checks for the key presence it just gets overwritten the next time I open the project. How do I make this stay/work?
     
  9. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    Code changes need to be made in the Global package cache. Making the change in the local package cache in the Library folder will result in those changes getting overwritten.
     
  10. zackblack

    zackblack

    Joined:
    May 17, 2015
    Posts:
    73
    Thank you. I was able to make the change and it fixed the issue. Looks like I'm using TMP 2.0.1. When will the update be available with a proper fix?
     
  11. Twyker_gp

    Twyker_gp

    Joined:
    Dec 4, 2018
    Posts:
    29
    Omg @Stephan_B thank you for the hotfix, this was so annoying.
    EDIT: Okay, I spoke too soon, with this fix the compile times are getting super high. This is even more annoying than the bug itself.
     
    Last edited: Oct 3, 2019
  12. aklgupta

    aklgupta

    Joined:
    Jun 9, 2014
    Posts:
    29
    Had the same issue: Had TMPro for quite some time, but created my first, several font assets yesterday. I had updated my Unity version a few times, from some 2019.x to 2019.2.
    Read a few thread.
    This is what I did, in order:
    • Re imported TMPro (By changin to a preview version, and then back to 2.0.1)
    • Edited "2.0.1/Scripts/Runtime/TMP_FontAsset.cs". Added the condition suggested by @Stephan_B
    • Recreated the Font asset that comes with TMPro "LiberationSans SDF" and "LiberationSans SDF - Fallback", as they were being used as fallback for many of the fonts I imported yesterday.
    • Re-enabled all the TMP-Text GOs and the TMPro component in them if they were disabled.
    • Voila. Everything seems to be working now.
    Thanks @Stephan_B.
    Though, I have gotten to test the claim in the above comment. I just hope that's not true.
     
  13. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    The latest 2.1.0-preview.2 should work well for everyone.

    There is the potential of a shader related issue due to added support for Cull Mode in the material inspector which requires updating the shaders by importing the updated TMP Essential Resources (everything or just the shaders folder). See the following post about a minor code change which will be included in preview.3 which I am hoping to have available within the next 10 days.

    P.S. I am considering relocating the shaders inside the TMP package. This would make it easier to handle shader changes and additions in the future.
     
    aklgupta likes this.
  14. aklgupta

    aklgupta

    Joined:
    Jun 9, 2014
    Posts:
    29
    Thanks @Stephan_B. Any ETA when the 2.1.0 will be out of preview?
    Also, I am making a custom shader t be used with TMP, and I have read that you need to place custom shaders in the same directory as other TMP shaders, is that restriction true?

    And in context to that, what are these path changes you speak of?
     
  15. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    I am planning / will be releasing at least one more preview for 1.5.0 and 2.1.0. In order to ensure these releases are as stable as possible, it is important to get as many users testing these previews as possible.

    The custom shader can be placed in other locations but you will need to revise the #include to make sure their path correctly points to the TMP .cginc files.
     
    aklgupta likes this.
  16. aklgupta

    aklgupta

    Joined:
    Jun 9, 2014
    Posts:
    29
    That's great. Look forward to it.

    Oh, I see. That's good to know. I hated having to put the custom shaders in the package directory.
     
  17. ThisIsDangerous

    ThisIsDangerous

    Joined:
    Dec 3, 2018
    Posts:
    39
    2.1.0-preview.2 still makes zillions of empty Glyph Adjustment Pairs when 'Get Kerning Pairs' is on effectively making the .asset file many megabytes long (12.8 megabytes in my case).

    Generation is done without overwriting or anything, that is, old font asset deleted, new created.

     
  18. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    See the following post and solution.
     
    aklgupta and ThisIsDangerous like this.
  19. aklgupta

    aklgupta

    Joined:
    Jun 9, 2014
    Posts:
    29
    Thanks. I didn't even realize there was this issue.
    That fixed it.