通用設定

MobileAds 類別提供 的全域設定。

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

會在不同於 Unity 主執行緒的執行緒中觸發事件。如果您導入廣告事件並與 Unity 物件互動,就必須將事件與 Unity 主執行緒同步處理。

您可以手動將廣告事件與 Unity 主執行緒同步處理,也可以讓 處理同步處理作業。

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

如要手動同步處理廣告事件,請在主要執行緒上使用 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);
    }
});

自動同步廣告事件

如要同步處理廣告事件,請將 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;
    }
}

影片廣告音量控制選項

如果您的應用程式有專屬的音量控制項 (例如自訂音樂或音效音量),向廣告揭露應用程式音量可讓影片廣告採用應用程式音量設定。確保使用者接收到影片廣告時,廣告的音量符合預期。

裝置音量鍵或作業系統層級的音量滑桿,可控制裝置音訊輸出音量。不過,應用程式可以獨立調整相對於裝置音量的音量,打造專屬的音訊體驗。

載入廣告前,您可以呼叫 SetApplicationVolume() 方法,向回報相對應用程式音量。有效廣告音量值範圍介於 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,也就是目前的裝置音量,且應用程式不會設為靜音。

如果應用程式有特殊需求,您可以將選用的 ApplicationPreferencesgad_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 *; }

停用當機報告

收集當機報告,以利偵錯和分析。如要停用當機報告功能,請參閱下列 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 版外掛程式依附於 Android 和 iOS 平台 SDK。如要取得平台 SDK 版本,請執行下列指令:

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