Search Unity

Inputing a line break in a text field for UI?

Discussion in 'UGUI & TextMesh Pro' started by Velo222, Apr 16, 2015.

  1. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    Hello all,

    I'm working on making tooltip-like functionality for my UI and running into a simple, yet strangely hard problem. I have a text "string" type that I made public in one of my scripts. And whenever a player mouses over one of my UI buttons, it displays a panel with the text that was "input" into the "string" on my script.

    So my problem is that I cannot figure out how to start a new text line within that string of text? I've googled it and only found a few options. I've tried /n, '/n', "/n", \n..... as well as <br> with rich text checked. I havn't come across any other commands really. Maybe I'm missing something?

    I should note that I am successfully using /n in my literal script code, and it works. But not when I'm publically entering text into a text field (for example in the inspector directly).

    How can I make a line break in the text field using Unity's new GUI (uGUI) system?
     
  2. Claytonious

    Claytonious

    Joined:
    Feb 16, 2009
    Posts:
    904
    I don't understand the problem. Are you just doing this:

     
    PhoenixRising1 likes this.
  3. Claytonious

    Claytonious

    Joined:
    Feb 16, 2009
    Posts:
    904
    That's just using the enter key. But notice that I've set "vertical overflow" to "overflow" instead of truncate. Maybe you're just accidentally truncating your extra lines?
     
  4. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    Well, I'm not actually entering text into the text field script though. So, I actually can't press enter in my field. It's a public "String" variable that is exposed in the inspector. But the actual field line is only one line (like an infinity line).

    Here is a picture of my inspector if it helps any:




    Then I populate the Text's script "text" field (as shown in your picture) with the text entered in my string text field as shown above.
     
    Last edited: Apr 16, 2015
    AlejMC and UnityUser00 like this.
  5. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    After more googling I havn't found an entire solution, but I did find an easy workaround (at least I think).

    According to a Unity Answers solution, apparently you can just do something like this:
    Code (JavaScript):
    1. //Populate the uGUI Text script's text field with a custom string variable, but tell it to replace /n with your own provided character
    2. tooltipTextFieldOnPanel.text = myCustomString.Replace("<br>", "\n");
    Never knew about the "Replace()" function until now. It's working for me, so I'm considering this solved unless I run into any side effects from it. :)

    Technically I think \n SHOULD work. But it's not. Not sure if that's a bug, but the work-around solution is really easy. So I'm very happy about that.
     
  6. Claytonious

    Claytonious

    Joined:
    Feb 16, 2009
    Posts:
    904
    Were you putting the "\n" into the inspector as the value for your "Tooltip Text to Show" variable? If so, then what you were actually getting into your variable would have been "\\n". For example, if you typed "Line1\nLine2" into the inspector, then at runtime the value in your variable would be "Line1\\nLine2" (because users have to be able to enter backslash characters into fields in the inspector as normal characters).

    So you could just as easily use .Replace("\\n","\n"), just as you're using .Replace() on "<br>". There's nothing magic about the string you're replacing. You could use your own convention. Tell your users that "bunnyrabbit" will be a line break, as long as you do .Replace("bunnyrabbit","\n"). Then if a user entered "Line1bunnryrabbitLine2" into your inspector field, you would end up with "Line1\nLine2" at runtime as expected.

    You're mixing up the idea of escaped characters in c# string literals with the normal use of backslashes in strings. Those backslashes are only interpreted by something that chooses to interpret them (such as the C# compiler when it examines string literals).
     
  7. sotirosn

    sotirosn

    Joined:
    Jan 5, 2013
    Posts:
    24
    Instead of writing your own replace method you can use System.Text.RegularExpressions.Regex.Unescape to convert all \a, \b, \e, \n, \r, \f, \t, and \v to their utf-8 control characters.

    @Claytonious
    What you are saying about actually putting in a "\\n" is not quite accurate. It is simply that Unity's string field is not converting "\n" into a new line character and instead leaving it actually as a '\' followed by an 'n'. Where as the c# compiler unescapes all occurrences of \n in string literals by replacing them with newline character at compile time. I actually wish Unity respected the backslash and unescaped all string values in the inspector fields by default during deserialization. Then if you wanted a backslash you would have to type \\.
     
  8. Krishx007

    Krishx007

    Joined:
    Jul 15, 2014
    Posts:
    14
    .Replace("\\n","\n") worked for me!
    Thank you so much..!!
     
    honor0102 and Alekxss like this.
  9. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    If you want to avoid post-processing your text in a script, you can add the [TextArea] attribute to your public string variable:
    Code (csharp):
    1. [TextArea]
    2. public string tooltipTextToShow;
    Then you can press Enter in the inspector and it'll add a newline to the string.
     
  10. AlbeyAl

    AlbeyAl

    Joined:
    Mar 26, 2015
    Posts:
    1
    I know this is an old thread, but I thought I'd share a simple solution. When using line escapes, you have to enclose the line escape sequence with single quotes, not double quotes. Example: '\n', not "\n".
     
    Calamity11 likes this.
  11. Emery-Monzerol

    Emery-Monzerol

    Joined:
    Nov 1, 2012
    Posts:
    20
    This was what I was looking for, thank you! :)
     
    fabiopicchi likes this.
  12. levi9687

    levi9687

    Joined:
    Dec 17, 2017
    Posts:
    4
    Text.text = "your text with a new line starting here \n and this is your new line";
     
  13. Tonio1308

    Tonio1308

    Joined:
    Sep 22, 2013
    Posts:
    7
    I agree. That's the correct analysis of the problem and therefore, the correct solution.
     
  14. XsettingsX

    XsettingsX

    Joined:
    Mar 3, 2019
    Posts:
    1
    well just so you know they actually have made it now so that you can just use /n to split the line, rip to all these past people who had to struggle lol
     
  15. Eandrushenko

    Eandrushenko

    Joined:
    Jul 6, 2020
    Posts:
    1
    You're an absolute legend. This is by far the best and easiest solution.
     
  16. Max_Bol

    Max_Bol

    Joined:
    May 12, 2014
    Posts:
    168
    The problem is still there and /n doesn't work if the string from which the text is being added/modified from isn't compiled at the right time. There are currently only 3 ways of handling line break (or any text formatting) when transferring a string onto a <text> component in a project.

    A) Transferring the string from another <Text> based component,
    B) Using the [TextArea] extension above the public string (array, list or single),
    C) Using a string within the code directly in the script's prior to compiling. (or loading the string from a previous scene).

    The issue comes from how the compiling order and string content is managed in the engine.
    • If you put the string with text formatting in a code as-is (like a dictionary for translation), it gets compiled after the string is being loaded.
    • If you use either a [TextArea] extension or copy from another <Text> based component, the content gets compiled with the proper formatting.
    • If you use a public exposed string variable and change it through the inspector, the text content is being compiled as empty, then gets filled up after the text compilation is completed which result in the engine skipping the text formatting implied from the parts like /n or whatever in the string.

    The content of the inspector (like public variable) are stored in the scene's file and not in the compiled codes (scripts) files. This results in a disparity between when the codes, in scripts, is being compiled versus when the variables (including strings) in the scene codes are being compiled. After all, the text you entered in the inspector are stored in the scene files and not in the codes files. The text formatting module only look at the content once unless specified otherwise and, since public variable are empty when loaded prior to the scene being started (prior to the Awake() method), it doesn't format the string since they are "empty" at that moment. The [TextArea] forces the text formatting module to look at the string content a 2nd time just before the scene starts. It's same principle when it comes to the UI's <Text> components in the scene.
     
  17. MaxGaming64

    MaxGaming64

    Joined:
    Feb 14, 2021
    Posts:
    1
    THANK YOU SO MUCH! Finally I could create a newline in the inspector =D
     
    TonyLi likes this.
  18. Rachan

    Rachan

    Joined:
    Dec 3, 2012
    Posts:
    781

    Thank you very much!,

    it need to replace from <br> to \n later,
    we can't use \n directly in text box!
     
  19. Deleted User

    Deleted User

    Guest

    Well, \n works in Text.text. :)
    Code (CSharp):
    1. [SerializeField]private Text outputText;
    2.  
    3. void Start()
    4. {
    5.     outputText.text = "Bullets = " + myInventoryContent.bullets + "\n" + "Fuel = " + myOtherInventoryContent.fuel;
    6. }
    Capture d’écran_2023-10-29_12-28-04.jpg