在 Android 中初始化 SDK

要初始化 SDK,您必须为相应的平台引用项目的 Game ID。您可以在 Unity Ads Monetization 控制面板 上找到 ID,方法是从辅助导航菜单中选择 **当前项目** > **项目设置**。

在您的游戏脚本中,您需要实现一个 IUnityAdsInitializationListener 接口来处理初始化回调。初始化 SDK 的 initialize 方法需要此监听器作为参数。在您需要加载广告之前,在项目的运行时生命周期中尽早初始化 SDK。例如

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

  }
}

对于 initialize 函数,context 参数是当前 Android ContextunityGameID 参数是您的项目的 Unity Game ID,可在 Monetization 控制面板 中找到,具体而言是您项目中的设置页面。IUnityAdsInitializationListener 是初始化调用结果的监听器。true 布尔值表示游戏处于测试模式,并且只会显示测试广告。

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.

下一步:要继续集成,请参阅 在 Android 中实现基本广告 文档。