简体   繁体   English

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

[英]How to check if Rewarded Ads are available?

I need to check If the rewarded video is Loaded or not, so I have this function : 我需要检查奖励视频是否已加载,所以我具有以下功能:

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

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

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

    return isLoaded;
}

This is RequestRewardBasedVideo function : 这是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);
         }

and this is the function that shows the rewarded video If it Loaded, If not It will show that message to user. 这是显示奖励视频的功能(如果已加载),否则显示该消息给用户。

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

}

But when I run my program in unity, In the console this message keep showing with no stop : 但是,当我统一运行程序时,在控制台中此消息将不断显示:

Dummy IsLoaded
UnityEngine.Debug:Log(Object)

It's like the program keep checking if the reward video is loaded or not. 就像程序不断检查奖励视频是否已加载。

I have tried to delete all the statements in IsAdAvailable() except return isLoaded I replaced it with return true . 我试图删除IsAdAvailable()所有语句,但return isLoaded除外,我将其替换为return true

and the message stopped showing with no stop, It showed just one time. 并且消息停止显示,没有停止,它只显示了一次。 and that's what I want. 这就是我想要的。

So is there a better way to check If the reward video is loaded or not ? 那么,有没有更好的方法来检查奖励视频是否已加载?

If you need more information about my code feel free to ask. 如果您需要有关我的代码的更多信息,请随时询问。

this is my Admob controller script : https://pastebin.com/TNNPKxQF 这是我的Admob控制器脚本: https : //pastebin.com/TNNPKxQF

this is the rewarded video button script : https://pastebin.com/FipHV9wt 这是奖励的视频按钮脚本: https : //pastebin.com/FipHV9wt

this is the Rewarded Video CallBack script : https://pastebin.com/g65zjBwt 这是Rewarded Video CallBack脚本: https : //pastebin.com/g65zjBwt

在此处输入图片说明 在此处输入图片说明 在此处输入图片说明

#edit #编辑

I deleted all the statements from IsAdAvailable() exept AdmobController.instance.RequestRewardBasedVideo(); 我从IsAdAvailable()删除了所有语句, IsAdAvailable() AdmobController.instance.RequestRewardBasedVideo(); and return true and I run my program and the problem still exist, the message Dummy IsLoaded keep showing with no stop. return true然后我运行我的程序,问题仍然存在,消息Dummy IsLoaded不断显示,没有停止。 so I think the problem from this statement AdmobController.instance.RequestRewardBasedVideo(); 所以我认为此语句AdmobController.instance.RequestRewardBasedVideo(); .

You can read how to implement rewarded video ads in the Google AdMob Mobile Ads SDK (Unity) documentation. 您可以在Google AdMob移动广告SDK(统一)文档中阅读如何实施奖励视频广告

The best way to check if the reward video is loaded is by hooking into RewardBasedVideoAd OnAdLoaded event: 检查奖励视频是否已加载的最好方法是,钩入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);
    }

You should also use events to check if the ad was shown, closed, clicked etc. 您还应该使用事件检查广告是否已显示,关闭,点击等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM