gpt4 book ai didi

c# - unity RewardAd 函数调用更多的时间不只是一次

转载 作者:行者123 更新时间:2023-11-30 23:07:49 40 4
gpt4 key购买 nike

我想在我的游戏中使用 RewardAd。当您观看视频时,您会得到当前分数的 +10 分,而不是您的最高分。

你的高分是 45,现在是 37,所以你看视频的分数是 +10,你的高分是 47,这很好。但如果你再做一次,视频会给你+20分?!以及后面的时间+30等等。

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds.Api;
using System.Threading;

public class RewardAd : MonoBehaviour {

public int highscore;
public int score;
public static GameObject drawscore_obj;
public RewardBasedVideoAd rewardBasedVideo = null;
public string adUnitId;

void Start()
{
rewardBasedVideo = null;
GetComponent<SpriteRenderer> ().enabled = false;
//highscore = PlayerPrefs.GetInt ("highscore");
adUnitId = "ca-app-pub-2879768424205988/1590886374";
rewardBasedVideo = RewardBasedVideoAd.Instance;
AdRequest request = new AdRequest.Builder().Build();
rewardBasedVideo.LoadAd(request, adUnitId);
rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
}
void Update()
{
if (GameOverScript.GameOver)
{
GetComponent<SpriteRenderer> ().enabled = true;
}
}

void OnMouseDown()
{
showAdd(rewardBasedVideo);
}

private void showAdd(RewardBasedVideoAd rewardBasedVideo)
{
if (rewardBasedVideo.IsLoaded())
{
if (GameOverScript.GameOver)
{
rewardBasedVideo.Show ();
}
}
}

public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
highscore = PlayerPrefs.GetInt ("highscore");

if ((Controll.points + 10) > highscore)
{
Controll.points += 10;
if(Controll.points > highscore)
{
PlayerPrefs.SetInt ("highscore", highscore);
}
}
}
}

最佳答案

如果你看一下这个方法:

private void showAdd(RewardBasedVideoAd rewardBasedVideo)
{
if (rewardBasedVideo.IsLoaded())
{
if (GameOverScript.GameOver)
{
rewardBasedVideo.Show ();
rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
}
}
}

每次调用时,它都会向OnAdRewarded 添加另一个HandleRewardBasedVideoRewarded 的监听器。所以,第一次它会调用一次HandleRewardBasedVideoRewarded;第二次两次;等等。

通常,您只会添加一次监听器:

void Start()
{
rewardBasedVideo = null;
GetComponent<SpriteRenderer> ().enabled = false;
//highscore = PlayerPrefs.GetInt ("highscore");
adUnitId = "ca-app-pub-2**97684242*****/15*08*****";
rewardBasedVideo = RewardBasedVideoAd.Instance;
AdRequest request = new AdRequest.Builder().Build();
rewardBasedVideo.LoadAd(request, adUnitId);

// Add a listener only when the script is loaded
rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
}

private void showAdd(RewardBasedVideoAd rewardBasedVideo)
{
if (rewardBasedVideo.IsLoaded())
{
if (GameOverScript.GameOver)
{
rewardBasedVideo.Show ();
}
}
}

关于c# - unity RewardAd 函数调用更多的时间不只是一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46939667/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com