Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Application.quitting event is not consistent in editor

Discussion in 'Editor & General Support' started by CodeRonnie, Aug 11, 2021.

  1. CodeRonnie

    CodeRonnie

    Joined:
    Oct 2, 2015
    Posts:
    528
    I have a static class that utilizes the
    [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSplashScreen)]
    attribute to execute a method when the game starts. This provides an excellent entry point for my application to start up. It also subscribes to the
    Application.quitting
    event in the static constructor which allows me to perform a graceful shutdown when the application is closed. This all works as expected in the built player application, and in the editor, even with domain reload and scene reload disabled. When I press play it's as if the game is starting up in a built player application, and when I exit play mode it's as if the built player application has quit or been closed. However, if I am playing in the editor, and I close the entire Unity editor application, the Application.quitting event does not fire. This seems inconsistent to me.

    I have designed my software so that missing that graceful shutdown should not be catostrophic, but it sure would be nice to consistently shut down gracefully if at all possible. As it is, closing the Unity editor application while playing is like simulating a critical failure that crashed the built player application. That's not what I would expect to occur.

    I already filed a bug report, but the customer QA representative who responded told me I should use EditorApplication.quitting, closed the report, and never responded to me again.

    It is indeed possible to force a graceful shutdown with that event. However, it would require me to add references to editor-only assemblies in a runtime-only class at the very core of my application that should have no concept of what a Unity editor is. The build will not even compile unless you wrap that editor specific code in preprocessor directives for editor-only compilation. It's an ugly hack (IMO) that isn't really up to my standards for quality code, and still it feels like I'm solving locally what is very much a bug in the Unity editor.

    An alternative would be to create an editor-only script that subscribes to the EditorApplication.quitting event, checks the state of playmode, and that tells my application to shut down. However, that would mean making the Shutdown() method on my globally accessible static class public, breaking encapsulation. Anything anywhere in the codebase could call Shutdown() at any time. That's also not really an acceptable solution.

    Am I missing some way to consistently pair up my startup and shutdown logic here? I am fairly certain I've got the right methods, attributes, and events. Is this indeed a bug in the editor? Should Application.quitting be called if you close the editor while in the middle of play?
     
    Last edited: Aug 12, 2021
  2. CodeRonnie

    CodeRonnie

    Joined:
    Oct 2, 2015
    Posts:
    528
    Update: The customer QA rep has said that the issue has been sent to the product team as a usability consideration. There is not a public issue tracker link for those types of issues.