Skip to content

Commit 3efdf0d

Browse files
Enable use outside of play mode (#12)
* 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. * Add enableInEditMode, improved and tested execute in edit mode Uses OnDestroy to unload Tested in editor, builds compile * Update scripts/Editor/DllManipulatorEditor.cs Co-Authored-By: mcpiroman <38111589+mcpiroman@users.noreply.github.com> * Review fixes Co-authored-by: mcpiroman <38111589+mcpiroman@users.noreply.github.com>
1 parent 6c6b17c commit 3efdf0d

3 files changed

Lines changed: 43 additions & 7 deletions

File tree

scripts/DllManipulator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,7 @@ public class DllManipulatorOptions
653653
public bool crashLogsStackTrace;
654654
public bool mockAllNativeFunctions;
655655
public bool onlyInEditor;
656+
public bool enableInEditMode;
656657
}
657658

658659
public enum DllLoadingMode

scripts/DllManipulatorScript.cs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
using System;
1+
using System;
22
using System.Reflection;
33
using System.Threading;
44
using System.Linq;
55
using UnityEngine;
66
using UnityNativeTool.Internal;
7+
#if UNITY_EDITOR
8+
using UnityEditor;
9+
#endif
710

811
namespace UnityNativeTool
912
{
13+
#if UNITY_EDITOR
14+
[ExecuteInEditMode]
15+
#endif
1016
public class DllManipulatorScript : MonoBehaviour
1117
{
1218
private static DllManipulatorScript _singletonInstance = null;
@@ -30,23 +36,45 @@ public class DllManipulatorScript : MonoBehaviour
3036
crashLogsStackTrace = false,
3137
mockAllNativeFunctions = true,
3238
onlyInEditor = true,
39+
enableInEditMode = false
3340
};
3441

3542
private void OnEnable()
3643
{
37-
#if !UNITY_EDITOR
38-
if (Options.onlyInEditor)
44+
#if UNITY_EDITOR
45+
if (_singletonInstance != null)
46+
{
47+
if (EditorApplication.isPlaying)
48+
Destroy(gameObject);
49+
else
50+
enabled = false; //Don't destroy as the user may be editing a Prefab
51+
return;
52+
}
53+
_singletonInstance = this;
54+
55+
if(EditorApplication.isPlaying)
56+
DontDestroyOnLoad(gameObject);
57+
58+
if(EditorApplication.isPlaying || Options.enableInEditMode)
59+
Initialize();
60+
#else
61+
if (Options.onlyInEditor)
3962
return;
40-
#endif
4163

4264
if (_singletonInstance != null)
4365
{
4466
Destroy(gameObject);
4567
return;
4668
}
4769
_singletonInstance = this;
48-
DontDestroyOnLoad(gameObject);
4970

71+
DontDestroyOnLoad(gameObject);
72+
Initialize();
73+
#endif
74+
}
75+
76+
private void Initialize()
77+
{
5078
var initTimer = System.Diagnostics.Stopwatch.StartNew();
5179

5280
DllManipulator.Options = Options;
@@ -66,7 +94,8 @@ private void OnDestroy()
6694

6795
DllManipulator.UnloadAll();
6896
DllManipulator.ForgetAllDlls();
69-
DllManipulator.ClearCrashLogs();
97+
DllManipulator.ClearCrashLogs();
98+
_singletonInstance = null;
7099
}
71100
}
72101
}

scripts/Editor/DllManipulatorEditor.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public class DllManipulatorEditor : Editor
2121
"If true, native functions will be mocked only in assembly that contains DllManipulator (usually Assembly-CSharp)");
2222
private static readonly GUIContent ONLY_IN_EDITOR = new GUIContent("Only in editor",
2323
"Whether to run only inside editor (which is recommended).");
24+
private static readonly GUIContent ENABLE_IN_EDIT_MODE = new GUIContent("Enable in Edit Mode",
25+
"Should the DLLs also be mocked in edit mode (i.e. even if you don't hit 'play' in editor). " +
26+
"Turning this off when not needed improves performance when entering edit mode. " +
27+
"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).");
2428
private static readonly GUIContent TARGET_ASSEMBLIES_GUI_CONTENT = new GUIContent("Target assemblies",
2529
"Choose from which assemblies to mock native functions");
2630
private static readonly GUIContent DLL_PATH_PATTERN_GUI_CONTENT = new GUIContent("DLL path pattern",
@@ -246,6 +250,8 @@ private void DrawOptions(DllManipulatorOptions options)
246250
}
247251

248252
options.onlyInEditor = EditorGUILayout.Toggle(ONLY_IN_EDITOR, options.onlyInEditor);
253+
254+
options.enableInEditMode = EditorGUILayout.Toggle(ENABLE_IN_EDIT_MODE, options.enableInEditMode);
249255

250256
options.dllPathPattern = EditorGUILayout.TextField(DLL_PATH_PATTERN_GUI_CONTENT, options.dllPathPattern);
251257

@@ -334,4 +340,4 @@ public static void UnloadAll()
334340
DllManipulator.UnloadAll();
335341
}
336342
}
337-
}
343+
}

0 commit comments

Comments
 (0)