行尾不一致(inconsistent line endings )
開發環境
有時候編輯Unity的腳本文件,代碼diff之后,或者從svn更新文件之后,Unity中會出現行尾不一致的信息。
我的開發環境如下:
visual studio 2015,unity3d 5.x,beyond compare 4,notepad++ 6.x
windows 7/10
行尾不一致
當Unity在編譯時,如果腳本的行尾不一致,會出現以下提示信息:
There are inconsistent line endings in the 'someFileName.cs' script. Some are Mac OS X (UNIX)
and some are Windows.This might lead to incorrect line numbers in stacktraces and compiler
errors. Unitron and other text editors can fix this using Convert Line Endings menu commands.
產生原因
這是由Windows和Unix不同的標准引起的,即“回車”和“換行”的問題。“回車”和“換行”是ASCII
字符集中兩個不可見的控制符。“回車”就是CHAR(13),即\r;“換行”就是CHAR(10),即\n。至於“回
車”和“換行”是來源於打字機,沒見過打字機或沒見過DOS時代的光標恐怕不好理解,因為GUI時代光
標都是自由移動的不再有回車的意義。
在Unix中“回車”不換行,“換行”才換行,行尾只需要一個“換行”,而在Windows中,“回 車”和“換
行”都換行,“回車”+“換行”才是行尾,所以符合Windows開發標准的文本編輯器才會提醒你當前編輯
的文本不符合Windows行尾標准。
Unix和Windows行尾
Unix平台: 行尾'LF'
Windows平台: 行尾‘CRLF’
CR回車(\r) LF換行(\n) Windows/Dos CRLF \r\n
Linux/Unix LF \n
MacOS CR \r
解決辦法
unity中創建的新腳本的默認行尾是LF,而visual studio中創建的新文件中默認行尾為CRLF,需要統一文件的行尾
統一為LF(Unix)
Visual Studio插件:Strip'em Add-in for Visual Studio
官網:http://www.grebulon.com/software/stripem.php
這個插件當你在Visual Studio中保存文本時自動轉換文件的格式,對你在Windows和Unix環境中混合工作時非常有用的。
在Unix中,文本文件的行結束是換行字符,在Windows中——一個回車和換行。
這個插件確保文件將被保存你所希望的方式。
它支持行尾(EOL)轉換到Unix,Windows或舊的Mac約定。
統一為CRLF(Windwos)
- 打開Unity安裝目錄(*C:\Program Files\Unity\Editor\Data\Resources\ScriptTemplates*)
- 找到Unity新建文件的模板文件(xx.txt),有規律的txt文件
- 用visual studio 打開這些文件,在文件 - 高級保存選項 - 行尾 選擇 window(CRLF)
以Unity 5.3.5為例,新建文件的模板文件如下:
- 81-C# Script-NewBehaviourScript.cs.txt
- 83-Editor Test C# Script-NewEditorTest.cs.txt
- 86-C# Script-NewStateMachineBehaviourScript.cs.txt
- 86-C# Script-NewSubStateMachineBehaviourScript.cs.txt
- 83-Shader__Standard Surface Shader-NewSurfaceShader.shader.txt
- 84-Shader__Unlit Shader-NewUnlitShader.shader.txt
- 90-Shader__Compute Shader-NewComputeShader.compute.txt
- 82-Javascript-NewBehaviourScript.js.txt
- 85-Shader__Image Effect Shader-NewImageEffectShader.shader.txt
參考資料
https://www.reddit.com/r/Unity3D/comments/3dl8oz/psa_how_to_fix_line_endings_in_unity_for_windows/
http://forum.unity3d.com/threads/inconsistent-line-endings.40671/
http://www.cnblogs.com/sevenyuan/archive/2012/12/06/2805114.html