Home > Enterprise >  Line Wrapping not working when reading from text file in c# (unity)
Line Wrapping not working when reading from text file in c# (unity)

Time:12-25

I have this fragment of code.

foreach (var symbol in _text)
{
    dialogueRunning = true;
    audioSource.clip = _soundArray[Random.Range(0, 2)];
    textGameObjects.text  = symbol;
    audioSource.Play();
    yield return new WaitForSeconds(0.05f);
}

_text variable is string. textGameObjects.text is a text field of an empty game object if _text equals for examaple "123\n123" and i run the script - line prints in text box just like in _text: "123\n123" (without line wrapping). _text is read from the text file via StramReader.

I tried using \r\n instead of \n, but it didn't work.

CodePudding user response:

Change your text file from:

123\n123

to:

123
123

Your first instance is a file with 8 characters. "\n" by itself just means the character '\' and the character 'n'. In an IDE, it will help you by inferring that you're referring to a special escape character and replace those with the represented single character. But in your case above, you're simply printing out all 8 characters in your initial string.

The second instance shown here has a carriage return, which will be read as a single character, sent to the ui text component, and displayed as a "line wrap" if the element supports that (line wrap and carriage returning being two different concepts, but we'll let that slide for the moment).

  • Related