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 Bug: TMPro will not parse "\n" when string is changed via UI event

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

  1. MrLucid72

    MrLucid72

    Joined:
    Jan 12, 2016
    Posts:
    962
    1. I have txt and btn.
    2. btn will use the UI's OnClick event to change txt.text's string: "Hello\nWorld"
    3. Ensure "parse escape characters" is checked (and rich text too, just in case).
    4. Start >> Click btn >> The result is the literal of @"Hello\nWorld"
    5. If I uncheck "parse escape chars" and check it again (while playing), the txt str will jump back to what's expected.
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Is this string in code or contained in a public string field in the inspector?

    Most of the time, the string being provided to the text object gets escaped by Unity. This can easily be verified stepping through the code an inspecting the string where you will end up with a "\" and "n" instead of char(10).

    The Parse Escape Characters only affects text input in the Text Input Box and not when the text comes from a string.

    Let me know the result of the above string inspection.

    P.S. I am at the Airport to fly over to Europe, so most likely I won't be able to reply until I get there as Internet Connections on flight is usually horrible.
     
  3. MrLucid72

    MrLucid72

    Joined:
    Jan 12, 2016
    Posts:
    962
    public string in the inspector -- it's just a Unity UI event. In the Button's OnClick event, I drag a TMPro txt and change it's string~
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    The Unity is escaping the text. You will need to account for this an use string.replace to remove the additional / inserted "\". See the following thread.

    P.S. As per my previous comments about this in other threads. This automatic escaping of characters in text fields in the editor is very confusing as having a string in code with "\n" is parsed as linefeed whereas the same text in a public string edited in a text field ends up being "\\n" . This is something I have discussed with the Editor team.
     
    MrLucid72 likes this.