应用程序关闭时每 24 小时执行一次操作 Unity
Perform an action every 24 hours when the application is turned off Unity
我有一个代码,每24小时一次(86400000 milliseconds)
如果玩家的分数超过7000分,那么他将所有超过7000分的分数除以2,我怎样才能让这个条件每24小时满足一次即使应用程序已关闭?
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;
}
}
您需要更改代码以识别时间间隔多次过去的时间。
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;
}
}
我有一个代码,每24小时一次(86400000 milliseconds)
如果玩家的分数超过7000分,那么他将所有超过7000分的分数除以2,我怎样才能让这个条件每24小时满足一次即使应用程序已关闭?
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;
}
}
您需要更改代码以识别时间间隔多次过去的时间。
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;
}
}