Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

OnApplicationPause ()

Discussion in 'Scripting' started by DannyJ, Dec 17, 2007.

  1. DannyJ

    DannyJ

    Joined:
    Sep 3, 2007
    Posts:
    40

    The quote is from the Unity manual. Does anyone know how to get OnApplicationPause() to work?
     
  2. DannyJ

    DannyJ

    Joined:
    Sep 3, 2007
    Posts:
    40
    Maybe nobody knows how this event is implemented? :?
     
  3. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    Maybe it is called when TimeScale is set to zero..?
     
  4. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    It's not something I've used, but I suspect it fires when the Unity player (web player or standalone) is no longer focussed. That is, if you switch to another application or window.
     
  5. Shannon

    Shannon

    Joined:
    Dec 20, 2007
    Posts:
    21
    I remember reading something about application pausing best practice in Unity, but I am not finding it in the search. The Application class does not include a Pause() function. How is it done as a game function?
     
  6. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    AFAIK OnApplicationPause() only gets fired in the Editor, not in the Player.
    Would be good if someone from UT could give some pointers.

    Cheers
    Shaun
     
  7. AmazingRuss

    AmazingRuss

    Joined:
    May 25, 2008
    Posts:
    933
    Anybody ever figure this out?
     
  8. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Isn't the pause event fired in the player if you don't enable application runinbackground and the window loses focus?
     
    kiber_ likes this.
  9. MrDude

    MrDude

    Joined:
    Sep 21, 2006
    Posts:
    2,569
    So my app has animations located inside various Update() calls, these animations are triggered via Invoke() calls and lastly, some of my objects are animated via ConstantForce...

    Then the player receives a call and the application is canceled simply because I have no idea how to pause all of this. I.e. save the amount of time left for an Invoke or Cancel the Update() calls. I know the TimeScale can end the FixedUpdate() calls, but what about Update() for non-physics-related rotations?

    Could this function be the answer to the question?
     
  10. TheAlchemist42

    TheAlchemist42

    Joined:
    Apr 17, 2008
    Posts:
    68
    I am working on a project now that has no physics. All motion is done with Translate() and Ani.Mate. When I set Time.timeScale=0, everything pauses, including timers, things in yield coroutines, and everything else. Though the docs allude to timeScale being for fixedUpdate, it seems to affect practically everything. Certain GUI functions still work, like popup windows that happen on mouseOver with the models, but that is about it.
     
  11. IPTN

    IPTN

    Joined:
    Jun 15, 2011
    Posts:
    4
    I know this thread is really old, but it seemed like people were still confused after the answer was given ^^ by Saun so I thought I would clarify his answer.

    OnApplicationPause() is fired when the app is being run inside the Unity editor and you click the Pause symbol next the Play symbol at the top.
     
  12. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    It's also triggered by iOS when the application is minimized.
     
  13. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    The complete summary of when it works:

    0. The event is fired on builds too but
    1. It only fires if the app is not told to run in background cause those are naturally never paused!
    2. It works on desktop if focus is lost
    3. As mentioned by Ntero, on iOS its fired as the app is being pushed into background due to switching to home or doing something else
     
  14. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Sorry for bump, but this is firing incorrectly when user presses play from the editor. Run in background is checked, and enabled in code too. Why is this?
     
  15. GennadiyKorol

    GennadiyKorol

    Joined:
    Jul 10, 2010
    Posts:
    107
    Same problem here, more than 1 year later.

    The event is triggered right after the play button is pressed but is being completely ignored when the "pause" button is being pressed in the editor.

    Is this an expected behavior?
     
  16. jistyles

    jistyles

    Joined:
    Nov 6, 2013
    Posts:
    34
    public void OnApplicationPause(bool pause) is fired on both a loss of focus as well as regaining focus; startup is apparently considered a focus gain too. The pause bool tell's you which state you're in.
    Here's the code we use on iOS and Android for when you exit to the home screen or change the app focus (also works for PC if your player/editor allows pause in background). This will pull up our pause state if you're in game, when the user switches back to the app:

    Code (csharp):
    1.     // on iOS and android - if you hit home button and then go back to the game then we should be nice and go to pause state
    2.     public void OnApplicationPause(bool pause)
    3.     {
    4.         //Debug.Log("APP PAUSE STATE = " + pause.ToString());
    5. #if (UNITY_IPHONE || UNITY_ANDROID)  !UNITY_EDITOR
    6.         if(!pause)
    7.         {
    8.             if(GameController.instance.gameState == GameController.GameState.GAME_STATE_PLAYING)
    9.             {
    10.                 GameController.instance.Action_PauseGame();
    11.             }
    12.        
    13.             // determine what music to play, ipad music may be on etc
    14.             if(MusicController.instance)
    15.             {
    16.                 MusicController.instance.UpdateApplicationWasPaused();
    17.             }
    18.         }
    19. #endif
    20.     }  
     
    Last edited: Nov 6, 2013