diff --git a/Assets/DemoGame/GameScript/Editor/DomainReloadTool.cs b/Assets/DemoGame/GameScript/Editor/DomainReloadTool.cs new file mode 100644 index 0000000..5065395 --- /dev/null +++ b/Assets/DemoGame/GameScript/Editor/DomainReloadTool.cs @@ -0,0 +1,79 @@ +using UnityEditor; +using UnityEditor.Compilation; +using UnityEngine; + +namespace DragonSoul.Editor +{ + public class DomainReloadTool + { + private const string logColor = "{0}"; + private const string ReloadKey = "DomainReloadTool.Reload"; + private const string ReloadDomainTimerKey = "DomainReloadTool.ReloadDomainTimer"; + private const string CompileDomainTimerKey = "DomainReloadTool.CompileDomainTimer"; + + [InitializeOnLoadMethod] + static void Init() + { + CompilationPipeline.compilationStarted += OnCompilationStarted; + CompilationPipeline.compilationFinished += OnCompilationFinished; + + AssemblyReloadEvents.beforeAssemblyReload += OnBeforeAssemblyReload; + AssemblyReloadEvents.afterAssemblyReload += OnAfterAssemblyReload; + + EditorApplication.playModeStateChanged += EditorApplication_playModeStateChanged; + } + + private static void OnCompilationStarted(object obj) + { + Debug.LogFormat(logColor, "Begin Compile Domain..."); + //记录时间 + SessionState.SetInt(CompileDomainTimerKey, (int)(EditorApplication.timeSinceStartup * 1000)); + } + + private static void OnCompilationFinished(object obj) + { + var timeMS = (int)(EditorApplication.timeSinceStartup * 1000) - SessionState.GetInt(CompileDomainTimerKey, 0); + Debug.LogFormat(logColor, $"End Compile Domain : {timeMS} ms"); + } + + + private static void EditorApplication_playModeStateChanged(PlayModeStateChange state) + { + switch (state) + { + case PlayModeStateChange.ExitingEditMode: + if (!SessionState.GetBool(ReloadKey, false)) + ForceReloadDomain(); + break; + case PlayModeStateChange.EnteredPlayMode: + break; + case PlayModeStateChange.ExitingPlayMode: + SessionState.SetBool(ReloadKey, false); + break; + case PlayModeStateChange.EnteredEditMode: + break; + } + } + + //开始reload domain + private static void OnBeforeAssemblyReload() + { + Debug.LogFormat(logColor, "Begin Reload Domain..."); + //记录时间 + SessionState.SetInt(ReloadDomainTimerKey, (int)(EditorApplication.timeSinceStartup * 1000)); + } + + //结束reload domain + private static void OnAfterAssemblyReload() + { + var timeMS = (int)(EditorApplication.timeSinceStartup * 1000) - SessionState.GetInt(ReloadDomainTimerKey, 0); + Debug.LogFormat(logColor, $"End Reload Domain : {timeMS} ms"); + SessionState.SetBool(ReloadKey, true); + } + + private static void ForceReloadDomain() + { + EditorUtility.RequestScriptReload(); + } + } +} \ No newline at end of file diff --git a/Assets/DemoGame/GameScript/Editor/DomainReloadTool.cs.meta b/Assets/DemoGame/GameScript/Editor/DomainReloadTool.cs.meta new file mode 100644 index 0000000..94c615d --- /dev/null +++ b/Assets/DemoGame/GameScript/Editor/DomainReloadTool.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9021bcbc6c9247c49825d31901cd8d17 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: