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 are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here: unity.com/legal/terms-of-service.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice
  4. Join us on Thursday, September 29, for a day with Unity's SRP teams here on the forum or on Reddit, and discuss topics around URP, HDRP, and the Scriptable Render Pipeline in general.
    Dismiss Notice

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:
    92
    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

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,525
    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:
    92
    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
    Thund3rDev likes this.
  4. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,525
    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. cjcatrix

    cjcatrix

    Joined:
    Jun 8, 2014
    Posts:
    13
    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

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,525
    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:
    36
    <br> is working stable.
     
    WBI-LLC, IanOnWork and crafTDev like this.
  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:
    119
    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:
    11
    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
     
  13. Kruti_24

    Kruti_24

    Joined:
    Jun 7, 2022
    Posts:
    1
unityunity