Skip to content

Instantly share code, notes, and snippets.

@Lachee
Last active August 26, 2021 12:17
Show Gist options
  • Save Lachee/64044a7a8414b2bf05a3fc3b7b2511e0 to your computer and use it in GitHub Desktop.
Save Lachee/64044a7a8414b2bf05a3fc3b7b2511e0 to your computer and use it in GitHub Desktop.
Utility Script to create EOL Conversions.
// Author: Lachee
// License: Public Domain - Do what you want with it
// Note: Script to convert line endings in unity so you dont get those annoying warnings.
// Simply put this script in a folder called Editor, then go Tools -> EOL Conversion -> Windows/Unix
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEditor;
using System.IO;
public class EOLConversion
{
[MenuItem("Tools/EOL Conversion/Windows")]
private static void ConvertToWindows()
{
Convert("\r\n");
}
[MenuItem("Tools/EOL Conversion/Unix")]
private static void ConvertToUnix()
{
Convert("\n");
}
/// <summary> Converts all the assets and returns a list of files that were modified </summary>
public static string[] Convert(string lineEnding) {
List<string> assetsConverted = new List<string>();
string[] assetPaths = AssetDatabase.GetAllAssetPaths();
int progress = 0;
foreach(string assetPath in assetPaths)
{
EditorUtility.DisplayProgressBar("Converting Line Ending", assetPath, (progress++ / (float)assetPaths.Length));
if(!assetPath.EndsWith(".cs")) continue;
if (assetPath.StartsWith("Packages/")) continue;
string content = File.ReadAllText(assetPath);
string contentNew = Regex.Replace(content, @"\r\n|\n\r|\n|\r", lineEnding);
if (content != contentNew) {
File.WriteAllText(assetPath, contentNew);
assetsConverted.Add(assetPath);
}
}
EditorUtility.ClearProgressBar();
return assetsConverted.ToArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment