Unity Editor Tool to Convert Line Endings

Unity Editor Menu to Convert Line EndingsIt sometimes happens that you run into "inconsistent line ending" warnings when using the Unity Editor. For example, this can happen when importing 3rd-party code from a package in the Unity Asset Store, when working on a project using both Windows and Mac machines, or when using multiple text editors or IDEs to write scripts (such as Microsoft Visual Studio and Unity MonoDevelop). The current post provides a C# script that implements menu-item tools in the Unity Editor to help fix these sorts of issues by applying your preferred end-of-line style consistently to all scripts, shaders and similar text files in the project.

For the record, the full warning message usually goes something like this:
There are inconsistent line endings in the 'Assets/FILENAME.cs' script. Some are Mac OS X (UNIX) and some are Windows.
This might lead to incorrect line numbers in stacktraces and compiler errors. Many text editors can fix this using Convert Line Endings menu commands.

In the download section near the end of this post is where you'll find the C# script in two available formats (namely, package or code listing). After either installing the provided Unity package or manually recreating the LineEndingsEditMenu.cs file from the source code, the new script should be located in your project's Editor folder as shown in the screenshot below.

Screenshot of CSI Simple C# Interpreter on Unity 3D

The new conversion tools can then be found in the main Edit menu under EOL Conversion. From there you can choose either the Windows of UNIX (Mac OS X) line-ending format as shown below.

Screenshot of CSI Simple C# Interpreter on Unity 3D

To make sure you do not accidentally trigger the conversion process, a confirmation dialog will be presented.

Screenshot of CSI Simple C# Interpreter on Unity 3D

If you continue, the next step may take a while to complete when you have many files in your project (so wait patiently). A log message will finally show up in the Console window with a summary of the conversion process. Any modified files will also trigger an automatic recompile, and the warning messages should then disappear. The list of file types that are converted is specified by the fileTypes variable in the script, should you wish to customize it to include more of fewer file extensions. Happy coding!

Attribution: The intro image includes a modified overlay of the "Write" icon by Freepik from www.flaticon.com where the original icon is licensed under CC BY 3.0.