2016-07-15 139 views
0

我想制作一个计时器来重置商家的物品价格和数量。如何在游戏关闭时继续倒数计时器?

与许多安卓游戏一样,有些计时器在不玩游戏时会继续下去。 当你不玩游戏时,计时器应该继续倒计时。 游戏关闭时如何保持倒数计时器正在运行?

下面是我倒数计时器

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(); 
    } 
} 

感谢。

+2

而不是继续的计时器,保存状态,当你退出游戏。然后,当游戏再次开始时,计算保存状态和当前时间之间的偏移量,并从那里继续计时。 – Cheesebaron

+1

^那么这是一个很好的解决方案。但我猜如果目的是在一定时间后发送推送通知,那么可能需要使用原生android代码或某些第三方插件来创建后台服务。 –

+0

为了上帝的缘故,只需使用Invoke。 – Fattie

回答

1

当用户离开会话并通过PlayerPrefs将其存储在设备上时取出UNIX时间戳。

当用户回来时,从PlayerPrefs获取数据并将其与当前时间戳进行比较。

这不会阻止用户时间旅行,因为这将需要服务器存储。

private void OnLeaveSession() 
{ 
    long timestamp= (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds; 
    string value = timestamp.ToString(); 
    PlayerPrefs.SetString("Time", value); 
} 

private void OnResumeSession() 
{ 
    long timestamp= (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds; 
    string value = PlayerPrefs.GetString("Time"); 
    long oldTimestamp = Convert.ToInt64(value); 
    long elapsedSeconds = timestamp - oldTimestamp; 
    OnResumeSession(elapsedSeconds); // forward to listeners 
} 

OnResumeSession将是一个事件,但您也可以在该方法中执行操作。

+0

这是伟大的,但在我上面的代码如何将elapsedseconds转换为小时,分钟和秒? –

+0

@DennisLiu检查我的答案 –

+0

你不想转换为小时或分钟,这使得它更复杂。考虑你想检查2h是否已经过去,1h37m23s已经过去了,你如何检查两个小时?随着elapsedSeconds,只是检查是否> 7200. – Everts

1

而不是使用客户端逻辑的,使用服务器端状态和DB保存用户定时器的状态,或者你只是使用的登录时间[其中计时器开始]在DB,当用户重新登录保存或恢复播放,请致电后端api匹配客户端和服务器时间之间的偏移时间。

如果用户时间在服务器上过期显示您想要显示的内容等等。 这样做的另一种方法是减少错误机会为计时器的最后一个状态添加服务器端状态和用户端本地存储/文件存储,并且可以从那里恢复并与服务器匹配以根据需要执行更正。

关键是上次偏移。

1

要获得DaysHours,从elapsedSecondsMinutesSeconds,您可以使用TimeSpane

像这样:

TimeSpan interval = TimeSpan.FromSeconds(elapsedSeconds); 
days = interval.Days; 
hours = interval.Hours; 
minutes = interval.Minutes; // etc 
+0

多数民众赞成在@Umair M,我很抱歉,我可以;剔两个答案。但感谢您的帮助.. :) –

+0

Np。你可以投票了;) –

+0

是的,我做它:) –