简体   繁体   English

从 c# (unity) 中的文本文件读取时换行不起作用

[英]Line Wrapping not working when reading from text file in c# (unity)

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. _text 变量是字符串。 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). textGameObjects.text 是空游戏对象的文本字段,如果 _text 等于示例“123\n123”并且我运行脚本 - 文本框中的行打印就像在 _text:“123\n123”(没有换行)中一样。 _text is read from the text file via StramReader. _text 通过 StramReader 从文本文件中读取。

I tried using \r\n instead of \n, but it didn't work.我尝试使用 \r\n 而不是 \n,但它没有用。

Change your text file from:更改您的文本文件:

123\n123 123\n123

to:到:

123 123
123 123

Your first instance is a file with 8 characters.您的第一个实例是一个包含 8 个字符的文件。 "\n" by itself just means the character '\' and the character 'n'. "\n" 本身仅表示字符 '\' 和字符 '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.在 IDE 中,它将通过推断您指的是特殊转义字符并将其替换为表示的单个字符来帮助您。 But in your case above, you're simply printing out all 8 characters in your initial string.但在你上面的例子中,你只是打印出初始字符串中的所有 8 个字符。

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).此处显示的第二个实例有一个回车符,它将作为单个字符读取,发送到 ui 文本组件,如果元素支持,则显示为“换行”(换行和回车是两个不同的概念,但我们暂时搁置这一点)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM