[AdMob]リワード広告を2回以上表示する

アプリ開発

[AdMob]リワード広告を2回以上表示する

ボタンを押したら流れる広告が2回目以降ボタンを押しても流れない

本記事の内容

リワード広告とは?

実装間違いによって起こる現象

正しい実装方法

リワード広告とは?

広告の視聴を選択したユーザーに報酬として特典を付与する全画面広告フォーマットです。リワード インタースティシャル広告とは異なり、ユーザーは広告の視聴を選択する必要があります。

Google AdMob

実装間違いによって起こる現象

例えば、RewardAdのインスタンスをStart()で生成し、必要なところでShowRewardAd()を呼ぶことでリワード広告を表示しようとすると、
1回目はうまく表示されるが、2回目以降はこのメソッドを呼んでもリワード広告は表示されない。

    public void Start()
    {
        // Clean up the old ad before loading a new one.
        if (rewardedAd != null)
        {
              rewardedAd.Destroy();
              rewardedAd = null;
        }
        string adUnitId = "";
        this.rewardedAd = new RewardedAd(adUnitId);

        // Called when an ad request has successfully loaded.
        this.rewardedAd.OnAdLoaded += HandleRewardedAdLoaded;
        // Called when an ad request failed to load.
        this.rewardedAd.OnAdFailedToLoad += HandleRewardedAdFailedToLoad;
        // Called when an ad is shown.
        this.rewardedAd.OnAdOpening += HandleRewardedAdOpening;
        // Called when an ad request failed to show.
        this.rewardedAd.OnAdFailedToShow += HandleRewardedAdFailedToShow;
        // Called when the user should be rewarded for interacting with the ad.
        this.rewardedAd.OnUserEarnedReward += HandleUserEarnedReward;
        // Called when the ad is closed.
        this.rewardedAd.OnAdClosed += HandleRewardedAdClosed;

        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder().Build();
        // Load the rewarded ad with the request.
        this.rewardedAd.LoadAd(request);
    }
    public void ShowRewardAd(UnityEvent onShowAdsEvent)
    {
        if (this.rewardBasedVideoAd == null)
        {
            return;
        }

        if (rewardBasedVideoAd.IsLoaded())
        {
            if (onShowAdsEvent != null)
                onShowAdsEvent.Invoke();
            this.rewardBasedVideoAd.Show();
        }
    }

正しい実装方法

ReawardAdは使い捨てなので、毎回インスタンスを作り直してあげる必要がある。

    public void Start()
    {
        this.RequestRewardBasedVideoAd();
    }
    private void RequestRewardBasedVideoAd()
    {
        // Clean up the old ad before loading a new one.
        if (rewardedAd != null)
        {
            rewardedAd.Destroy();
            rewardedAd = null;
        }
        string adUnitId = "";
        this.rewardedAd = new RewardedAd(adUnitId);

        // Called when an ad request has successfully loaded.
        this.rewardedAd.OnAdLoaded += HandleRewardedAdLoaded;
        // Called when an ad request failed to load.
        this.rewardedAd.OnAdFailedToLoad += HandleRewardedAdFailedToLoad;
        // Called when an ad is shown.
        this.rewardedAd.OnAdOpening += HandleRewardedAdOpening;
        // Called when an ad request failed to show.
        this.rewardedAd.OnAdFailedToShow += HandleRewardedAdFailedToShow;
        // Called when the user should be rewarded for interacting with the ad.
        this.rewardedAd.OnUserEarnedReward += HandleUserEarnedReward;
        // Called when the ad is closed.
        this.rewardedAd.OnAdClosed += HandleRewardedAdClosed;

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

    // 現在は OnAdFullScreenContentClosed の使用がお勧め
    private void HandleRewardedAdClosed(object sender, EventArgs args)
    {
        RequestRewardBasedVideoAd();
    }
    // 現在は OnAdFullScreenContentFailed の使用がお勧め
    private void HandleRewardedAdFailedToShow(object sender, EventArgs args)
    {
        RequestRewardBasedVideoAd();
    }
    public void ShowRewardAd(UnityEvent onShowAdsEvent)
    {
        if (this.rewardBasedVideoAd == null)
        {
            return;
        }

        if (rewardBasedVideoAd.IsLoaded())
        {
            if (onShowAdsEvent != null)
                onShowAdsEvent.Invoke();
            this.rewardBasedVideoAd.Show();
        }
    }

インスタンスを作り直すとしても、Destroy()を呼ぶのは必要です。呼ばないとメモリリークが発生します。

-アプリ開発
-, ,