简体   繁体   中英

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 :

         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 .

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

this is the rewarded video button script : https://pastebin.com/FipHV9wt

this is the Rewarded Video CallBack script : https://pastebin.com/g65zjBwt

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

#edit

I deleted all the statements from IsAdAvailable() exept 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. so I think the problem from this statement AdmobController.instance.RequestRewardBasedVideo(); .

You can read how to implement rewarded video ads in the Google AdMob Mobile Ads SDK (Unity) documentation.

The best way to check if the reward video is loaded is by hooking into RewardBasedVideoAd OnAdLoaded event:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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