Search Unity

4.6b20 how to add a line break in a text widget by script

Discussion in 'UGUI & TextMesh Pro' started by korbain, Oct 23, 2014.

  1. korbain

    korbain

    Joined:
    Sep 11, 2013
    Posts:
    5
    Hello,

    I read my localized text from a CSV file. Each text entry, no matter how long, is a single line.

    I then set the text on a Text component by script.

    My CSV line is: BIO_TEXT_ID, Age: 27 Gender: male Place of Birth: Austin

    When the menu is displayed, I want my text widget to display the text on separate lines:
    Age: 27
    Gender: male
    Place of Birth: Austin

    I tried doing: BIO_TEXT_ID, Age: 27\nGender: male\nPlace of Birth: Austin

    But the \n is displayed with no line break.

    Any idea how to do this?

    Thanks!
     
  2. korbain

    korbain

    Joined:
    Sep 11, 2013
    Posts:
    5
    After some experimentation, I found a solution:

    string characterTextString = LocalizationManager.Instance.getText(BIO_TEXT_ID);
    characterTextString = characterTextString.Replace("\\n", System.Environment.NewLine );
    characterText.text = characterTextString;

    The 2 slashes in "\\n" ARE important.
    The characterText will now display correctly the line breaks :)
     
    racSG and Baroni like this.