android 倒计时 杀死,[Unity计时器倒计时,当应用杀死Android时不起作用

我对unity和c#还是陌生的,我有一个代码可以在10m后增加能量,我已经使用PlayerPrefs这样做,所以它可以统一工作,但不能在移动设备上工作,请帮忙。我无法理解到底是什么导致了电话问题。当应用程序未终止但在移动设备上最小化时,计时器也会暂停。我希望计时器倒数继续使游戏最小化,并且如果被杀死也可以。using System;

using UnityEngine;

using UnityEngine.UI;

public class EnergyAdder : MonoBehaviour

{

// Start is called before the first frame update

public float waitTime = 600;

Timer timer;

public GameRoot gameRoot;

public int count;

DateTime currentDate;

DateTime oldDate;

void Start()

{

//timer = new Timer(waitTime);

float remainingTime = PlayerPrefs.GetFloat("TimeOnExit");

timer = new Timer(remainingTime);

if (PlayerPrefs.HasKey("sysString"))

{

currentDate = DateTime.Now;

long temp = Convert.ToInt64(PlayerPrefs.GetString("sysString"));

oldDate = DateTime.FromBinary(temp);

TimeSpan difference = currentDate.Subtract(oldDate);

count = (int)(difference.TotalSeconds / waitTime);

Debug.Log("time remaining " + count);

gameRoot.AddEnergy(count);

timer = new Timer(remainingTime - (float)difference.TotalSeconds);

//PlayerPrefs.DeleteKey("TimeOnExit");

//PlayerPrefs.DeleteKey("sysString");

}

}

// Update is called once per frame

void Update()

{

if (Gameroot.isFull)

{

GetComponent().text = "Full";

count = 0;

timer.refresh();

}

else

{

//Debug.Log("deltatime ************************"+ secondstime);

timer.countDown();

if (timer.isFinished())

{

timer = new Timer(waitTime);

timer.refresh();

gameRoot.AddEnergy(1);

}

UpdateClock();

}

}

void UpdateClock()

{

int seconds = ((int)timer.timeLeft) % 60;

int minutes = (int)(timer.timeLeft / 60);

GetComponent().text = minutes.ToString() + ":" + seconds.ToString("00");

}

void OnApplicationQuit()

{

PlayerPrefs.SetFloat("TimeOnExit", timer.timeLeft);

var today = DateTime.Now;

PlayerPrefs.SetString("sysString", today.ToBinary().ToString());

PlayerPrefs.Save();

}

}

请帮助上面代码中的错误或缺失。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值