From 8d45bfd4eb9b9224e30aa3e2a025a861ee6640c9 Mon Sep 17 00:00:00 2001 From: Roger Barton Date: Sun, 15 Mar 2020 20:18:14 +0100 Subject: [PATCH 1/4] Enables use outside of play mode This can be useful when writing native dlls for the editor, e.g. custom model importer. The unloading is currently done in OnDisable which may not be ideal. --- scripts/DllManipulatorScript.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/scripts/DllManipulatorScript.cs b/scripts/DllManipulatorScript.cs index ec3b615..4fae876 100644 --- a/scripts/DllManipulatorScript.cs +++ b/scripts/DllManipulatorScript.cs @@ -3,10 +3,12 @@ using System.Threading; using System.Linq; using UnityEngine; +using UnityEditor; using UnityNativeTool.Internal; namespace UnityNativeTool { + [ExecuteInEditMode] public class DllManipulatorScript : MonoBehaviour { private static DllManipulatorScript _singletonInstance = null; @@ -41,11 +43,15 @@ private void OnEnable() if (_singletonInstance != null) { - Destroy(gameObject); + if (EditorApplication.isPlaying) + Destroy(gameObject); + else + enabled = false; return; } _singletonInstance = this; - DontDestroyOnLoad(gameObject); + if(EditorApplication.isPlaying) + DontDestroyOnLoad(gameObject); var timer = System.Diagnostics.Stopwatch.StartNew(); @@ -85,7 +91,7 @@ private void OnEnable() InitializationTime = timer.Elapsed; } - private void OnDestroy() + private void OnDisable() { if (_singletonInstance == this) { @@ -96,6 +102,7 @@ private void OnDestroy() DllManipulator.UnloadAll(); DllManipulator.ForgetAllDlls(); DllManipulator.ClearCrashLogs(); + _singletonInstance = null; } } } From 0b77423324da045d8d267b85d917461f8d332cd7 Mon Sep 17 00:00:00 2001 From: Roger Barton Date: Sat, 21 Mar 2020 13:52:05 +0100 Subject: [PATCH 2/4] Add enableInEditMode, improved and tested execute in edit mode Uses OnDestroy to unload Tested in editor, builds compile --- scripts/DllManipulator.cs | 1 + scripts/DllManipulatorScript.cs | 40 ++++++++++++++++++++------ scripts/Editor/DllManipulatorEditor.cs | 6 ++++ 3 files changed, 38 insertions(+), 9 deletions(-) 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 62fcf4a..b0edc17 100644 --- a/scripts/DllManipulatorScript.cs +++ b/scripts/DllManipulatorScript.cs @@ -1,14 +1,19 @@ -using System; +using System; using System.Reflection; using System.Threading; using System.Linq; using UnityEngine; using UnityEditor; 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; @@ -32,27 +37,44 @@ public class DllManipulatorScript : MonoBehaviour crashLogsStackTrace = false, mockAllNativeFunctions = true, onlyInEditor = true, + enableInEditMode = false }; private void OnEnable() { -#if !UNITY_EDITOR - if (Options.onlyInEditor) - return; -#endif - +#if UNITY_EDITOR if (_singletonInstance != null) { if (EditorApplication.isPlaying) Destroy(gameObject); else - enabled = false; + 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; + + if (_singletonInstance != null) + { + Destroy(gameObject); + return; + } + _singletonInstance = this; + + DontDestroyOnLoad(gameObject); + Initialize(); +#endif + } + + private void Initialize() + { var initTimer = System.Diagnostics.Stopwatch.StartNew(); DllManipulator.Options = Options; @@ -62,7 +84,7 @@ private void OnEnable() InitializationTime = initTimer.Elapsed; } - private void OnDisable() + private void OnDestroy() { if (_singletonInstance == this) { @@ -72,7 +94,7 @@ private void OnDisable() DllManipulator.UnloadAll(); DllManipulator.ForgetAllDlls(); - DllManipulator.ClearCrashLogs(); + DllManipulator.ClearCrashLogs(); _singletonInstance = null; } } diff --git a/scripts/Editor/DllManipulatorEditor.cs b/scripts/Editor/DllManipulatorEditor.cs index 69b58ae..21e4ab3 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. " + + "Turning this off when not needed improves performance when entering edit mode. " + + "Changed are currently only visible on the next time edit mode is entered."); 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); From 3bc630b68bbafae4a661f6837d1f72edcdf81652 Mon Sep 17 00:00:00 2001 From: Roger Barton <42413282+rogerbarton@users.noreply.github.com> Date: Wed, 8 Apr 2020 16:57:02 +0200 Subject: [PATCH 3/4] Update scripts/Editor/DllManipulatorEditor.cs Co-Authored-By: mcpiroman <38111589+mcpiroman@users.noreply.github.com> --- scripts/Editor/DllManipulatorEditor.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/Editor/DllManipulatorEditor.cs b/scripts/Editor/DllManipulatorEditor.cs index 21e4ab3..551d475 100644 --- a/scripts/Editor/DllManipulatorEditor.cs +++ b/scripts/Editor/DllManipulatorEditor.cs @@ -22,7 +22,7 @@ public class DllManipulatorEditor : Editor 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. " + + "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. " + "Changed are currently only visible on the next time edit mode is entered."); private static readonly GUIContent TARGET_ASSEMBLIES_GUI_CONTENT = new GUIContent("Target assemblies", @@ -340,4 +340,4 @@ public static void UnloadAll() DllManipulator.UnloadAll(); } } -} \ No newline at end of file +} From c95ef293f8360a23f0122f22c8e304d52a21d100 Mon Sep 17 00:00:00 2001 From: Roger Barton Date: Wed, 8 Apr 2020 17:06:53 +0200 Subject: [PATCH 4/4] Review fixes --- scripts/DllManipulatorScript.cs | 4 ++-- scripts/Editor/DllManipulatorEditor.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/DllManipulatorScript.cs b/scripts/DllManipulatorScript.cs index b0edc17..6679f4f 100644 --- a/scripts/DllManipulatorScript.cs +++ b/scripts/DllManipulatorScript.cs @@ -3,7 +3,6 @@ using System.Threading; using System.Linq; using UnityEngine; -using UnityEditor; using UnityNativeTool.Internal; #if UNITY_EDITOR using UnityEditor; @@ -59,7 +58,8 @@ private void OnEnable() if(EditorApplication.isPlaying || Options.enableInEditMode) Initialize(); #else - if (Options.onlyInEditor) return; + if (Options.onlyInEditor) + return; if (_singletonInstance != null) { diff --git a/scripts/Editor/DllManipulatorEditor.cs b/scripts/Editor/DllManipulatorEditor.cs index 551d475..1a64aee 100644 --- a/scripts/Editor/DllManipulatorEditor.cs +++ b/scripts/Editor/DllManipulatorEditor.cs @@ -24,7 +24,7 @@ public class DllManipulatorEditor : Editor 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. " + - "Changed are currently only visible on the next time edit mode is entered."); + "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",