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. Dismiss Notice

Tab escape character (\t) set from script doesn't make a tab?

Discussion in 'UGUI & TextMesh Pro' started by GuMMaN, Apr 19, 2020.

  1. GuMMaN

    GuMMaN

    Joined:
    Jan 21, 2017
    Posts:
    5
    Hi

    I have a TextMeshPro with Parse Escape Characters set to true, and I can see that the \t characters are rendered as tabs when I use the editor in Unity. I need to set the text from a script, but then it simply shows as text - "\t". Strangely, lineshift (\n) still works as it should even when set from the script.

    Setting the text is done something like this:

    GetComponent<TextMeshPro>().text = "text on the left \t\t text on the right";


    Any ideas?
     
  2. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    The Parse Escape Characters option applies to the Text Input Box in the Inspector.

    I does not affect text input via a string and public text field.

    As you pointed out, in a string, such as "text on the left \t text on the right" works as expected.

    However, in a public field in the Inspector, the "\t" becomes visible and this is because Unity escapes control characters in all public fields in Inspectors which in turn results in "\\t" where we end up with "\" + "t".

    To account for control characters being escaped in public fields you will need to use string.replace.

    There are several threads about this topic on the forum here. See the following thread which includes links to other threads as well.
     
  3. GuMMaN

    GuMMaN

    Joined:
    Jan 21, 2017
    Posts:
    5
    Aha! Thank you. \n wouldn't be escaped, but \t would, so myString = myString.Replace("\\t", "\t"); did the trick!