Search Unity

TextMesh Pro Newline \n not parsing correctly

Discussion in 'UGUI & TextMesh Pro' started by TSabos, May 12, 2017.

  1. TSabos

    TSabos

    Joined:
    Mar 8, 2015
    Posts:
    94
    I saw this question posed and not fully answered on the TMP forums and wanted to try to explain a bit to see if it's a bug or just working as intended.

    A google spreadsheet with \n imported to a public string variable is test 1.
    A simple hand typed \n into the public string variable test 2.

    Under both conditions a breakpoint showed \\n as the character and is parsed as \n in TMP text. If you click into the inspector and simply hit the space bar to "add" to the text on the end it instantly parses correctly as a newline.

    I have to use .Replace("\\n", "\n") in order to correct the problem.

    Tried messing with parse escape characters on and off and neither appeared to alter the results.

    Is there a way to type \n into a string variable and have it parsed correctly?

    Thanks.
     
    Thund3rDev and tillmonkthered like this.
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    In the Inspector, \n will always be displayed as \n unless you have "Parse Escape Characters" enabled in the Extra Settings panel. In such case it will be processed as a linefeed.

    In a string, C# will convert "\n" into a line feed which will then be handled as such by TMP.

    In a string, "\\n" tells C# to process the string as a literal and thus is becomes / displays as "\" + "n" or two separate characters.

    Most of the time when you use some text editor or xml or excel / google spreadsheet, etc. the "\n" gets escaped to "\\n" which then results in the "\" + "\n" instead of a linefeed.

    BTW: If you were to create a simple script that iterates over the string from Google spreadsheet, you would see how the "\n" is escaped into "\\n" or literal and not char(10) / linefeed.

    P.S. In the TMP Settings file, the default behavior of newly created objects and whether or not Parse Escape Characters will be enabled by default can be set.
     
    Last edited: May 12, 2017
    naviln and theolagendijk like this.
  3. TSabos

    TSabos

    Joined:
    Mar 8, 2015
    Posts:
    94
    Apparently unity serializer also stores anything typed into a public string variable as \\n as well? I think this is the confusion. I do pull data from Google spreadsheets, but even if I manually type \n into a public string in Unity's inspector it gets parsed as \\n which is quite annoying. No way around this I assume... we just have to .Replace("\\n", "\n") every single text we know will need to have linefeeds in?

    I have changed my import parsing to replace everything and it works great thanks... just didn't think of that one.

    Most likely no one would type \n into unity strings in the inspector, so I'm moving on for now ;)
     
    Last edited: May 12, 2017
    nguyenlamlll and Thund3rDev like this.
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    When I ran into that the first I too was surprised by the behavior. I think given that you can press TAB to add a Tab or ENTER / RETURN to add a Linefeed in a public field or text box in the editor, this is why these are getting double escaped. Whereas in a string, your only options is to use \t and \n for those.

    As I work on the Integrated version of TMP, I am certainly open to revisiting this to see if we should continue to handle as it is now.
     
  5. SpaceManDan

    SpaceManDan

    Joined:
    Aug 3, 2013
    Posts:
    15
    For those that are having trouble with this it may be important to point out that .Replace is a return value. If you are trying to do the following you'll not see any change.

    myString.Replace("\\n", "\n");

    You'll need to set your string to the value like so

    myString = myString.Replace("\\n", "\n");

    If you have more than one you'd like to replace you can stack the function like so

    myString = myString.Replace("\\n", "\n").Replace("\\t", "\t");
    or
    myString = myString.Replace("\\n", "\n").Replace("\\t", "\t").Replace("nexttexttoreplace", "next text to replace");

    Hope this helps somebody.
     
  6. csiro_rmc3

    csiro_rmc3

    Joined:
    Jun 8, 2014
    Posts:
    14
    Another option could be to use the xml tag <br> in the string instead.

    Gets expanded automatically into a newline by TMP whether it is passed from a script or hand-entered into the inspector (as with other formatting tags like <b> and <smallcaps>).
     
  7. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    The <br> tag is already supported in TMP.

    But the real solution is for me to change the behavior in the editor so that \n and other control sequences are not escape or at least that we provide control over whether or not the particular text field should escape them.
     
    assertor likes this.
  8. BeregAlto

    BeregAlto

    Joined:
    Aug 5, 2018
    Posts:
    14
    I fixed that with replacing \n with \u000a (UTF-16)
     
    danle2584 and Jericol1 like this.
  9. sayginkarahan

    sayginkarahan

    Joined:
    Jan 23, 2015
    Posts:
    49
    <br> is working stable.
     
  10. Gobstar

    Gobstar

    Joined:
    Nov 12, 2015
    Posts:
    7
    You sir are a hero.
     
    Canley and WBI-LLC like this.
  11. LunaTrap

    LunaTrap

    Joined:
    Apr 10, 2015
    Posts:
    120
    It helped me a LOT! thanks a lot!! I wish you the best from the future of 2022 and kisses!
     
  12. Alex_the_Coder

    Alex_the_Coder

    Joined:
    Dec 3, 2018
    Posts:
    21
    Another way to solve this, at least for inspector strings, is to mark the string as [TextArea] in the respective script,
    e.g.

    [TextArea]
    public string myString;

    then in Inspector, you can write it like that:
    This is a string
    with two lines
     
    dman8723 and tagnadia1992 like this.
  13. Kruti_24

    Kruti_24

    Joined:
    Jun 7, 2022
    Posts:
    1
    dman8723 and zero4444 like this.
  14. dazarolar

    dazarolar

    Joined:
    May 8, 2020
    Posts:
    2
    <br> it's working fine.
     
    zero4444 likes this.