Search Unity

TMP and Newline

Discussion in 'UGUI & TextMesh Pro' started by Lo-renzo, Jun 1, 2019.

  1. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,514
    How does TMP treat newlines vs. System.Environment.NewLine? What is recommended usage?

    In code, manually declaring strings with "\n" always seems to work (w/ ParseEscape + RichText on). For example:
    Code (CSharp):
    1.  
    2. string s = "This is a line\nAnother line\nDouble linebreaks\n\n";
    3. myTMP.text = s;
    4.  
    However, I had an issue where importing strings from a .CSV with newlines would fail.
    Code (CSV):
    1. (CSV entries),
    2. Here's an entry,
    3. This is a line\nAnother line\nDouble linebreaks\n\n,
    4. Something else,
    5. (more CSV entries),
    6.  
    For the middle entry, this gives me the same thing as before.


    However, TextMeshProUGUI component displayed this as if I had escaped all the "\n"s or used <noparse>. What's most strange is that if I then manually unchecked+re-checked the checkbox for RichText or Parse Escape on the component in Play mode, it would fix itself and correctly parse the strings into linebreaks, but this would stop working again after reloading.

    I found that I could fix this by manually replacing newline with the environment specific newline when parsing the .CSV:
    Code (CSharp):
    1. betweenCommas.Replace("\\n", System.Environment.NewLine)
    What was going on with that error?
     
    Last edited: Jun 1, 2019
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Escape characters in most file formats like .csv, .html, .xml, .json will all have these characters escaped where they end up as string literals. In all these cases, you will need to use string.Replace. See the following post / thread as well as this one still on the first page here.

    The Parse Escape character option in the Extra Settings only applies to text typed in the Text Input Box and not when using the text component properties.