简体   繁体   中英

Perform an action every 24 hours when the application is turned off Unity

I have a code that once every 24 hours (86400000 milliseconds) if the player's score is more than 7000 points, then he divides all points above 7000 by 2, how can I make this condition be fulfilled every 24 hours even when the application is turned off?

using System;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using Random=UnityEngine.Random;

public class LeagueClock : MonoBehaviour
{
    private ulong TimeToReset;
    public float msToWait = 86400000f;
    private int scrMain;
    private bool Flag;

    private void Start(){
        scrMain = PlayerPrefs.GetInt("scrMain");

        TimeToReset = ulong.Parse(PlayerPrefs.GetString("LastReset"));

        Debug.Log(DateTime.Now);

    }

    private void Update(){
            if (scrMain > 7001){
                if (IsResetTrue()){
                    scrMain -= (scrMain-7000)/2;
                    PlayerPrefs.SetInt("scrMain", scrMain);
                    TimeToReset = (ulong)DateTime.Now.Ticks;
                    PlayerPrefs.SetString("LastReset", TimeToReset.ToString());
                }
                Flag = true;
            }
    }

    private bool IsResetTrue(){
        ulong diff = ((ulong)DateTime.Now.Ticks - TimeToReset);
        ulong m = diff / TimeSpan.TicksPerMillisecond;

        float secondsLeft = (float)(msToWait - m) / 1000.0f;

        Debug.Log(secondsLeft + " / " + scrMain);

        if (secondsLeft < 0){
            return true;
        }
        return false;


        }
    }

You need to change your code to recognize when the time interval has passed multiple times.

using System;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using Random=UnityEngine.Random;

public class LeagueClock : MonoBehaviour
{
    private ulong TimeLastReset;
    [SerializeField] private float msToWait = 86400000f;
    [SerializeField] private int scoreThreshold = 7000;
    private int scrMain;


    private void Start(){
        scrMain = PlayerPrefs.GetInt("scrMain");
        TimeLastReset = ulong.Parse(PlayerPrefs.GetString("LastReset", "0"));
        Debug.Log(DateTime.Now);
    }

    private void Update(){
        if (scrMain > scoreThreshold) {
            //apply the action for each interval that has passed.
            //For example, if the interval is 24 hours, and 49 hours
            //have passed, that's 2 intervals, so we reduce the score
            //twice.
            int intervalsPassed = GetIntervalsPassed();
            if (intervalsPassed > 0){
                for (int i = 0; i < intervalsPassed; i++) {
                    ReduceScore();
                }
                PlayerPrefs.SetInt("scrMain", scrMain);
                TimeLastReset = (ulong)DateTime.Now.Ticks;
                PlayerPrefs.SetString("LastReset", TimeLastReset.ToString());
            }
        }
    }

    private void ReduceScore() {
        scrMain -= (scrMain-scoreThreshold)/2; 
    }

    //returns the number of full time intervals that have passed since 
    //we last reduced the score
    private int GetIntervalsPassed(){
        ulong diff = ((ulong)DateTime.Now.Ticks - TimeLastReset);
        ulong ms = diff / TimeSpan.TicksPerMillisecond;

        float secondsLeft = (float)(msToWait - ms) / 1000.0f;
        int intervalsPassed = Mathf.FloorToInt(ms / msToWait);
        
        Debug.Log($"SecondsLeft: {secondsLeft} | Intervals: {intervalsPassed} | Score: {scrMain}");        
        return intervalsPassed;
    }
}

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