Android でのリワード広告の実装

広告を視聴したユーザーに報酬を与えると、ユーザーエンゲージメントが向上し、収益の増加につながります。例えば、ユーザーに与える報酬には、ゲーム内通貨、消耗品、追加のライフ、経験値の倍増などがあります。リワード広告を効果的にデザインする方法の詳細については、収益化戦略 ガイドを参照してください。

動画広告を最後まで視聴したユーザーに報酬を与えるには、IUnityAdsShowListener.onUnityAdsShowComplete リスナーコールバックメソッドの UnityAdsShowCompletionState の結果を使用して、ユーザーが広告の視聴を完了し、報酬を受け取る資格を得ているかどうかを確認します。

動画リワードの例

[!include]

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import com.unity3d.ads.IUnityAdsLoadListener;
import com.unity3d.ads.IUnityAdsShowListener;
import com.unity3d.ads.UnityAdsShowOptions;
import com.unity3d.ads.example.R;

import com.unity3d.ads.IUnityAdsInitializationListener;
import com.unity3d.ads.UnityAds;

public class ShowRewardedAd extends AppCompatActivity implements IUnityAdsInitializationListener  {

  private String unityGameID = "1234567";
  private Boolean testMode = true;
  private String adUnitId = "rewardedVideo";

  private IUnityAdsLoadListener loadListener = new IUnityAdsLoadListener() {
     @Override
     public void onUnityAdsAdLoaded(String placementId) {
        UnityAds.show((Activity)getApplicationContext(), adUnitId, new UnityAdsShowOptions(), showListener);
     }

     @Override
     public void onUnityAdsFailedToLoad(String placementId, UnityAds.UnityAdsLoadError error, String message) {
        Log.e("UnityAdsExample", "Unity Ads failed to load ad for " + placementId + " with error: [" + error + "] " + message);
     }
  };

  private IUnityAdsShowListener showListener = new IUnityAdsShowListener() {
     @Override
     public void onUnityAdsShowFailure(String placementId, UnityAds.UnityAdsShowError error, String message) {
        Log.e("UnityAdsExample", "Unity Ads failed to show ad for " + placementId + " with error: [" + error + "] " + message);
     }

     @Override
     public void onUnityAdsShowStart(String placementId) {
        Log.v("UnityAdsExample", "onUnityAdsShowStart: " + placementId);
     }

     @Override
     public void onUnityAdsShowClick(String placementId) {
        Log.v("UnityAdsExample", "onUnityAdsShowClick: " + placementId);
     }

     @Override
     public void onUnityAdsShowComplete(String placementId, UnityAds.UnityAdsShowCompletionState state) {
        Log.v("UnityAdsExample", "onUnityAdsShowComplete: " + placementId);
        if (state.equals(UnityAds.UnityAdsShowCompletionState.COMPLETED)) {
           // Reward the user for watching the ad to completion
        } else {
           // Do not reward the user for skipping the ad
        }
     }
  };

  @Override
  protected void onCreate (Bundle savedInstanceState) {
     super.onCreate (savedInstanceState);
     setContentView (R.layout.activity_main);
     // Initialize the SDK:
     UnityAds.initialize(getApplicationContext(), unityGameID, testMode, this);
  }

  @Override
  public void onInitializationComplete() {
     DisplayRewardedAd();
  }

  @Override
  public void onInitializationFailed(UnityAds.UnityAdsInitializationError error, String message) {
     Log.e("UnityAdsExample", "Unity Ads initialization failed with error: [" + error + "] " + message);
  }

  // Implement a function to load a rewarded ad. The ad will start to show after the ad has been loaded.
  public void DisplayRewardedAd () {
     UnityAds.load(adUnitId, loadListener);
  }
}

動画リワード広告ボタン

動画リワード広告では、プレイヤーが広告の視聴を選択できるボタンを実装するのが一般的です。リワード広告ボタンを作成するには、以下のコード例を使用します。この広告ボタンを押すと、広告が初めてロードされたときに限り、広告が表示されます。ボタンの設定については、ボタン に関する Android 開発者向けドキュメントを参照してください。

