Search Unity

How to automatically switch to pause screen when user turns off the phone?

Discussion in 'Android' started by Jeffthegreat17, Jun 25, 2017.

  1. Jeffthegreat17

    Jeffthegreat17

    Joined:
    Jun 4, 2015
    Posts:
    6
    Hello there Unity Community,

    I wanted to know how I could switch screens to my pause screen when a person who is trying out my app on his phone decides to turn off the screen. I want the game to already have switched to my pause screen when he returns to the game.

    Thanks in advance :)
     
  2. Evaldas_Unity

    Evaldas_Unity

    Unity Technologies

    Joined:
    Jun 23, 2016
    Posts:
    85
    Hey! For such simple questions it might be more efficient to ask in 'Answers' forum.

    I think using OnApplicationFocus and OnApplicationPause should work in your case : )
    Code (CSharp):
    1. void OnApplicationFocus(bool pauseStatus)
    2. {
    3.     if(pauseStatus)
    4.     {
    5.         //your app is NO LONGER in the background
    6.     }
    7.     else
    8.     {
    9.         //your app is now in the background
    10.     }
    11. }
    Read more here:
    https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationFocus.html
    https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationPause.html
     
    CrandellWS likes this.