【发布时间】:2016-07-16 05:15:55
【问题描述】:
我想制作一个计时器来重置商家的商品价格和数量。
像许多安卓游戏一样,有些计时器在不玩游戏时会继续下降。 当您不玩游戏时,计时器应继续倒计时。 游戏关闭时如何保持倒计时?
下面是我的倒计时
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class margaretSellTimer : MonoBehaviour {
public Text TimerText;
public string dy;
public float days;
public float Hours;
public float Minutes;
public float Seconds;
initMerchantMargaret initMerchants;
player Player;
// Use this for initialization
void Start () {
initMerchants = GameObject.FindGameObjectWithTag ("initMerchant").GetComponent<initMerchantMargaret> ();
Player = GameObject.FindGameObjectWithTag ("player").GetComponent<player> ();
StartCoroutine(Wait());
}
// Update is called once per frame
void Update () {
if (days == 1) {
dy = "day ";
} else if (days > 1) {
dy = "days ";
} else {
dy = "day ";
}
if(Seconds < 10)
{
TimerText.text = (days + " " + dy + Hours + ":" + Minutes + ":0" + Seconds);
}
if(Seconds > 9)
{
TimerText.text = (days + " " + dy + Hours + ":" + Minutes + ":" + Seconds);
}
}
public void CountDown()
{
if(Seconds <= 0) {
if(Minutes > 0) {
MinusMinute();
Seconds = 60;
}
}
if (Minutes <= 0) {
if(Hours > 0) {
MinusHours();
Minutes = 59;
Seconds = 60;
}
}
if (Hours <= 0) {
if(days > 0) {
MinusDays();
Hours = 23;
Minutes = 59;
Seconds = 60;
}
}
if(Minutes >= 0)
{
MinusSeconds();
}
if(Hours <= 0 && Minutes <= 0 && Seconds <= 0 && days <= 0)
{
StopTimer();
}
else
{
Start ();
}
}
public void MinusDays() {
days -= 1;
}
public void MinusHours() {
Hours -= 1;
}
public void MinusMinute() {
Minutes -= 1;
}
public void MinusSeconds() {
Seconds -= 1;
}
public IEnumerator Wait() {
yield return new WaitForSeconds(1);
CountDown();
}
public void StopTimer() {
Seconds = 5;
Minutes = 0;
Hours = 0;
days = 0;
Player.margaretItemSell.Clear ();
Player.productMargaretSell.Clear ();
Player.productSellMargaret.Clear ();
initMerchants.margaretSell ();
Start ();
}
}
谢谢。
【问题讨论】:
-
退出游戏时保存状态,而不是继续计时。然后当游戏再次开始时,计算保存状态与当前时间之间的偏移量,并从那里继续计时器。
-
^ 嗯,这是一个很好的解决方案。但我想如果目的是在特定时间后发送推送通知,那么可能需要使用原生 android 代码或一些 3rd 方插件创建后台服务。
-
看在上帝的份上……只需要使用 Invoke。