【问题标题】:How to continue count down timer when game is closed?游戏关闭时如何继续倒计时?
【发布时间】: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。

标签: c# android unity3d timer


【解决方案1】:

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

当用户回来时,从 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 将是一个事件,但您也可以在方法中执行操作。

【讨论】:

  • 很好,但是在我上面的代码中,如何将经过的秒数转换为小时、分钟和秒?
  • @DennisLiu 检查我的答案
  • 您不想转换为小时分钟或秒,这会使一切变得更加复杂。考虑您要检查是否已过去 2 小时和已过去 1 小时 37 分 23 秒,您如何检查这两个小时?使用 elapsedSeconds,只需检查是否 > 7200。
【解决方案2】:

不要使用客户端逻辑,而是使用服务器端状态和数据库来保存用户计时器状态,或者您只需使用登录时间[计时器开始的地方]保存在数据库中以及用户再次登录或恢复时播放,调用后端api匹配客户端和服务器时间的偏移时间。

如果服务器上的用户时间已过期,则显示您要显示的内容,依此类推。 另一种方法是减少错误机会,为计时器的最后状态添加服务器端状态和用户端本地存储/文件存储,然后可以从那里恢复并与服务器匹配以根据需要进行更正。

关键是最后一次偏移。

【讨论】:

    【解决方案3】:

    要从elapsedSeconds 获取DaysHoursMinutesSeconds,可以使用TimeSpane

    像这样:

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    • 2019-06-01
    相关资源
    最近更新 更多