Skip to content

Commit

Permalink
fix: when using VS Code, C# project file is not modified in Unity 202…
Browse files Browse the repository at this point in the history
…1 or later
  • Loading branch information
mob-sakai committed Dec 13, 2021
1 parent a623bcf commit dc97d57
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions Editor/CSharpProjectModifier.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -106,5 +107,61 @@ private static string AddRuleSet(string content, string ruleset)

private const string NewLine = "\r\n";
private const string AdditionalContentComment = "<!-- C# Settings For Unity -->";

#if UNITY_2021_1_OR_NEWER
private static FileSystemWatcher _watcher;

private static void OnCSProjectCreated(string file)
{
if (string.IsNullOrEmpty(file) || Path.GetExtension(file) != ".csproj" || !File.Exists(file))
return;

//
var codeEditor = Unity.CodeEditor.CodeEditor.Editor.CurrentCodeEditor;
if (codeEditor?.GetType().Name != "VSCodeScriptEditor")
return;

try
{
var text = File.ReadAllText(file, System.Text.Encoding.UTF8);
var beforeHash = text.GetHashCode();
text = OnGeneratedCSProject(file, text);
var afterHash = text.GetHashCode();
if (beforeHash != afterHash)
{
Logger.LogInfo("<color=orange>Modify CSProject</color> {0}", Path.GetFileName(file));
File.WriteAllText(file, text);
}
}
catch (Exception e)
{
UnityEngine.Debug.LogException(e);
}
}

[InitializeOnLoadMethod]
private static void WatchCSProject()
{
#if !UNITY_EDITOR_WIN
Environment.SetEnvironmentVariable("MONO_MANAGED_WATCHER", "enabled");
#endif
var resultDir = Path.GetFullPath(".");
foreach (var file in Directory.GetFiles(resultDir, "*.csproj"))
EditorApplication.delayCall += () => OnCSProjectCreated(Path.Combine(resultDir, file));

_watcher?.Dispose();
_watcher = new FileSystemWatcher()
{
Path = resultDir,
NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.LastWrite,
IncludeSubdirectories = false,
EnableRaisingEvents = true,
Filter = "*.csproj",
};

_watcher.Created += (s, e) => EditorApplication.delayCall += () => OnCSProjectCreated(e.FullPath);
_watcher.Changed += (s, e) => EditorApplication.delayCall += () => OnCSProjectCreated(e.FullPath);
}
#endif
}
}

0 comments on commit dc97d57

Please sign in to comment.