Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

TextMesh Pro TMP InputField doesn't correctly display newlines.

Discussion in 'UGUI & TextMesh Pro' started by galloper, Nov 11, 2019.

  1. galloper

    galloper

    Joined:
    Aug 5, 2017
    Posts:
    30
    Issue it this: if I set TMPInputField.text through code to some string which will contain new lines, it shows the string in text area like "text text\n text\n" etc. Then, if I run it in the Unity Editor, it still shows the text with all the "\n"s visible, then I press "auto size" for its text area (still in play mode), the newlines get processed and text shows like it should:
    "text text
    text"

    So I guess the problem is that it doesn't process the text correctly at some point after the text value changes, but it does so when text size is changed while in editor play mode?
     
  2. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    4,857
    The behavior is likely related to how Unity incorrectly handles characters escape sequence in public fields. See the following post and solution.
     
  3. galloper

    galloper

    Joined:
    Aug 5, 2017
    Posts:
    30
    I see, thank you for your reply, however, in my case it doesn't change a thing about the string, the "\n" doesn't get converted into "\\n" for example.
    The text I output into this field comes from Unity Web Request component's download handler in form of a UTF-8 string.
    What is really curious for me is that if I get a web page through it, its code is displayed with all the new lines as it should, but if I get text data that is not a web page, then I get "\n" instead of new lines. Code in both cases is the same and looks like this:

    outputField.text = uwr.downloadHandler.text;

    where the outputField is a TMPInputField component.

    Then again, while I have the string with "\n" and/or any other such symbols displaying inside the said field, still with the editor play mode running the game, if I change anything about this field in inspector - be it setting "auto size" for text, or checking/unchecking RTL Editor there, it "magically" refreshes into displaying the string with all the newlines, symbols etc. Which kinda makes me believe that the string itself is okay, since doing such crude "refresh" on the component does the trick, or am I wrong here? How can I achieve this result without touching "auto size" for example?
     
  4. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    4,857
    The best way for you to isolate / figure out the source of the issue would be to set a break point in the code to inspect the string. This would allow you to see exactly when the \n is escaped to a string literal or \\n.

    It is important to note that some input format like XML, etc. will escape control characters. Typing \n in a public field in the Inspector will also result in the \n being escaped to \\n which is "\" + "n" which is not char(10) or a linefeed.
     
  5. galloper

    galloper

    Joined:
    Aug 5, 2017
    Posts:
    30
    Excuse me, do you mean that in the Unity editor, in Inspector window, when I see the Text property written as: "a\na\na", it's a lie and it really is "a\\na\\na"?
    Let me explain more clearly, try this:
    make a new project in Unity and put a TMP Input Field in it.
    Set it up so you see the text.
    In the Inspector input "a\na\na" string into the Text field (in the inspector!)
    You see how it's printed out in the "Game" window just like "a\na\na"?
    Now, in the Inspector, press Auto Size checkmark, or press "B" for Bold or change ANY other setting that will make component refresh! It all has the same effect.
    See how it changed to
    "a
    a
    a"
    everywhere - in scene, in game???
    What happens here? I believe it has everything to do with the way the component handles it's Text value input.

    Whatever's happening there, I need to be able to trigger this behaviour through code, so far no luck.
     
    Last edited: Nov 12, 2019
  6. galloper

    galloper

    Joined:
    Aug 5, 2017
    Posts:
    30
    Let me rephrase once again: if I input a string "a\na" into the Text attribute of TMP Input Field component it the inspector, I will see it exactly as "a\na" in the scene and game view of the editor. Then, if I press any button in the inspector - "B" for bold text or another, the text will reformat itself into
    "a
    a"
    and it is it's normal, default behaviour, nothing to look at here, move along people? Why the check for escape chars only happens after I press something else, not "on the fly"? Why it doesn't happen at execution on text change either?
     
  7. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    4,857
    Just to make sure I understand, you are typing "a\na" in the inspector text field of the TMP Input Field (the parent object). When doing so the text appears literal as typed. However, when you select the child text component of the TMP Input Field and select Bold or another style, the text is revised to a + LF + a. Is that correct?
     
  8. galloper

    galloper

    Joined:
    Aug 5, 2017
    Posts:
    30
    Yes, just like that.
    I actually recorded it on video:

    And so I'd also love to be able to trigger such behaviour through code :)
     
    Last edited: Nov 18, 2019
  9. galloper

    galloper

    Joined:
    Aug 5, 2017
    Posts:
    30
    Been a while, any luck tracking down this issue?
     
unityunity