建立自訂事件

發布商使用 AdMob 中介服務時,可透過自訂事件,為非支援的廣告聯播網新增刊登序列中介服務。本指南說明如何在 Unity 專案中使用為 Android 和 iOS 建立的現有自訂事件。

必要條件

  • 完成入門指南步驟。您的 Unity 應用程式應已匯入 Google 行動廣告 Unity 外掛程式。

  • 已為 Android 和 iOS 建立自訂事件轉接程式。如要建立自訂事件轉接程式,請參閱 AndroidiOS 的自訂事件指南。

定義自訂事件

如要讓自訂事件參與中介服務,必須在 AdMob 網頁介面中定義該事件。在 Android 和 iOS 中介服務群組中新增自訂事件

這張螢幕截圖顯示一些自訂事件設定範例:

如何填寫參數
類別名稱 (iOS)

如果是 iOS,請輸入實作自訂事件的類別名稱。

如果您的類別是透過 Swift 導入,則必須在類別名稱前面加上應用程式 / 架構模組的名稱 (例如 appName.className)。

如果專案中有多個目標,或專案名稱與目標名稱不同,則必須提供目標名稱。如果使用目標名稱,則會如下所示:appName_targetName.className。此外,請記得將任何非英數字元 (例如破折號) 替換為底線。

類別名稱 (Android) 如果是 Android,請確認您為 Class Name 提供的值是 Android 的完整類別名稱 (例如 com.google.ads.mediation.sample.customevent.SampleCustomEvent)。
標籤 輸入事件的專屬名稱。
參數 如要將字串引數傳遞至自訂事件,例如廣告單元 ID。

匯入自訂事件程式庫

自訂事件可能需要納入其他程式庫才能正常運作。舉例來說,您可能需要加入下列程式庫:

  • Android 第三方 SDK
  • Android 第三方自訂事件
  • iOS 第三方廣告 SDK
  • iOS 第三方自訂事件

程式庫類型

您可以透過多種方式將 Android 或 iOS 程式碼匯入 Unity 專案,包括:

  • 使用 Unity 的 External Dependency Manager 匯入預先建構的 Android 或 iOS 構件
  • 匯入 AAR 外掛程式和 Android 程式庫
  • 匯入 Java 和 Kotlin 來源檔案
  • 匯入 iOS 來源檔案和靜態程式庫

視您使用的程式庫封裝方式而定,您可能需要為每個程式庫採用不同的匯入策略。我們稍後會詳細說明每個選項。

(建議) 匯入預先建構的 Android 或 iOS 構件

使用 Unity 的外部依附元件管理工具,從 Maven 或 CocoaPods 匯入預先建構的構件。這個外掛程式隨附於 GoogleMobileAds 外掛程式。

如要匯入現有構件,請建立設定檔來定義匯入作業。檔案名稱和路徑必須符合下列規定:

  • 檔案必須位於 /Editor/ 資料夾中。
  • 檔案名稱結尾必須是 Dependencies.xml

舉例來說,如要匯入假設名為 AdPub 的廣告聯播網自訂事件轉接程式,請建立下列檔案:

Assets/AdPub/Editor/AdPubDependencies.xml

接著,在 AdPubDependencies.xml 檔案中定義依附元件。如要瞭解匯入設定規則,請參閱 External Dependency Manager for Unity 入門指南。下列程式碼片段包含 Android 和 iOS SDK,以及假設「AdPub」廣告聯播網的自訂事件程式庫。

Assets/AdPub/Editor/AdPubDependencies.xml

<dependencies>
  <androidPackages>
    <androidPackage spec="com.adpub.android:adpub-sdk:1.0.0" />
    <androidPackage spec="com.adpub.android:adpub-custom-event:1.0.0">
      <repositories>
        <repository>https://repo.maven.apache.org/maven2/</repository>
        <repository>https://dl.google.com/dl/android/maven2/</repository>
      </repositories>
    </androidPackage>
  </androidPackages>
  <iosPods>
    <iosPod name="AdPubSDK" version="1.0" />
    <iosPod name="AdPubCustomEvent" version="1.0">
      <sources>
        <source>https://github.com/CocoaPods/Specs</source>
      </sources>
    </iosPod>
  </iosPods>
</dependencies>

如果自訂事件構件已依附於必要的廣告聯播網 SDK,您就不需要明確定義 SDK 依附元件:範例

