4

I am making an Editor plugin that communicates with a native plugin made with C++. I am required to notify the native plugin when Editor is about to be closed. I spent few hours on Unity's doc looking for an event or callback function that can be used to detect when Editor closes but I couldn't find one.

Usually, OnApplicationQuit, OnApplicationPause and OnApplicationFocus are used for something like this on standalone build but this is for the Editor so it wouldn't work

Does anyone know about any function or event to do this? If this there no built-in solution to do, is there a hack or some other ways to do this?

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • [editor on destroy](https://docs.unity3d.com/ScriptReference/EditorWindow.OnDestroy.html) maybe? – MX D Jun 21 '17 at 07:39
  • Nope. You use that when you have a plugin Window and want to detect when that Windows closes. I want to detect when the Editor itself it is about to close. – Programmer Jun 21 '17 at 07:40

2 Answers2

2

There is no native way to detect the shutdown of the editor it self.

However, you possibly could hook up to the proccess itself and wait for the exited event as described in this answer.

But if possible you will want to do this on the c++ side itself instead.

C++, How to determine if a Windows Process is running?

MX D
  • 2,453
  • 4
  • 35
  • 47
  • I am not looking for a native way to detect the shutdown. I am looking for way to do it in C# then I can call a native function to let it know that Unity is closing. The problem is detecting the close in C#. – Programmer Jun 21 '17 at 07:55
  • Also, checking process won't help here because I have pointers in the native side and C# side that is used to pass and receive Object instances. I have to use the current pointer from the C# side then pass it to the C++ side to disable a hardware.Basically Instance of Editor must match the plugin. So process can't do that as there is no way to retrieve the pointer but thanks for your answer. – Programmer Jun 21 '17 at 07:57
  • In that case I fear it may not be possible at all. – MX D Jun 21 '17 at 08:01
  • I am thinking about using reflection but that will take days to find the proper variables to hook to. – Programmer Jun 21 '17 at 08:04
  • 2
    A possible, quirky work around could be having a 0,0 sized editor window open off screen at all times. which, upon closing does the things you want to do. On a alt+f4 shutdown that would give you 1-2 seconds to cleanup all the things you had to clean up. – MX D Jun 21 '17 at 08:06
  • 1
    Sounds ok. Don't even know that will work but the only bad side of this is the short-cut + the window. Opening window that is not being used can be annoying. Will use this method if there is no other solution. – Programmer Jun 21 '17 at 08:12
2

now there is EditorApplication.quitting in unity 2018.2

UnityEditor.EditorApplication.quitting += OnQuitting;

private void OnQuitting()
{
    // do what you want to do when editor quit
}
Atheos
  • 21
  • 3