Working in the Background

Hello, for example, we won a chest in the game and the chest opening time is 3 hours. What code should I write to make it run in the background?

I would be glad if you could help me. I Will Do It As Android Output

You could start a basic timer when you win a chest, Then if the application is paused you can use the

private DateTime timeWhenPaused;
private float timer;
private bool isTimerRunning;

void Start()
{
    isTimerRunning = true;
}

void Update()
{
    if (isTimerRunning == true)
    {
        if (timer < 0)
        {
            timer -= Time.deltaTime;
        }
        else
        {
            timer = 0;
            isTimerRunning = false;
            Debug.Log("Timer Was Complete");
        }
    }
}

private void OnApplicationPause(bool pause)
{
    if (isTimerRunning == true)
    {
        if (pause == true)
        {
            Debug.Log("Game Got Paused");
            timeWhenPaused = DateTime.Now;
        }
        else
        {
            Debug.Log("Game Got UnPaused");
            timer -= (float)(DateTime.Now - timeWhenPaused).TotalSeconds;
        }
    }
}

Now this only works when the app is minimised meaning paused, You could use the OnApplicationQuit() Callback and at that moment save the time and the state of a bool, then on start you can check if the state of the bool is the same that of which you saved it, then you can load the time it was quit from wherever you saved it, And use that to get the difference between the current time and the time the app was quit, and deplete that much secs from the timer just like I have shown you above in the code which works for the app being paused.