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 Unity Ads Monetization dashboard by selecting CURRENT PROJECT > Project Settings from the secondary navigation menu.

In your game script, you 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 project’s run-time life cycle before you need to load an ad. For 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.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, specifically the Settings page of your project. 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, refer to the documentation on the IUnityAdsInitializationListener API.

Next steps: To continue your integration, refer to the Implementing basic ads in Android documentation.