Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

TextMesh Pro Modifying TMPro Text Through Code Does Not Apply Unicode Fallback

Discussion in 'UGUI & TextMesh Pro' started by Jmangles, Nov 27, 2017.

  1. Jmangles

    Jmangles

    Joined:
    Aug 1, 2015
    Posts:
    53
    I spent an evening creating an emoji setup but when I finished I noticed that if in my game's chat messages players used an emoji's name such as :thinking_face: it would grab the correct unicode value from my dictionary but just end up writing \U0001f914 into chat instead of replacing it with the emoji in the sprite sheet.

    If I click pause while the chat message is up and I click anything in the inspector for the TextMeshProUGUI object, it immediately replaces the unicode value with the emoji. I thought maybe I was just missing some sort of parsing feature by directly modifying the .text member of the object in code so I used SetText instead and still no luck.

    After that I tried SetAllDirty in hopes it would trigger an update and even tried triggering it a few frames later to see if that would have the same effect as clicking anything in the inspector (even "enable rtl editor" would cause the emoji to properly parse).

    I'm assuming this is a bug of sorts or I'm missing some sort of "refresh text object" function that would replace unicode values written out with their actual characters.
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Just to make sure I understand correctly. You are setting the text property of the text using something like

    Code (csharp):
    1. TMP_Text textComponent = GetComponent<TMP_Text>();
    2. textComponent.text = "X\U0001F600X";
    and instead of the correct sprite / emoji showing up, you get the unicode string instead?

    Can you provide a simple script that I could add to a text object to reproduce the behavior you are reporting?
     
  3. Jmangles

    Jmangles

    Joined:
    Aug 1, 2015
    Posts:
    53
    I just decided to use <sprite name=""> instead as I could not get unicode to work.

    Code (CSharp):
    1. public static string GetEmojiUnicode(string emojiName)
    2.     {
    3.         if (!nameToEmojiUnicode.ContainsKey(emojiName))
    4.             return emojiName;
    5.  
    6.         //return "<size=20><sprite name=\"" + nameToEmojiUnicode[emojiName] + "\"></size>";
    7.  
    8.         var unicode = nameToEmojiUnicode[emojiName];
    9.         var zeroCount = 8 - unicode.Length;
    10.  
    11.         var sb = new StringBuilder(@"\U", 10);
    12.         sb.Append('0', zeroCount);
    13.         sb.Append(unicode);
    14.      
    15.         return "<size=20>" + sb.ToString() + "</size>";
    16.     }
    \U on its own gives an invalid escape character error, using \\U instead of the string literal @ does not fix it either.
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Using a string literal or "\\U" results in C# processing the string as a literal instead of converting it to the correct UTF32 character.

    "\U" or "\u" on their own is invalid as C# expects the "\U" to be followed by 4 hex pairs and "\u" by two hex pairs.

    Your dictionary should contain / replace the emoji name by the full unicode which "\U0001F600" and not just the "0001F000".
     
  5. ZacharyOwenCAT

    ZacharyOwenCAT

    Joined:
    May 28, 2019
    Posts:
    1
    This bug is still present in Unity 2021.1 using TextMeshPro 3.0.6.

    Setting a the text of a TextMeshProUGUI component in a script will not properly display unicode characters until the editor is interacted with.

    Is there a work around for this?
     
    dk_niit, FelixK-FG and sdpgames like this.
  6. sdpgames

    sdpgames

    Joined:
    Sep 2, 2014
    Posts:
    17
  7. danielrusnac

    danielrusnac

    Joined:
    Jun 17, 2021
    Posts:
    4
    I have the same problem. No unicode formatting until I change the text in the inspector.

    My setup is a script that displays an
    int
    , with a
    prefix
    and a
    suffix
    indicated in the inspector.

    Code (CSharp):
    1. public class UIDisplayInt : UIDisplay<int>
    2. {
    3.     [SerializeField] private string _prefix;
    4.     [SerializeField] private string _suffix;
    5.     [SerializeField] private TMP_Text _text;
    6.  
    7.     protected override void OnRefresh(int value)
    8.     {
    9.         _text.SetText($"{_prefix}{value}{_suffix}");
    10.     }
    11. }
     
    Last edited: Jan 24, 2022
  8. N0sf3ratu

    N0sf3ratu

    Joined:
    May 14, 2016
    Posts:
    6
    Did anybody find a solution for this? I am passing strings from a TextFile with Unicode included and it always shows the unicode in the TextMeshPro until I interact with it in the editor...
    Is there a method to update the TestMeshPro in Script so that it Refreshes the text content to understand unicode?
    Please help out!
     
  9. rscopic

    rscopic

    Joined:
    Jul 3, 2018
    Posts:
    2
    Same issue here. Looking at the source for TMP_Text, unicode characters are intentionally only handled when the text is changed via the inspector. (I.e., only when the textmesh's internal m_inputSource enum is set to TextInputBox.) Attempting to set the text through code changes m_inputSource to TextString which bypasses unicode handling.

    A way around this is to manually unescape the unicode characters before assigning them to the textmesh:
    Code (CSharp):
    1. string content = @"\uF4F5";
    2. content = Regex.Unescape(content);
    3. textMesh.text = content;
    A downside to this approach is your now unescaped unicode characters may be invisible in the inspector text box, but they'll render fine in-game. A formal fix to this problem would still be very appreciated!
     
    Last edited: Jul 24, 2022
  10. unity_0-vdYj4ZGv7CJA

    unity_0-vdYj4ZGv7CJA

    Joined:
    May 17, 2023
    Posts:
    1
    Omg. This is unbelievable. I just spent 2 days troubleshooting why Font Awesome icons were not working through script, while working fine in the editor.

    @rscopic, you are my hero. ❤️

    How could this possibly not be fixed by now? @Stephan_B, please look in to it.

    Search tags for my fellow googlers:
    "TMP_SubMeshUI using wrong font after text update through script", "TextMeshPro reverting to default font"
     
  11. Stephan doesn't work on TMP anymore. You should submit a bug report instead.
     
    unity_0-vdYj4ZGv7CJA likes this.
  12. haywirephoenix

    haywirephoenix

    Joined:
    May 17, 2017
    Posts:
    101
    Thank f for that <3 works for me in (latest TMPro 3.0.6, unity 2022.3.8f1)

    Issue still hasn't been officially resolved in 2023
     
  13. AjdiNNNN

    AjdiNNNN

    Joined:
    Jan 14, 2019
    Posts:
    22
    I have same issue but partly... If I apply some FA icons trought script they work but if I set them to \uf06a or \uf05a they apply as charachters?
    Edit: It seems it work if you go tmp.text = "\uxxxx" or variable assign but only if variable is assigned on start and hardcoded u can't edit variable in editor, if you do it displays chars.
     
    Last edited: Dec 5, 2023