unity3d 奖励视频脚本能量+1

omvjsjqw  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(140)

看到奖励广告后,我不知道如何奖励玩家更多的能量。所以我想奖励玩家更多的能量。看起来你的帖子大部分是代码,请添加更多的细节。看起来你的帖子大部分是代码,请添加更多的细节。看起来你的帖子大部分是代码,请添加更多的细节。

private BannerView bannerAd;
private InterstitialAd interstitial;
private RewardBasedVideoAd rewardBasedVideo;
bool isRewarded = false;
// Start is called before the first frame update
void Start()
{
    MobileAds.Initialize(InitializationStatus => { });
    this.rewardBasedVideo = RewardBasedVideoAd.Instance;

    this.rewardBasedVideo.OnAdRewarded += this.HandleRewardBasedVideoRewarded;
    this.rewardBasedVideo.OnAdClosed += this.HandleRewardBasedVideoClosed;
    this.RequestRewardBasedVideo();
    this.RequestBanner();
}
void Update()
{
    if (isRewarded)
    {
        isRewarded = false;
        
    }

我有一个能量条250/250,一旦按下播放的能量是去249/250能量,我如何脚本增加能量+1时,球员按下奖励按钮?

[SerializeField] Text energyText;
[SerializeField] Text timerText;
[SerializeField] Slider energyBar;
private int maxEnergy = 250;
private int currentEnergy;
private int restoreDuration = 250;
private DateTime nextEnergyTime;
private DateTime lastEnergyTime;
private bool isRestoring = false;
// Start is called before the first frame update
void Start()
{
    if(!PlayerPrefs.HasKey("currentEnergy"))
    {
        PlayerPrefs.SetInt("currentEnergy", 50);
        Load();
        StartCoroutine(RestoreEnergy());

    }
    else
    {
        Load();
        StartCoroutine(RestoreEnergy());
    }
}
public void UseEnergy()
{
    if(currentEnergy >= 1)
    {
        currentEnergy--;
        UpdateEnergy();
        if (isRestoring == false)
        {
            if(currentEnergy + 1 == maxEnergy)
            {
                nextEnergyTime = AddDuration(DateTime.Now, restoreDuration);
            }
            StartCoroutine(RestoreEnergy());
        }
    }
    else
    {
        Debug.Log("Isufficient Energy!!");
    }
}
private IEnumerator RestoreEnergy()
{
    UpdateEnergyTimer();
    isRestoring = true;
    
    while(currentEnergy < maxEnergy)
    {
        DateTime currentDateTime = DateTime.Now;
        DateTime nextDateTime = nextEnergyTime;
        bool isEnergyAdding = false;

        while(currentDateTime > nextDateTime)
        {
            if(currentEnergy < maxEnergy)
            {
                isEnergyAdding = true;
                currentEnergy++;
                UpdateEnergy();
                DateTime timeToAdd = lastEnergyTime > nextDateTime ? lastEnergyTime : nextDateTime;
                nextDateTime = AddDuration(timeToAdd, restoreDuration);
            }
            else
            {
                break;
            }
        }
        if (isEnergyAdding == true)
        {
            lastEnergyTime = DateTime.Now;
            nextEnergyTime = nextDateTime;
        }
        UpdateEnergyTimer();
        UpdateEnergy();
        Save();
        yield return null;
    }
    isRestoring = false;
}

private DateTime AddDuration(DateTime datetime, int duration)
{
    return datetime.AddSeconds(duration);
    //return datetime.AddMinutes(duration);
}

private void UpdateEnergyTimer()
{
    if (currentEnergy >= maxEnergy)
    {
        timerText.text = "Full";
        return;
    }
    TimeSpan time = nextEnergyTime - DateTime.Now;
    string timeValue = String.Format("{0:D2}:{1:D1}", time.Minutes, time.Seconds);
    timerText.text = timeValue;
}

private void UpdateEnergy()
{
    energyText.text = currentEnergy.ToString() + "/" + maxEnergy.ToString();

    energyBar.maxValue = maxEnergy;
    energyBar.value = currentEnergy;

}

private DateTime StringToDate(string datetime)
{
    if (String.IsNullOrEmpty(datetime))
    {
        return DateTime.Now;
    }
    else
    {
        return DateTime.Parse(datetime);
    }
}

private void Load()
{
    currentEnergy = PlayerPrefs.GetInt("currentEnergy");
    nextEnergyTime = StringToDate(PlayerPrefs.GetString("nextEnergyTime"));
    lastEnergyTime = StringToDate(PlayerPrefs.GetString("lastEnergyTime"));
}

private void Save()
{
    PlayerPrefs.SetInt("currentEnergy", currentEnergy);
    PlayerPrefs.SetString("nextEnergyTime", nextEnergyTime.ToString());
    PlayerPrefs.SetString("lastEnergyTime", lastEnergyTime.ToString());
}

}

tyky79it

tyky79it1#

您可以确定用户是否在广告回调事件中获胜并添加此事件回调

// Called when the user should be rewarded for interacting with the ad.
this.rewardedAd.OnUserEarnedReward += HandleUserEarnedReward;

然后

public void HandleUserEarnedReward(object sender, Reward args)
{
        //Now it's time to reward the user
        //Use those if you want to get the amount from the ad setup
        //string type = args.Type;
        //double amount = args.Amount;
}

相关问题