Initializing the SDK in Android

To initialize the SDK, you must reference your Project’s Game ID for the appropriate platform. You can locate the ID on the Monetization dashboard by selecting CURRENT PROJECT > Project Settings from the secondary navigation menu.

In your game script, you’ll need to implement an IUnityAdsInitializationListener interface that handles initialization callbacks. The initialize method to initialize the SDK requires this listener as a parameter. Initialize the SDK early in your game’s run-time life cycle, before you need to load an ad. For example:

Copy
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import com.unity3d.ads.example.R;

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

public class InitializeAdsScript extends AppCompatActivity implements IUnityAdsInitializationListener {

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

  @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() {

  }

  @Override
  public void onInitializationFailed(UnityAds.UnityAdsInitializationError error, String message) {

  }
}

For the initialize function, the context parameter is the current Android Context. The unityGameID parameter is the Unity Game ID for your project, found in the Monetization dashboard. The IUnityAdsInitializationListener is the listener for the result of the initialization call. The true Boolean indicates that the game is in test mode, and will only show test ads.

Note: You must implement each of the callback methods in the listener interface, even if they are empty functions for now. You will populate them with the appropriate logic where needed in the following sections. For more information on each listener callback method, see documentation on IUnityAdsInitializationListener API.

Next steps: See documentation on implementing basic ads in Android to continue your integration.