Alternative to EditorWindow.OnDisable to run before compiling

Hi,

I have a script that creates threads, open .Net sockets, and runs in the Editor. The issue I have is that if any sockets are open, Unity hangs if I go and edit my code, at the point it compiles the code again it does not kill my threads and close my sockets (at least properly). In order to avoid Unity to hang I need to close any connection/thread before the code is recompiled.

So far the way I found to handle this is to close my sockets/threads in the OnDisable method of an EditorWindow, because it is called just before the code gets recompiled. I don’t want to depend on having this EditorWindow opened in order to handle this.

Is there an alternative method that gets called just before the code gets recompiled? For example some static method, or anything in a MonoBehavior, or some delegate I could use in the EditorApplication class, etc.

Ok, I found the solution here: Callback before Unity reloads editor assemblies? - Questions & Answers - Unity Discussions

The solution came 1 month after I asked this, in Unity 2017.1, I added this in the static constructor of my MonoBehavior:

AssemblyReloadEvents.beforeAssemblyReload += MyDisconnectMethod;

Thanks, your answer helped me get rid of the ‘GarbageCollector disposing of ComputeBuffer allocated in…’ warning by giving me a way to release the ComputeBuffers when recompiling. :smiley: