リワード広告

リワード広告は、ユーザーが広告を操作することと引き換えに利用できる広告である 獲得できます。このガイドでは、Google Play Console からリワード広告を AdMob を Unity アプリに統合できます。

このガイドでは、リワード広告を Unity アプリに統合する方法について説明します。

前提条件

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

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

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

/6499/example/rewarded

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() RewardedAd クラス。読み込まれた RewardedAd オブジェクトは、 パラメータを使用します。以下の例は、Python や Curl で RewardedAd


  // This ad unit is configured to always serve test ads.
  private string _adUnitId = "/6499/example/rewarded";

  private RewardedAd _rewardedAd;

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

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

      // create our request used to load the ad.
      var adRequest = new AdManagerAdRequest();

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

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

              _rewardedAd = ad;
          });
  }

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

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

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

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

    // If the operation completed successfully, no error is returned.
    Debug.Log("Rewarded 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 ShowRewardedAd()
{
    const string rewardMsg =
        "Rewarded ad rewarded the user. Type: {0}, amount: {1}.";

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

リワード広告イベントをリッスンする

広告の動作をさらにカスタマイズするには、 広告のライフサイクルにおけるイベント(開始、終了など)です。聞き取る内容 これらのイベントを処理するには、次のようにデリゲートを登録します。

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

リワード広告をクリーンアップする

RewardedAd が終了したら、必ず Destroy() を呼び出します。 メソッドを参照してください。

_rewardedAd.Destroy();

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

次のリワード広告をプリロードする

RewardedAd は使い捨てオブジェクトです。つまりリワード広告が表示されると そのオブジェクトを再度使用することはできません。別のリワード広告をリクエストするには、 新しい RewardedAd オブジェクトを作成する必要があります。

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

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

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

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

参考情報