全局设置

MobileAds 类为 Google Mobile Ads Unity Plugin 提供了全局设置。

在 Unity 主线程中触发广告事件

Google Mobile Ads Unity Plugin 会在不同于 Unity 主线程的线程上引发事件。如果您实现了广告事件并与 Unity 对象进行交互,则需要将 Google Mobile Ads Unity Plugin 事件与 Unity 主线程同步。

通过让 Google Mobile Ads Unity Plugin 处理同步,手动或自动将广告事件与 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);
    }
});

自动同步广告事件

如需让 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 报告应用的音量后,视频广告会遵循相关的应用音量设置。这可确保用户收到符合预期的视频广告音量。

设备音量由音量按钮或操作系统级的音量滑块控制,它决定了设备的音频输出音量。但是,应用可相对于该设备音量独立调节自己的音量水平,从而定制音频体验。

您可以在加载广告之前调用 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());