- Home /
How to I write native newline Characters (when using the systemCopyBuffer)?
I am writing an editor script using EditorGUIUtility.systemCopyBuffer to make a textual cut/paste for some custom data in my scene. My first intuition was that I could read and write to the system copy buffer on my own platform without having to worry about newline character differences. On Windows, my copied strings don't contain the correct windows newline sequence, "\r\n", instead containing only the "\n" character.
I denote newline characters in my code with "\n", but I expected that to either represent or be translated to the correct newline sequence for the environment (at least the one where initial compilation occurred). Perhaps this expectation comes incorrectly from a C++ mentality, but it is not fulfilled.
Ideally, there would be an easy way to get the proper newlines for the platform where Unity is being run.
What is the best way to fix my newline characters?
Answer by DtBeloBrown · Apr 19, 2010 at 06:51 PM
The .Net framework provides the System.Environment.NewLine property which will be the proper newline character sequence for the platform your program runs in. You can use "\n" as usual in your code and then translate back and fourth quickly by using something like this:
myString = myString.Replace("\n", System.Environment.NewLine);
EditorGUIUtility.systemCopyBuffer = myString;
See http://msdn.microsoft.com/en-us/library/system.environment.newline.aspx
Good Question/Answer, I didn't know about that property. You must have pre-written the answer to beat @Duck to it, though :)
hehe, I did indeed pre-write it. Duck is the speed machine. At first I thought it was a bug in systemCopyBuffer (I have come to expect non-native newlines popping up in a few strange places while using unity on windows), but once I figured out what was going on I figured it would still be worth sharing so I posted it anyway.