リワード インタースティシャル(ベータ版)

リワード インタースティシャルは 報酬型広告フォーマットで、表示された広告に対して報酬を提供できる 自動的に最適化されます。リワード広告とは異なり、ユーザーは オプトイン インタースティシャルを表示する必要があります。

前提条件

必ずテスト広告でテストする

次のサンプルコードには、広告ユニット ID が含まれています。この ID を使用して、 テスト広告。これは、特定の要素の代わりにテスト広告を返すように 安全に使用できます。

ただし、 独自の広告ユニットを作成し アプリで使用する ID。デバイスをテスト用として明示的に設定します デバイス 必要があります。

Android

ca-app-pub-3940256099942544/5354046379

iOS

ca-app-pub-3940256099942544/6978759866

Mobile Ads SDK を初期化する

広告を読み込む前に、以下を呼び出して Mobile Ads SDK を初期化します。 MobileAds.Initialize()。この処理は 1 回だけ行います(アプリの起動時に行うのが理想的です)。

using GoogleMobileAds;
using GoogleMobileAds.Api;

public class GoogleMobileAdsDemoScript : MonoBehaviour
{
    public void Start()
    {
        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize((InitializationStatus initStatus) =>
        {
            // This callback is called once the MobileAds SDK is initialized.
        });
    }
}

メディエーションを使用している場合は、コールバックが発生するのを待ってから、広告を これにより、すべてのメディエーション アダプタが確実に初期化されます。

実装

リワード インタースティシャル広告を実装する主な手順は次のとおりです。

  1. リワード インタースティシャル広告を読み込む
  2. [省略可] サーバー側の検証(SSV)コールバックを検証する
  3. リワード インタースティシャル広告と報酬コールバックを表示する
  4. リワード インタースティシャル広告のイベントをリッスンする
  5. リワード インタースティシャル広告をクリーンアップする
  6. 次のリワード インタースティシャル広告をプリロードする

リワード インタースティシャル広告を読み込む

リワード インタースティシャル広告の読み込みは、静的な Load() を使って行います。 メソッドを RewardedInterstitialAd クラスのメソッドに渡します。読み込みメソッドには広告が必要 ユニット ID、AdRequest オブジェクト、コンプリーション このハンドラは、広告の読み込みが成功または失敗したときに呼び出されます。読み込まれた RewardedInterstitialAd オブジェクトは、完了時にパラメータとして提供されます。 使用します。以下の例は、RewardedInterstitialAd を読み込む方法を示しています。


  // These ad units are configured to always serve test ads.
#if UNITY_ANDROID
  private string _adUnitId = "ca-app-pub-3940256099942544/5354046379";
#elif UNITY_IPHONE
  private string _adUnitId = "ca-app-pub-3940256099942544/6978759866";
#else
  private string _adUnitId = "unused";
#endif

  private RewardedInterstitialAd _rewardedInterstitialAd;

  /// <summary>
  /// Loads the rewarded interstitial ad.
  /// </summary>
  public void LoadRewardedInterstitialAd()
  {
      // Clean up the old ad before loading a new one.
      if (_rewardedInterstitialAd != null)
      {
            _rewardedInterstitialAd.Destroy();
            _rewardedInterstitialAd = null;
      }

      Debug.Log("Loading the rewarded interstitial ad.");

      // create our request used to load the ad.
      var adRequest = new AdRequest();
      adRequest.Keywords.Add("unity-admob-sample");

      // send the request to load the ad.
      RewardedInterstitialAd.Load(_adUnitId, adRequest,
          (RewardedInterstitialAd ad, LoadAdError error) =>
          {
              // if error is not null, the load request failed.
              if (error != null || ad == null)
              {
                  Debug.LogError("rewarded interstitial ad failed to load an ad " +
                                 "with error : " + error);
                  return;
              }

              Debug.Log("Rewarded interstitial ad loaded with response : "
                        + ad.GetResponseInfo());

              _rewardedInterstitialAd = ad;
          });
  }

[省略可] サーバー側の検証(SSV)コールバックを検証する

サーバー側の検証で追加データを必要とするアプリ コールバックでは、リワード インタースティシャル広告のカスタムデータ機能を使用する必要があります。 リワード広告オブジェクトに設定されている文字列値が custom_data に渡されます。 クエリ パラメータを渡します。カスタムデータ値が設定されていない場合、 custom_data クエリ パラメータ値は SSV コールバックに含まれません。

次のコードサンプルは、コマンドの後に SSV オプションを設定する方法を示しています。 リワード インタースティシャル広告が読み込まれました。

