使用DateTime UNITY AD进行倒计时

问题描述 投票:0回答:1

我希望打赏广告之间有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个类:TimerString和TimeSpan。

我有两个类

  • 玩家观看广告
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;
        }

}}

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


    }
c# unity3d admob
1个回答
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
    }
}

0
投票

你的代码看起来很混乱,保持一个定时器,并在每一帧中用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;
     }
}
© www.soinside.com 2019 - 2024. All rights reserved.