如何查看奖励广告是否可用?

时间:2019-01-30 10:22:37

标签: c# unity3d console admob ads

我需要检查奖励视频是否已加载,所以我具有此功能:

private bool IsAdAvailable()
{
    if (AdmobController.instance.rewardBasedVideo == null) return false;

    bool isLoaded = AdmobController.instance.rewardBasedVideo.IsLoaded();

    if (!isLoaded)
    {
        AdmobController.instance.RequestRewardBasedVideo();
    }

    return isLoaded;
}

这是RequestRewardBasedVideo函数:

         public void RequestRewardBasedVideo()
         {
#if UNITY_ANDROID
               string adUnitId = "ca-app-pub-3940256099942544/5224354917";
#elif UNITY_IPHONE
               string adUnitId = "ca-app-pub-3940256099942544/1712485313";
#else
               string adUnitId = "unexpected_platform";
#endif

              // Create an empty ad request.
              AdRequest request = new AdRequest.Builder().Build();
             // Load the rewarded video ad with the request.
              this.rewardBasedVideo.LoadAd(request, adUnitId);
         }

这是显示奖励视频的功能(如果已加载),否则,将向用户显示该消息。

public void ShowRewardedAds()
{
    if (rewardBasedVideo.IsLoaded())
    {
        rewardBasedVideo.Show();
    }
    else
    {
        MonoBehaviour.print("Reward based video ad is not ready yet");
    }

}

但是当我统一运行我的程序时,在控制台中,此消息不断显示:

Dummy IsLoaded
UnityEngine.Debug:Log(Object)

就像程序不断检查奖励视频是否已加载。

我尝试删除IsAdAvailable()中的所有语句,但用return isLoaded代替了return true

,消息停止显示,没有停止,它只显示了一次。 这就是我想要的。

有没有更好的方法检查奖励视频是否已加载?

如果您需要有关我的代码的更多信息,请随时询问。

这是我的Admob控制器脚本: https://pastebin.com/TNNPKxQF

这是奖励视频按钮脚本: https://pastebin.com/FipHV9wt

这是“奖励视频回叫”脚本: https://pastebin.com/g65zjBwt

enter image description here enter image description here enter image description here

#edit

我删除了IsAdAvailable()部门的所有陈述  AdmobController.instance.RequestRewardBasedVideo();return true 并且我运行我的程序,问题仍然存在,消息Dummy IsLoaded不断显示,没有停止。 因此,我认为此语句AdmobController.instance.RequestRewardBasedVideo();中的问题。

1 个答案:

答案 0 :(得分:1)

您可以在Google AdMob Mobile Ads SDK (Unity) documentation.

中了解如何实施奖励视频广告

检查奖励视频是否已加载的最佳方法是通过连接到RewardBasedVideoAd OnAdLoaded事件:

using GoogleMobileAds.Api;
...
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
    private RewardBasedVideoAd rewardBasedVideo;
    ...

    public void Start()
    {
        // Get singleton reward based video ad reference.
        this.rewardBasedVideo = RewardBasedVideoAd.Instance;

        // Called when an ad request has successfully loaded.
        rewardBasedVideo.OnAdLoaded += HandleRewardBasedVideoLoaded;
        // Called when an ad request failed to load.
        rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoad;

        this.RequestRewardBasedVideo();
    }

    private void RequestRewardBasedVideo()
    {
        #if UNITY_ANDROID
            string adUnitId = "ca-app-pub-3940256099942544/5224354917";
        #elif UNITY_IPHONE
            string adUnitId = "ca-app-pub-3940256099942544/1712485313";
        #else
            string adUnitId = "unexpected_platform";
        #endif

        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder().Build();
        // Load the rewarded video ad with the request.
        this.rewardBasedVideo.LoadAd(request, adUnitId);
    }

    public void HandleRewardBasedVideoLoaded(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleRewardBasedVideoLoaded event received");
    }

    public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        MonoBehaviour.print(
            "HandleRewardBasedVideoFailedToLoad event received with message: "
                             + args.Message);
    }

您还应该使用事件检查广告是否显示,关闭,点击等。