使用 DateTime UNITY AD 倒计时

Countdown using DateTime UNITY AD

我希望激励广告之间有 15 分钟的间隔。我做了这个:

public void HandleUserEarnedReward(object sender, Reward args)
    {
        DateTime ad= DateTime.Now.AddMinutes(15);

        long adTicks = ad.Ticks;
        PlayerPrefs.SetInt("ticksVideo", (int)adTicks); }
void Update(){   

        DateTime currentTime= DateTime.Now;
        long currentTicks= currentTime.Ticks;
        PlayerPrefs.SetInt("currentTicks", (int)currentTicks);

        TimerControl = PlayerPrefs.GetInt("ticksVideo") - PlayerPrefs.GetInt("currentTicks");

        string mins = ((int)TimerControl / 600000000).ToString("00"); //600.000.000 ticks per minute
        string segs = ((int)TimerControl % 600000000).ToString("00");
        TimerString = string.Format("{00}:{01}", mins, segs);
        GetComponent<Text>().text = TimerString;  }

在DateTime.Now.AddMinutes中我输入了15但是倒计时持续了大约50秒。另一方面,TimerString 也不显示我指定的格式。怎么了?我应该使用 TimeSpan 吗?

编辑:

我有2个类:

public class AdMob : MonoBehaviour
{
   public static bool video = false;
   public Button buttonAd;
   public GameObject countdown;

   public void HandleUserEarnedReward(object sender, Reward args)
    {
        //Rewards
        buttonAd.interactable = false;
        countdown.SetActive(true);
        video = true;
    }
}
public class CountdownAd : MonoBehaviour
{
    public static float timeSinceLastAd = 0;
    void Update(){
    if (AdMob.video)
        {
            timeSinceLastAd += Time.deltaTime;
            if (timeSinceLastAd > (60 * 15))
            {
                buttonAd.interactable = true;
                countdown.SetActive(false);
                timeSinceLastAd = 0;
                AdMob.video = false;
            }
        } else
        {
            timeSinceLastAd = 0;
        }

}}

编辑 2:

 public class AdMob : MonoBehaviour {
        public GameObject countdownGameObject;
        public Button adButton;
        public Text countdown;

        //I hit the adButton and I watch the rewarded ad...
        public void HandleUserEarnedReward(object sender, Reward args)
        {
            //Rewards..
            countdownGameObject.SetActive(true);
            StartCoroutine(timer(15));
            adButton.interactable = false;
        }

        IEnumerator timer(int lapse)
        {
            while (lapse > 0)
            {
                int seconds = lapse % 60;
                int minutes = lapse / 60;
                countdown.text = $"{lapse / 60: 00}:{lapse % 60:00}";
                yield return new WaitForSeconds(1f);
                lapse--;
            }
            countdown.text = "00:00";
            //CountDown Finished
            gameObject.SetActive(false);

            if (lapse == 0)
            {
                adButton.interactable = true;
                countdownGameObject.SetActive(false);
            }
        }


    }

您的代码看起来很混乱,保留一个计时器并在每帧增加 Time.delaTime,当计时器超过 15 分钟时播放广告并重置计时器。

float timeSinceLastAd = 0;

void Update(){
     timeSinceLastAd += Time.deltaTime;
     if (timeSinceLastAd > (60 * 15)) {
     PlayAd(); //or whatever your method to play an ad is called
     timeSinceLastAd = 0;
     }
}

它是链接到显示倒计时的 UIText 的倒计时示例,我使用的是从 Start() 启动的 StartCoroutine,不使用 Update()

using System.Collections;
using UnityEngine;
using UnityEngine.UI;


public class Countdown : MonoBehaviour
{
    private Coroutine coroutine;
    private Text UITimer;
    void Start()
    {
        UITimer = GetComponent<Text>();
        if (coroutine != null) StopCoroutine(coroutine);
        coroutine = StartCoroutine(timer(60*15));

    }

    IEnumerator timer(int lapse)
    { 
        while (lapse > 0)
        {
            UITimer.text = $"{lapse / 60:00}:{lapse % 60:00}";
            yield return new WaitForSeconds(1f);
            lapse--;
        }
        UITimer.text = "00:00";
        //CountDown Finished
        gameObject.SetActive(false);

        // and all other things
    }
}