Implementing rewarded ads in Android

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

To reward users 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

Note: Unity Ads requires access to the currently running Activity, so the following example uses getApplicationContext(). Because this might not be suitable for all implementations, some customization might be required (depending on the integration).

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);
  }
}

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 following example code 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, refer to the Android developer documentation on Buttons.

After you have implemented your button, add a script with the following code, where showAdButton is a button configured in the View:

Note: Unity Ads requires access to the currently running Activity, so the following example uses getApplicationContext(). Because this might not be suitable for all implementations, some customization might be required (depending on the integration).

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);
  }
}

Next steps: Refer to Implementing banner ads in Android.