Search Unity

EditorWindow callback for editor application closing?

Discussion in 'Scripting' started by greg-h, Aug 7, 2014.

  1. greg-h

    greg-h

    Joined:
    Jul 29, 2012
    Posts:
    17
    Is there any callback to an EditorWindow script for when the actual Unity editor is closing?

    All I want to do is save some assets using a custom serialization system before the application closes. Using EditorUtility.DisplayDialogComplex works perfectly when the play button is pressed or the EditorWindow itself is closed, but none of the normal callbacks seem to be invoked when the user closes Unity.

    OnDisable, OnDestroy, and OnApplicationQuit are not called.
     
    vexe likes this.
  2. greg-h

    greg-h

    Joined:
    Jul 29, 2012
    Posts:
    17
    For anybody wondering, this actually almost sorta works:

    Code (CSharp):
    1. void OnEnable()
    2. {
    3.     AppDomain.CurrentDomain.DomainUnload += OnDomainUnload;
    4. }
    5.  
    6. void OnDomainUnload()
    7. {
    8.     // do whatever
    9. }
    But the AppDomain is unloaded any time scripts are recompiled OR you enter play mode and the correct flags (EditorApplication.isPlaying, isPlayingOrWillChangePlayMode, isCompiling) are all false at the time DomainUnload is called. Also, if you try to use EditorUtility.DisplayDialogComplex, it causes a native null pointer exception and crashes unity. BUT you can write to files and stuff, so it has that going for it, which is nice. I guess.

    Still looking for a better solution, though.
     
    Kirbyrawr likes this.
  3. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    393
    http://answers.unity3d.com/answers/902435/view.html


    You could try using OnWillSaveAssets to add your assets to the list that needs to be saved. This is called when changing scenes, when you save a scene and if I'm not mistaken when Unity is closing.

    public class MyAssetModificationProcessor : AssetModificationProcessor {

    1. publicstaticstring[]OnWillSaveAssets(string[] paths)
    2. {
    3. return paths;
    4. }
    }

    Hope this helps.