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.

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.

26381-asm.png

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! :slight_smile:

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.

Since version 2017.1 we (finally) have:


AssemblyReloadEvents

class in UnityEditor

Events:

  • afterAssemblyReload
  • beforeAssemblyReload

Delegates:

  • AssemblyReloadCallback

Link: Unity - Scripting API: AssemblyReloadEvents


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!
    }
}