ボタンを実装したら、以下のコードを使用してスクリプトを追加します。showAdButton はビューで設定されたボタンを示します。

[!include]

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import com.unity3d.ads.IUnityAdsLoadListener;
import com.unity3d.ads.IUnityAdsShowListener;
import com.unity3d.ads.UnityAdsShowOptions;
import com.unity3d.ads.example.R;

import com.unity3d.ads.IUnityAdsInitializationListener;
import com.unity3d.ads.UnityAds;

public class RewardedAdButton extends AppCompatActivity implements IUnityAdsInitializationListener  {

  private String unityGameID = "1234567";
  private Boolean testMode = true;
  private String adUnitId = "rewardedVideo";
  private Button rewardedButton;

  private IUnityAdsLoadListener loadListener = new IUnityAdsLoadListener() {
     @Override
     public void onUnityAdsAdLoaded(String placementId) {
        UnityAds.show((Activity)getApplicationContext(), adUnitId, new UnityAdsShowOptions(), showListener);
     }

     @Override
     public void onUnityAdsFailedToLoad(String placementId, UnityAds.UnityAdsLoadError error, String message) {
        Log.e("UnityAdsExample", "Unity Ads failed to load ad for " + placementId + " with error: [" + error + "] " + message);
     }
  };

  private IUnityAdsShowListener showListener = new IUnityAdsShowListener() {
     @Override
     public void onUnityAdsShowFailure(String placementId, UnityAds.UnityAdsShowError error, String message) {
        Log.e("UnityAdsExample", "Unity Ads failed to show ad for " + placementId + " with error: [" + error + "] " + message);
        // Re-enable the button if the user should be allowed to watch another rewarded ad
        rewardedButton.setEnabled(true);
     }

     @Override
     public void onUnityAdsShowStart(String placementId) {
        Log.v("UnityAdsExample", "onUnityAdsShowStart: " + placementId);
     }

     @Override
     public void onUnityAdsShowClick(String placementId) {
        Log.v("UnityAdsExample", "onUnityAdsShowClick: " + placementId);
     }

     @Override
     public void onUnityAdsShowComplete(String placementId, UnityAds.UnityAdsShowCompletionState state) {
        Log.v("UnityAdsExample", "onUnityAdsShowComplete: " + placementId);
        if (state.equals(UnityAds.UnityAdsShowCompletionState.COMPLETED)) {
           // Reward the user for watching the ad to completion
        } else {
           // Do not reward the user for skipping the ad
        }
        // Re-enable the button if the user should be allowed to watch another rewarded ad
        rewardedButton.setEnabled(true);
     }
  };

  @Override
  protected void onCreate (Bundle savedInstanceState) {
     super.onCreate (savedInstanceState);
     setContentView (R.layout.activity_main);

     // Initialize the SDK:
     UnityAds.initialize(getApplicationContext(), unityGameID, testMode, this);

     // Find the button in the view hierarchy and set its click function to load ads:
     rewardedButton = findViewById (R.id.showAdButton);
     rewardedButton.setOnClickListener (new View.OnClickListener () {
        @Override
        public void onClick (View v) {
           DisplayRewardedAd ();
           rewardedButton.setEnabled(false);
        }
     });
  }

  @Override
  public void onInitializationComplete() {
     rewardedButton.setEnabled(true);
  }

  @Override
  public void onInitializationFailed(UnityAds.UnityAdsInitializationError error, String message) {
     Log.e("UnityAdsExample", "Unity Ads initialization failed with error: [" + error + "] " + message);
  }

// Implement a function to load an rewarded ad. The ad will start to show after the ad has been loaded.
  public void DisplayRewardedAd () {
     UnityAds.load(adUnitId, loadListener);
  }
}

次のステップ: Android でのバナー広告の実装 に関するドキュメントを参照します。