- Home /
Callback before Unity reloads editor assemblies?
Is there a callback or event that I can subscribe to allowing me to detect when Unity plans to reload its editor assemblies?
For instance, before recompiling, before switching to/from play mode, etc:
??? += () {
// i.e. Stash some static variables before they are lost!
};
I am aware of EditorApplication.playmodeStateChanged
, but I was hoping for a more reliable solution which will also work for the various other situations where reload occurs.
Answer by numberkruncher · May 09, 2014 at 08:41 PM
With thanks to IRC it seems that this problem can be solved quite easily using a ScriptableObject
:
using UnityEngine;
using UnityEditor;
[InitializeOnLoad]
internal sealed class MyScriptableFriend : ScriptableObject {
static MyScriptableFriend() {
// Create instance as soon as Unity starts.
s_Instance = CreateInstance<MyScriptableFriend>();
// For some reason the following breaks this upon
// exiting from Unity (Case 598897).
//s_Instance.hideFlags = HideFlags.DontSave;
}
private static MyScriptableFriend s_Instance;
private void OnDisable() {
// This was what I needed!
}
}
Unity 4.5 is here and ISerializationCallbackReceiver
has been introduced.
@vexe ISerializationCallbackReceiver
occurs on a separate "serialization" thread and so cannot interact with the Unity API: http://docs.unity3d.com/ScriptReference/ISerializationCallbackReceiver.OnBeforeSerialize.html
Unity's serializer usually runs on the non main thread, while most of the Unity API can only be called from the main thread.
Crap. Never payed attention to that. Thanks for the hint. What do they actually mean by Unity API? Like, basically anything Unity-related?
Answer by vexe · May 09, 2014 at 03:45 AM
If you want an editor solution, have a look at the UnityEditor.Callbacks
namespace. DidReloadScripts
and PostProcessBuild/Scene
look interesting. (*1)
EDIT: I think I found something more interesting. EditorUtility.AssemblyReloadEvents
it's internal so you have to use reflection. (*2) Unfortunately there's no delegates, but methods. And analyzing them (finding out who uses/consumes them) didn't lead me anywhere. I'll see if I can inject something into them.
EDIT: I have indeed managed to inject custom code inside these assembly reload methods using the idea I mentioned here.
Here's an example of OnBeforeAssemblyReload
:
1- Somewhere in your code, create a twin OnBeforeAssemblyReload
with your injected code, and whatever code that is actually called (in this case, Security.ClearVerifiedAssemblies
), so:
public static void OnBeforeAssemblyReload()
{
typeof(Security).GetMethod("ClearVerifiedAssemblies", BindingFlags.NonPublic | BindingFlags.Static)
.Invoke(null, null);
Debug.Log("Injected OnBeforeAssemblyReload!");
}
2- Follow the injection steps in the previous link, and redirect the method call Security.ClearVerifiedAssemblies
to your OnBeforeAssemblyReload
so now it executes your injected code, and calls the same method it used to call (`ClearVerifiedAssemblies`)
3- Save the patched dll, and paste over your Unity/Editor/Managed folder.
4- Enjoy! :)
Of course, instead of the debug log, you could invoke a delegate from which you could subscribe to, use your imagination...
Edit: You might get some type/file not found errors. Just use peverify.exe on your patched assembly.
Now here's what you can do about OnAfterAssemblyReload
:
0- Create an injection method:
public static Action onAfterAssemblyReload;
public static void OnAfterAssemblyReload()
{
Debug.Log("Injected OnAfterAssemblyReload");
if (onAfterAssemblyReload != null)
onAfterAssemblyReload();
}
1- Navigate to the method using your reflector
2- Add a new call opcode to call your new method, right before the first call to GetAllProjectBrowsers
.
3- Save, and you're done (don't forget to verify your assembly using peverify)
Obviously, we could have done the same with OnBeforeAssemblyReload
, would have been cleaner, but just to show a variety of methods here... I'm still getting the hang of reflexil.
DISCLAIMER: Do this shit on you own responsibility, I'm not responsible for the laws/stuff/etc that might be broken due to this hack. I do this for pure educational reasons.
Wow that is complicated! but fascinating since I haven't seen this kind of injection before. Didn't even know it was possible with .NET! Sadly this approach is not workable for me since this is for a commercial product and I wouldn't want to use this kind of hack. But thanks anyhow!
Answer by bluksPL · Aug 22, 2017 at 07:54 AM
Since version 2017.1 we (finally) have:
AssemblyReloadEvents
class in UnityEditor
Events:
afterAssemblyReload
beforeAssemblyReload
Delegates:
AssemblyReloadCallback
Link: https://docs.unity3d.com/2017.1/Documentation/ScriptReference/AssemblyReloadEvents.html
It's worth noting that beforeAssemblyReload
is called before OnDisable
and afterAssemblyReload
is called after OnEnable
for $$anonymous$$onoBehaviours
Your answer
![](https://koobas.hobune.stream/wayback/20220613144619im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
UnityEventBase editor changed callback 0 Answers
Locking assembly reloading in editor 0 Answers
Editor class for Animation window 1 Answer
Can Editor scripts be in external DLL's or assemblies? 1 Answer