外部依附元件管理工具會自動監控設定變更,並解決依附元件問題。您也可以使用下列選單指令執行手動解析:

Assets > External Dependency Manager > Android Resolver > Force Resolve

匯入 AAR 外掛程式和 Android 程式庫

Unity 支援匯入 *.aar 檔案和 Android 程式庫專案。如果 Android 自訂事件是以這種方式封裝,請參閱「AAR 外掛程式和 Android 程式庫」,瞭解如何在 Unity 專案中加入這些檔案。

匯入 Java 和 Kotlin 來源檔案

從 Unity 2018.2 以上版本開始,如果 Android 自訂事件程式碼包含未編譯的 *.java*.kt 檔案,您可以將 Java 或 Kotlin 來源檔案做為外掛程式

匯入 iOS 來源檔案和靜態程式庫

Unity 支援 *.framework 構件、*.h*.m 來源檔案。如要瞭解如何匯入 iOS 構件和來源檔案,請參閱 Unity 的原生外掛程式指南

使用廣告檢查器測試自訂事件

您可以使用廣告檢查器測試自訂事件是否已正確匯入應用程式。只要使用手勢或以最少的程式碼,即可透過程式輔助開啟廣告檢查器。

(選用) 從 C# 指令碼呼叫第三方 SDK 原生方法

第三方廣告聯播網 SDK 可能有特殊需求,需要直接呼叫 Android 或 iOS 方法。直接呼叫這些方法的程序如下:

  1. 定義平台用戶端的通用介面
  2. 為不受支援的平台實作預設用戶端
  3. 實作 Android 用戶端,用於呼叫 Android 方法
  4. 實作 iOS 用戶端,用於呼叫 iOS 方法
  5. 實作用戶端工廠,有條件地在 iOS 和 Android 用戶端之間切換
  6. 定義 API,存取所有第三方廣告聯播網 SDK 功能

以下章節說明如何針對名為「AdPub」的假設廣告聯播網,在 C# API 中實作這些步驟,以便呼叫 Android 和 iOS 的方法:

Android

package com.adpub.android;

public class AdPubSdk
{
    public static void setHasUserConsent(boolean hasUserConsent);
}

iOS

@interface AdPubSdk : NSObject
+ (void)setHasUserConsent:(BOOL)hasUserConsent;
@end

定義平台用戶端的通用介面

建立 IAdPubClient 介面,其中包含代表基礎 Android 和 iOS API 的方法。

Assets/AdPub/Common/IAdPubClient.cs

namespace AdPub.Common
{
    public interface IAdPubClient
    {
        ///<summary>
        /// Sets a flag indicating if the app has user consent for advertisement.
        ///</summary>
        void SetHasUserConsent(bool hasUserConsent);
    }
}

為不支援的平台定義預設用戶端

建立實作 IAdPubClient 介面的 DefaultClient 類別,只記錄方法名稱。適用於 Unity 編輯器,以及 Android 或 iOS 以外的所有平台。

Assets/AdPub/Common/DefaultClient.cs

namespace AdPub.Common
{
    public class DefaultClient : IAdPubClient
    {
        public void SetHasUserConsent(bool hasUserConsent)
        {
            Debug.Log("SetHasUserConsent was called.");
        }
    }
}

實作 iOS 平台用戶端

在 iOS 上建立實作 IAdPubClient 介面的 iOSAdPubClient 類別。這項實作項目會使用 InteropServices,呼叫 iOS AdPubSdk 類別中的 setHasUserConsent() 方法。

Assets/AdPub/Platforms/iOS/iOSAdPubClient.cs

// Wrap this class in a conditional operator to make sure it only runs on iOS.
#if UNITY_IOS

// Reference InteropServices to include the DLLImportAttribute type.
using System.Runtime.InteropServices;

using AdPub.Common;

namespace AdPub.Platforms.Android
{
    public class iOSAdPubClient : IAdPubClient
    {
        public void SetHasUserConsent(bool hasUserConsent)
        {
            GADUAdPubSetHasUserConsent(hasUserConsent);
        }

        [DllImport("__Internal")]
        internal static extern void GADUAdPubSetHasUserConsent(bool hasUserConsent);
    }
}
#endif

接著,請實作上方定義的 GADUAdPubSetHasUserConsent() 方法。建立 AdPubClientBridge.m,並使用 C 方法 GADUAdPubSetHasUserConsent() 處理來自 Unity 的方法呼叫,然後叫用 AdPubSDK

