Unity EditorGUIUtility.systemCopyBuffer,StringBuilder附加包含换行符的字符串

时间:2017-01-15 09:31:39

标签: c# string unity5 stringbuilder

我想使用StringBuilder合并几个字符串,然后将它们提供给UnityEngine中的EditorGUIUtility.systemCopyBuffer。其中一个字符串本身包含换行符,但是当我粘贴EditorGUIUtility.systemCopyBuffer的内容时它们不存在。我不能自己添加换行符,因为我不知道有多少或哪里。

// This string is in my real code unknown to me.
// I also don't know the format of the newline characters.
string description = "A long description\nwith multiple lines.";

var builder = new StringBuilder();
builder.Append("Name: ");
builder.AppendLine(someObject.Name);
builder.Append("Description: ");
builder.AppendLine(description);
Debug.Log(builder.ToString());
EditorGUIUtility.systemCopyBuffer = builder.ToString();

Unity Console中Debug.Log打印的预期输出为:

Name: A name
Description: A long description
with multiple lines.

所以StringBuilder可以处理它。但是,当我将文字粘贴到例如记事本,它看起来像这样:

Name: A name
Description: A long descriptionwith multiple lines.

因此,系统复制缓冲区可以处理StringBuilder添加的换行符,但不能处理原始字符串中的换行符。 我可以使EditorGUIUtility.systemCopyBuffer以某种方式包含原始字符串中的换行符吗?

1 个答案:

答案 0 :(得分:0)

在不同的操作系统中输入换行符的方法有很多种。 对于Windows - \ r \ n,表格Mac - \ r,对于Unix - \ n和Linux - \ n

使用Environment.NewLine代替 \ n

var exp = new Regex(@“(?:\ r \ n | [\ r \ n])+”);

var general = exp.Replace(test,Environment.NewLine);