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?
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.
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
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.
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”);
Another option could be to use the xml tag
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 and ).
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.