AdPubClientBridge 是 iOS 來源檔案,必須放在 Plugins/iOS 資料夾中,如 Unity 的原生外掛程式指南所述。

Assets/AdPub/Plugins/iOS/AdPubClientBridge.m

#import <AdPubSDK/AdPubSDK.h>

void GADUAdPubSetHasUserConsent(BOOL hasUserConsent) {
  [AdPubSDK setHasUserConsent:hasUserConsent];
}

實作 Android 平台用戶端

在 Android 上建立實作 IAdPubClient 介面的 AndroidAdPubCient 類別。這項實作作業會使用 Android Java 輔助類別,呼叫 Android 靜態方法 setHasUserConsent()

由於 Android Java 輔助類別僅在 Android 執行階段提供,因此您可以使用 UNITY_ANDROID 編譯器指令包裝類別,如程式碼片段所示,避免發生編譯錯誤。或者,您也可以在 Unity 2017.4 以上版本使用組件定義解決這個問題。

Assets/AdPub/Platforms/Android/AndroidAdPubClient.cs

// Wrap this class in a conditional operator to make sure it only runs on Android.
#if UNITY_ANDROID

// Reference the UnityEngine namespace which contains the JNI Helper classes.
using UnityEngine;

using AdPub.Common;

namespace AdPub.Platforms.Android
{
    public class AndroidAdPubClient : IAdPubClient
    {
        public void SetHasUserConsent(bool hasUserConsent)
        {
             // Make a reference to the com.adpub.AdPubSDK.
            AndroidJavaClass adPubSdk = new AndroidJavaClass("com.adpub.AdPubSdk");

            // Call the native setHasUserConsent method of com.adpub.AdPubSDK.
            adPubSdk.CallStatic("setHasUserConsent", hasUserConsent);
        }
    }
}
#endif

建立工廠方法,傳回正確的用戶端實作項目

現在您已為每個平台實作用戶端,請建立 AdPubClientFactory 類別,根據執行階段平台傳回 IAdPubClient 介面的正確實作項目。這個類別會使用編譯器指令,傳回正確的 IAdPubClientclient。

Assets/AdPub/Common/AdPubClientFactory.cs

namespace AdPub.Common
{
    public class AdPubClientFactory
    {
        // Return the correct platform client.
        public static IAdPubClient GetClient()
        {
#if   !UNITY_EDITOR && UNITY_ANDROID
            return new AdPub.Platforms.Android.AndroidAdPubClient();
#elif !UNITY_EDITOR && UNITY_IOS
            return new AdPub.Platforms.iOS.iOSAdPubClient();
#else
            // Returned for the Unity Editor and unsupported platforms.
            return new DefaultClient();
#endif
        }
    }
}

為每個介面方法定義公開 API

建立 AdPubApi 類別,其中包含 IAdPubClient 介面中每個用戶端方法的呼叫方法。這個類別會使用 AdPubClientFactory 取得 IAdPubClient 的例項,並呼叫該用戶端,以取得基礎 SDK 功能。

Assets/AdPub/AdPubApi.cs

using AdPub.Common;

namespace AdPub
{
    public class AdPubApi
    {
        private static readonly IAdPubClient client = GetAdPubClient();

        // Returns the correct client for the current runtime platform.
        private static IAdPubClient GetAdPubClient()
        {
            return AdPubClientFactory.GetClient();
        }

        // Sets the user consent using the underlying SDK functionality.
        public static void SetHasUserConsent(bool hasUserConsent)
        {
            client.SetHasUserConsent(hasUserConsent);
        }
    }
}

呼叫新定義的 API

以下說明如何呼叫上述 API:

Assets/Scripts/AdPubController.cs

using UnityEngine;
using AdPub;

public class AdPubController : MonoBehaviour
{
    // TODO: Get consent from the user and update this userConsent field.
    public bool userConsent;

    // Called on startup of the GameObject it's assigned to.
    public void Start()
    {
        // Pass the user consent to AdPub.
        AdPubApi.SetHasUserConsent(userConsent);
    }
}

其他第三方廣告聯播網轉接程式範例

如需更多第三方中介服務介面卡的範例,請前往 Google Mobile Ads Unity 外掛程式 GitHub 存放區,瞭解如何實作 C# API,包裝對 iOS 和 Android 方法的呼叫。