diff --git a/scripts/DllManipulator.cs b/scripts/DllManipulator.cs index 2b4c5b4..8440a3f 100644 --- a/scripts/DllManipulator.cs +++ b/scripts/DllManipulator.cs @@ -653,6 +653,7 @@ public class DllManipulatorOptions public bool crashLogsStackTrace; public bool mockAllNativeFunctions; public bool onlyInEditor; + public bool enableInEditMode; } public enum DllLoadingMode diff --git a/scripts/DllManipulatorScript.cs b/scripts/DllManipulatorScript.cs index fcdf870..6679f4f 100644 --- a/scripts/DllManipulatorScript.cs +++ b/scripts/DllManipulatorScript.cs @@ -1,12 +1,18 @@ -using System; +using System; using System.Reflection; using System.Threading; using System.Linq; using UnityEngine; using UnityNativeTool.Internal; +#if UNITY_EDITOR +using UnityEditor; +#endif namespace UnityNativeTool { + #if UNITY_EDITOR + [ExecuteInEditMode] + #endif public class DllManipulatorScript : MonoBehaviour { private static DllManipulatorScript _singletonInstance = null; @@ -30,14 +36,30 @@ public class DllManipulatorScript : MonoBehaviour crashLogsStackTrace = false, mockAllNativeFunctions = true, onlyInEditor = true, + enableInEditMode = false }; private void OnEnable() { -#if !UNITY_EDITOR - if (Options.onlyInEditor) +#if UNITY_EDITOR + if (_singletonInstance != null) + { + if (EditorApplication.isPlaying) + Destroy(gameObject); + else + enabled = false; //Don't destroy as the user may be editing a Prefab + return; + } + _singletonInstance = this; + + if(EditorApplication.isPlaying) + DontDestroyOnLoad(gameObject); + + if(EditorApplication.isPlaying || Options.enableInEditMode) + Initialize(); +#else + if (Options.onlyInEditor) return; -#endif if (_singletonInstance != null) { @@ -45,8 +67,14 @@ private void OnEnable() return; } _singletonInstance = this; - DontDestroyOnLoad(gameObject); + DontDestroyOnLoad(gameObject); + Initialize(); +#endif + } + + private void Initialize() + { var initTimer = System.Diagnostics.Stopwatch.StartNew(); DllManipulator.Options = Options; @@ -66,7 +94,8 @@ private void OnDestroy() DllManipulator.UnloadAll(); DllManipulator.ForgetAllDlls(); - DllManipulator.ClearCrashLogs(); + DllManipulator.ClearCrashLogs(); + _singletonInstance = null; } } } diff --git a/scripts/Editor/DllManipulatorEditor.cs b/scripts/Editor/DllManipulatorEditor.cs index 69b58ae..1a64aee 100644 --- a/scripts/Editor/DllManipulatorEditor.cs +++ b/scripts/Editor/DllManipulatorEditor.cs @@ -21,6 +21,10 @@ public class DllManipulatorEditor : Editor "If true, native functions will be mocked only in assembly that contains DllManipulator (usually Assembly-CSharp)"); private static readonly GUIContent ONLY_IN_EDITOR = new GUIContent("Only in editor", "Whether to run only inside editor (which is recommended)."); + private static readonly GUIContent ENABLE_IN_EDIT_MODE = new GUIContent("Enable in Edit Mode", + "Should the DLLs also be mocked in edit mode (i.e. even if you don't hit 'play' in editor). " + + "Turning this off when not needed improves performance when entering edit mode. " + + "Changes are currently only visible on the next time edit mode is entered (i.e. when OnEnable is called so hit 'play' then 'stop' to apply)."); private static readonly GUIContent TARGET_ASSEMBLIES_GUI_CONTENT = new GUIContent("Target assemblies", "Choose from which assemblies to mock native functions"); private static readonly GUIContent DLL_PATH_PATTERN_GUI_CONTENT = new GUIContent("DLL path pattern", @@ -246,6 +250,8 @@ private void DrawOptions(DllManipulatorOptions options) } options.onlyInEditor = EditorGUILayout.Toggle(ONLY_IN_EDITOR, options.onlyInEditor); + + options.enableInEditMode = EditorGUILayout.Toggle(ENABLE_IN_EDIT_MODE, options.enableInEditMode); options.dllPathPattern = EditorGUILayout.TextField(DLL_PATH_PATTERN_GUI_CONTENT, options.dllPathPattern); @@ -334,4 +340,4 @@ public static void UnloadAll() DllManipulator.UnloadAll(); } } -} \ No newline at end of file +}