Edit options when initialized, Fix cleanup when recompiling - #20
Conversation
…ns copy to DllManipScript When DllManipulator is initialized we pass a copy of the current DllManipulatorScript options. This allows us to keep editing the options when the manipulator is initialized/dlls are loaded as we are editing a copy. This is particularly useful when using enableInEditMode. Changes are always applied at OnEnable as before. To allow modifying without play/stop a DllManipulatorScript.Reinitialize() has been added which can be called via GUI (see DetectOptionChanges) which unloads dlls and initializes with new options. GUI is only shown when there are changes.
When recompiling only `OnDisable` is called, not `OnDestroy`. Now gets the callback for when the assembly is being reloaded, just when finished with compiling. This is called before `OnDisable`, so I just set a flag `_isRecompiling`. Previously the native function `UnityPluginUnload` was not being called causing various bugs.
mcpiroman
left a comment
There was a problem hiding this comment.
Some quick notes I left here, will have a closer look soon, lgtm though, thanks!
|
|
||
| // When enabling enableInEditMode for the first time, allow immediately initializing without waiting for OnEnable | ||
| if(DllManipulator.Options == null && t.Options.enableInEditMode && !EditorApplication.isPlaying && | ||
| GUILayout.Button(INITIALIZE_ENABLED_EDIT_MODE_GUI_CONTENT)) |
There was a problem hiding this comment.
I would maybe move that button to be close to the 'enable in edit mode' option
There was a problem hiding this comment.
I mean you enable it and this shows up right next to it so that you know you have to click it first for it to work, sth like warning button i guess
There was a problem hiding this comment.
I think its reasonably clear when the button pops up. If the user immediately clicks the initialize button and then changes the options below they will have to unnecessarily reinitialize. Having the button at the bottom implies that the other options also affect its behaviour.
Also in the code it is clearer as the functionality about re/initializing is together in DetectOptionChanges.
There was a problem hiding this comment.
Although, it looks like Options in DllManipulator can now be private?
There was a problem hiding this comment.
We use it in the Script and Editor to check if the DllManip is initialized/there are changes to be applied. We could make the setter private?
| if (_isRecompiling) | ||
| { | ||
| _isRecompiling = false; | ||
| OnDestroy(); |
There was a problem hiding this comment.
I don't really like calling a callback method so I'd be after creating separate one
There was a problem hiding this comment.
Created a Reset()
| if (DllManipulator.Options != null) | ||
| DllManipulator.Reset(); | ||
|
|
||
| #if UNITY_EDITOR |
There was a problem hiding this comment.
I think if this is not going to actually perform the initialization again, then this method shouldn't be called in the first place (it doesn't actually reinitialize).
There was a problem hiding this comment.
Yes, I've changed it although this requires that the Initialize function is public.
| if(_singletonInstance != this) | ||
| return; | ||
|
|
||
| if (DllManipulator.Options != null) |
There was a problem hiding this comment.
I'm not certain, but it appears to me that we could reduce the number of different places when options are stored and move them from this class to the DllManipulator itself? TBH I don't really like how it's scattered.
There was a problem hiding this comment.
Yes its a bit scattered but I think this makes sense:
- DllManipulatorScript has an Options copy which are seen in the inspector.
- DllManipulator has Options which are currently used.
- DllManipulatorEditor has Options solely for detecting changes in the inspector.
Each .cs only has to consider one Options (except the editor). Having the previousInspectorOptions from the editor in DM means DM has a copy which it never uses. I would leave it as is personally.
Co-authored-by: mcpiroman <38111589+mcpiroman@users.noreply.github.com>
Also removed check if singletonInstance in Reinitialize as this should never occur
| DllManipulator.Reset(); | ||
| _singletonInstance = null; | ||
| } | ||
| public void Reset() |
There was a problem hiding this comment.
This one doesn't need to be public right?
There was a problem hiding this comment.
No, its now private
|
|
||
| // When enabling enableInEditMode for the first time, allow immediately initializing without waiting for OnEnable | ||
| if(DllManipulator.Options == null && t.Options.enableInEditMode && !EditorApplication.isPlaying && | ||
| GUILayout.Button(INITIALIZE_ENABLED_EDIT_MODE_GUI_CONTENT)) |
| _customAfterUnloadTriggers?.Clear(); | ||
| _customBeforeUnloadTriggers?.Clear(); | ||
|
|
||
| Options = null; |
There was a problem hiding this comment.
We delete the DllManip Options copy. This is also used as an indicator to whether the DllManip is initialized.
There was a problem hiding this comment.
I mean, how does it consume the options when they are null? Sry if I miss sth
There was a problem hiding this comment.
Ah nvm, on the diff it blended in as though it was called in Initalize, not Reset, sorry
|
LGTM |

See commit messages for details.