//Add a private class variable
private FileSystemWatcher _assetFileWatcher;
//Create this method
private void ConfigureHotReload()
{
var scriptFileName = String.Format("{0}.lua", this.ScriptName);
//Set up an asset watcher that will reload our app.lua file
_assetFileWatcher = new FileSystemWatcher(Application.streamingAssetsPath);
_assetFileWatcher.Changed += OnAssetFileWatcherChanged;
_assetFileWatcher.Filter = scriptFileName;
_assetFileWatcher.IncludeSubdirectories = false;
_assetFileWatcher.EnableRaisingEvents = true;
}
//Set up the handler for the file watcher
private void OnAssetFileWatcherChanged(object sender, FileSystemEventArgs e)
{
var scriptFileName = String.Format("{0}.lua", this.ScriptName);
if (e.Name.EndsWith(scriptFileName))
LoadLuaFile(e.Name);
}
//Reload your script
private void LoadLuaFile(string filePath)
{
if (File.Exists(filePath))
{
Task.RunInMainThread(() =>
{
#if UNITY_EDITOR
EditorHelper.ClearConsole();
#endif
_script.DoString(File.ReadAllText(filePath));
});
}
}
public static class EditorHelper{ public static void ClearConsole() { // This simply does "LogEntries.Clear()" the long way: var logEntries = System.Type.GetType("UnityEditorInternal.LogEntries,UnityEditor.dll"); var clearMethod = logEntries.GetMethod("Clear", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public); clearMethod.Invoke(null, null); } }