// send the request to load the ad.
RewardedInterstitialAd.Load(_adUnitId,
                            adRequest,
                            (RewardedInterstitialAd ad, LoadAdError error) =>
    {
        // If the operation failed, an error is returned.
        if (error != null || ad == null)
        {
            Debug.LogError("Rewarded interstitial ad failed to load an ad " +
                           " with error : " + error);
            return;
        }

        // If the operation completed successfully, no error is returned.
        Debug.Log("Rewarded interstitial ad loaded with response : " +
                   ad.GetResponseInfo());
        
        // Create and pass the SSV options to the rewarded ad.
        var options = new ServerSideVerificationOptions
                              .Builder()
                              .SetCustomData("SAMPLE_CUSTOM_DATA_STRING")
                              .Build()
        ad.SetServerSideVerificationOptions(options);
        
});

カスタムのリワード文字列を設定する場合は、 表示されます。

リワード インタースティシャル広告とリワード コールバックを表示する

広告を表示する際に、 できます。広告は読み込みごとに 1 回だけ表示できます。CanShowAd() メソッドを使用して以下を行います。 広告を表示できる状態であることを確認します。

次のコードは、リワード広告を表示するのに最適な方法を示しています。 インタースティシャル広告です。

public void ShowRewardedInterstitialAd()
{
    const string rewardMsg =
        "Rewarded interstitial ad rewarded the user. Type: {0}, amount: {1}.";

    if (rewardedInterstitialAd != null && rewardedInterstitialAd.CanShowAd())
    {
        rewardedInterstitialAd.Show((Reward reward) =>
        {
            // TODO: Reward the user.
            Debug.Log(String.Format(rewardMsg, reward.Type, reward.Amount));
        });
    }
}

リワード インタースティシャル広告のイベントをリッスンする

広告の動作をさらにカスタマイズするには、 広告のライフサイクルで測定されます。委任を登録してこれらのイベントをリッスンする 次のように指定します。

private void RegisterEventHandlers(RewardedInterstitialAd ad)
{
    // Raised when the ad is estimated to have earned money.
    ad.OnAdPaid += (AdValue adValue) =>
    {
        Debug.Log(String.Format("Rewarded interstitial ad paid {0} {1}.",
            adValue.Value,
            adValue.CurrencyCode));
    };
    // Raised when an impression is recorded for an ad.
    ad.OnAdImpressionRecorded += () =>
    {
        Debug.Log("Rewarded interstitial ad recorded an impression.");
    };
    // Raised when a click is recorded for an ad.
    ad.OnAdClicked += () =>
    {
        Debug.Log("Rewarded interstitial ad was clicked.");
    };
    // Raised when an ad opened full screen content.
    ad.OnAdFullScreenContentOpened += () =>
    {
        Debug.Log("Rewarded interstitial ad full screen content opened.");
    };
    // Raised when the ad closed full screen content.
    ad.OnAdFullScreenContentClosed += () =>
    {
        Debug.Log("Rewarded interstitial ad full screen content closed.");
    };
    // Raised when the ad failed to open full screen content.
    ad.OnAdFullScreenContentFailed += (AdError error) =>
    {
        Debug.LogError("Rewarded interstitial ad failed to open " +
                       "full screen content with error : " + error);
    };
}

リワード インタースティシャル広告をクリーンアップする

RewardedInterstitialAd の使用が終了したら、必ず Destroy() メソッドを次のように作成します。

_rewardedInterstitialAd.Destroy();

これにより、オブジェクトが使用されなくなったことがプラグインに通知されます。また、 解放されますこのメソッドを呼び出さないと、メモリリークが発生します。

次のリワード インタースティシャル広告をプリロードする

RewardedInterstitialAd は使い捨てオブジェクトです。つまり このオブジェクトは再使用できません。もう一度リクエストするには 新しい RewardedInterstitialAd を読み込む必要があります。 渡されます。

次のインプレッションの機会に備えてリワード インタースティシャル広告を準備するには、 リワード インタースティシャル広告が OnAdFullScreenContentClosed または OnAdFullScreenContentFailed の広告イベントが発生します。

private void RegisterReloadHandler(RewardedInterstitialAd ad)
{
    // Raised when the ad closed full screen content.
    ad.OnAdFullScreenContentClosed += ()
    {
        Debug.Log("Rewarded interstitial ad full screen content closed.");

        // Reload the ad so that we can show another as soon as possible.
        LoadRewardedInterstitialAd();
    };
    // Raised when the ad failed to open full screen content.
    ad.OnAdFullScreenContentFailed += (AdError error) =>
    {
        Debug.LogError("Rewarded interstitial ad failed to open " +
                       "full screen content with error : " + error);

        // Reload the ad so that we can show another as soon as possible.
        LoadRewardedInterstitialAd();
    };
}

参考情報