简体   繁体   中英

How can I make my timer keep running when the iPhone user quits to the home screen or the App Switcher?

I'm making a quiz game and want to make sure that the user does not get additional answering time by exiting the app (without shutting it down).

My current timer uses Time.deltaTime . I'm thinking of replacing this with something like DateTimeOffset.Now.ToUnixTimeMilliseconds() (epoch time) or Time.realtimeSinceStartup (not sure if this one works). A solution to detect when the user closes the app would also work.

There are three built-in functions that might do what you want. There is OnApplication.Quit (does not work on mobile), OnApplication.Pause , and OnApplicationFocus .

I personally like using OnApplicationPause instead of OnApplicationFocus as focus is called on some phones when the keyboard is brought up. I have found that pause is called whenever a user hits the home button, closes the app, their phone dies, turns off their phone, etc.

The great part about the OnApplicationFocus and OnApplicationPause is that there is a bool parameter passed in to let you know whether you are unfocused or focused / unpaused or paused.

If you just want to know how long it has been since the last time the app is opened, you can easily stored a variable locally after an OnApplicationPause is called when the bool is true , then when it is false , you can detect how much time has elapsed. As the local variable will not persist if the user quits the game, you will need to also look into some sort of save/load implementation. There are already many good answers on StackOverflow for how to save data in Unity, so if you need help with that I can add a link.

As for the OnPause example here is how I would use it.

private long lastTimePaused = 0;

private void Awake
{
    // setting our paused time to the start time
    lastTimePaused = DateTimeOffset.Now.ToUnixTimeMilliseconds();   
}

void OnApplicationPause(bool pauseStatus)
{
    if(pauseStatus)
    {
        lastTimePaused = DateTimeOffset.Now.ToUnixTimeMilliseconds();  
        // save this lastTimePaused if you want the data to persist between a user quitting the app
        // and coming back
    }
    else
    {
        Debug.Log("Our elapsed miliseconds is: " + (DateTimeOffset.Now.ToUnixTimeMilliseconds() - lastTimePaused));
    }
}

If you like, you can also use OnApplicationFocus together with OnApplicationPause . Something like:

private long lastTimePaused = 0;

private void Awake
{
    // setting our paused time to the start time
    lastTimePaused = DateTimeOffset.Now.ToUnixTimeMilliseconds();   
}

void OnApplicationPause(bool pauseStatus)
{
    HandlePause(pauseStatus);
}

void OnApplicationFocus(bool hasFocus)
{
    HandlePause(!hasFocus);
}

private void HandlePause(bool isPaused)
{
    if(isPaused)
    {
        lastTimePaused = DateTimeOffset.Now.ToUnixTimeMilliseconds();  
        // save this lastTimePaused if you want the data to persist between a user quitting the app
        // and coming back
    }
    else
    {
        Debug.Log("Our elapsed miliseconds is: " + (DateTimeOffset.Now.ToUnixTimeMilliseconds() - lastTimePaused));
    } 
}

I am not sure what cases you would like to detect when a user is suspended, but first, try OnApplicationPause to see if it handles everything you need. If it does not, you can also combine it with OnApplicationFocus .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM