via “Tools for Unity” I’ve enabled Asset Database Refresh in Visual Studio Pro 2019. I really like that feature as my code is automatically re-compiled in Unity when saving in Visual Studio and Unity is ready to play when switching into the Editor.
With Unity 2019 LTS this worked absolutely flawless EVERY time. However, recently I’ve updated to 2020 LTS and now this feature is working out of 50 saves maybe 3 times. So it is absolutely useless at the moment.
It is utilizing one CPU core at 100% - but in wise foresight I’ve invested in a Ryzen 9 5950x - so it is not really a problem for me.
Hopefully this feature will work again without ugly hacks needed in the near future.
I’ve altered the file-watcher script a little bit - and it is now consuming ~1,5% of my CPU. As long as the refresh is broken I’m totally fine with this “price”.
Simply put it in any “Editor” script directory in your project, add the full path to your scripts into “ScriptPath” and you are ready to go.
Happy compiling.
using System.IO;
using System.Threading;
using UnityEditor;
[InitializeOnLoad]
public class FileWatcher
{
public static string ScriptPath = "PATH TO YOUR SCRIPTS";
public static bool SetRefresh;
static FileWatcher()
{
ThreadPool.QueueUserWorkItem(MonitorDirectory, ScriptPath);
EditorApplication.update += OnUpdate;
}
private static void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
{
SetRefresh = true;
}
private static void MonitorDirectory(object obj)
{
string path = (string)obj;
FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();
fileSystemWatcher.Path = path;
fileSystemWatcher.IncludeSubdirectories = true;
fileSystemWatcher.Changed += FileSystemWatcher_Changed;
fileSystemWatcher.Created += FileSystemWatcher_Changed;
fileSystemWatcher.Renamed += FileSystemWatcher_Changed;
fileSystemWatcher.Deleted += FileSystemWatcher_Changed;
fileSystemWatcher.EnableRaisingEvents = true;
}
private static void OnUpdate()
{
if (!SetRefresh) return;
if (EditorApplication.isCompiling) return;
if (EditorApplication.isUpdating) return;
AssetDatabase.Refresh();
SetRefresh = false;
}
}