通用設定

MobileAds 類別提供 Google Mobile Ads Unity Plugin 的全域設定。

在 Unity 主執行緒上觸發廣告事件

Google Mobile Ads Unity Plugin 觸發事件的執行緒與 Unity 主執行緒不同。如果您導入廣告事件並與 Unity 物件互動,則需要將 Google Mobile Ads Unity Plugin 事件與 Unity 主執行緒同步。

手動或自動同步處理廣告事件與 Unity 主執行緒,讓 Google Mobile Ads Unity Plugin 處理同步作業。

建議做法:手動同步處理廣告事件

如要手動同步處理廣告事件,請在主執行緒上使用 ExecuteInUpdate 方法。與 UnityEngine 物件互動時,如果 RaiseAdEventsOnUnityMainThread 屬性已停用,請使用 ExecuteInUpdate 方法。

以下範例會記錄背景執行緒,並執行動作與 UnityEngine 物件互動:

// Google Mobile Ads events are raised off the Unity main thread.

// This log is executed off the Unity main thread.
// Write all time-sensitive code before ExecuteInUpdate().
Debug.Log("Executing off the Unity main thread.");

// Use ExecuteInUpdate to run code on the main thread, allowing you to
// interact with Unity UI and GameObjects.
// Changed to fully-qualified name to resolve CS0103
GoogleMobileAds.Common.MobileAdsEventExecutor.ExecuteInUpdate(() =>
{
    // This callback may be delayed on Android until the user returns to the app.
    Debug.Log("Executing on the Unity main thread.");

    // Place all code that interacts with Unity UI and GameObjects inside this callback.
    if (_myGameObject != null)
    {
        _myGameObject.SetActive(true);
    }
});

自動同步廣告事件

如要讓 Google Mobile Ads Unity Plugin 同步處理廣告事件,請將 MobileAds.RaiseAdEventsOnUnityMainThread 屬性設為 true:

...
using GoogleMobileAds.Api;
...
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
    public void Start()
    {
        // When true all events raised by GoogleMobileAds will be raised
        // on the Unity main thread. The default value is false.
        MobileAds.RaiseAdEventsOnUnityMainThread = true;
    }
}

影片廣告音量控制選項

如果應用程式有專屬的音量控制項 (例如自訂音樂或音效音量),請將應用程式音量提供給 Google Mobile Ads Unity Plugin,讓影片廣告採用應用程式音量設定。確保使用者接收到影片廣告時,廣告的音量符合預期。

裝置音量鍵或 OS 層級的音量滑桿,可控制裝置音訊輸出的音量。不過,應用程式可以根據裝置音量獨立調整音量,打造專屬的音訊體驗。

您可以在載入廣告前呼叫 SetApplicationVolume() 方法,向 Google Mobile Ads Unity Plugin 回報應用程式的相對音量。有效廣告音量值的範圍為 0.0 (靜音) 到 1.0 (目前裝置音量)。以下範例說明如何向 SDK 回報相對應用程式音量:

// Set app volume to be half of current device volume.
MobileAds.SetApplicationVolume(0.5f);

如要通知 SDK 應用程式音量已靜音,請在載入廣告前呼叫 SetApplicationMuted() 方法:

// Set app to be muted.
MobileAds.SetApplicationMuted(true);

根據預設,應用程式音量會設為 1 (目前裝置音量),且應用程式不會設為靜音。

如果應用程式有特殊需求,您可以將選用的 ApplicationPreferences 鍵 gad_has_consent_for_cookies 設為零,啟用受限制的廣告:

// Enable limited ads
ApplicationPreferences.SetInt("gad_has_consent_for_cookies", 0);

Android 壓縮

這個 Unity 發布選項可讓您啟用 Java 程式碼壓縮。如果啟用壓縮功能,您也需要建立自訂 ProGuard 檔案,保留 SDK 參照的類別。

  1. 啟用自訂 Proguard 檔案

    依序前往「Project Settings」>「Player」>「Android」>「Publishing Settings」>「Build」,然後選取:

    • 自訂 Proguard 檔案
  2. 開啟 /Assets/Plugins/Android/proguard-user.txt 並新增下列內容:

-keep class com.google.** { public *; }

停用當機報告

Google Mobile Ads Unity Plugin 會收集當機報告,用於偵錯和分析。如要停用當機回報功能,請參閱下列 Android 和 iOS 相關章節。

Android

在應用程式的 AndroidManifest.xml 檔案中,新增 <meta-data> 標記,並將 DISABLE_CRASH_REPORTING 設為 true:

<manifest>
   <application>
       <meta-data
           android:name="com.google.android.gms.ads.flag.DISABLE_CRASH_REPORTING"
           android:value="true" />
   </application>
</manifest>

iOS

呼叫 DisableSDKCrashReporting 方法,在 iOS 停用當機報告:

void Awake() {
  MobileAds.DisableSDKCrashReporting();
}

取得 Unity 外掛程式版本

如要取得 Unity SDK 版本,請執行下列指令:

// Get the Unity SDK version.
Debug.Log("Unity SDK Version: " + MobileAds.GetVersion());

取得平台版本

Unity Google Mobile Ads Unity Plugin 依附於 Android 和 iOS 平台 SDK。如要取得平台 SDK 的版本,請執行下列指令:

// Get the underlying platform SDK version.
Debug.Log("Platform SDK Version: " + MobileAds.GetPlatformVersion());