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

Stop playmode when losing focus on application

Discussion in 'Editor & General Support' started by nicmarxp, Sep 4, 2020.

  1. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    When I save the code in my IDE, Unity starts building/reloading in the background, but only if Play mode is not active. If Play mode is on, it takes a lot of time before it stops play mode, and starts compiling/reloading.

    Therefore I'd like to always stop the play mode when Unity loses focus, so I tried this, but this stops the application if I click the console or anything else except the Game window.

    Code (CSharp):
    1.         void OnApplicationFocus(bool hasFocus) {
    2.             if (!hasFocus) {
    3.                 EditorApplication.ExecuteMenuItem("Edit/Play");
    4.             }
    5.         }
    The documentation says:
    Is there a way to detect only if I leave the actual Unity application? I'm using OS X Mojave and Unity 2020.1.4.

    Thanks! :)
     
  2. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    Does anyone have a clue on how to do this? It takes a lot of time each time I forget to quit playmode, change the code and then go back to Unity. Thanks :)
     
  3. Djayp

    Djayp

    Joined:
    Feb 16, 2015
    Posts:
    114
    nicmarxp likes this.
  4. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    Thanks, i'll check it out!