Implementing rewarded ads in Android

Rewarding players for watching ads increases user engagement, resulting in higher revenue. For example, games may reward players with in-game currency, consumables, additional lives, or experience-multipliers. For more information on how to effectively design your rewarded ads, see the monetization strategy guide.

To reward players for completing a video ad, use the IUnityAdsShowListener.onUnityAdsShowComplete listener callback method’s UnityAdsShowCompletionState result to check if the user finished watching the ad and should be rewarded.

Rewarded video example

Copy
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 once the ad has been loaded.
  public void DisplayRewardedAd () {
     UnityAds.load(adUnitId, loadListener);
  }
}

Rewarded video ad buttons

Using a button to allow the player to opt in to watching an ad is a common implementation for rewarded video ads. Use the example code below to create a rewarded ads button. The ads button displays an ad when pressed, as long as an ad has loaded first. For information on configuring buttons, see Android's documentation.

Once you've implemented your button, add a script with the following code, where showAdButton is a button configured in the View:

Copy
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 once the ad has been loaded.
  public void DisplayRewardedAd () {
     UnityAds.load(adUnitId, loadListener);
  }
}

Next steps: See documentation on implementing banner ads in Android.