Global Settings

Select platform: Android iOS Unity

The MobileAds class provides global settings for the Google Mobile Ads SDK.

Raise ad events on the Unity main thread

Google Mobile Ads SDK raises events on a different thread than the Unity main thread. If you implement ad events and interact with Unity objects, you must synchronize the Mobile Ads SDK events with the Unity main thread.

Synchronize ad events with the Unity main thread either manually, or automatically, by letting Google Mobile Ads SDK handle the synchronization.

Manually synchronize ad events

To synchronize ad events, manually use ExecuteInUpdate method on the main thread. You must use the method when RaiseAdEventsOnUnityMainThread property is disabled and interacts with UnityEngine objects.

The following example logs a background thread and runs an action to interact with UnityEngine objects:

rewardedAd.OnAdPaid += (AdValue value) =>
{
    // Log the ad value immediately.
    UnityEngine.Debug.Log($"Received ad value of {value.Value} {value.CurrencyCode}.");

    MobileAdsEventExecutor.ExecuteInUpdate(() =>
    {
        // Interact with UnityEngine objects here.
    });
};

Automate synchronizing on ad events

For Google Mobile Ads SDK to synchronize ad events, set MobileAds.RaiseAdEventsOnUnityMainThread property to 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;
    }
}

Video ad volume control

If your app has its own volume controls, such as custom music or sound effect volumes, disclosing app volume to the Google Mobile Ads SDK enables video ads to respect app volume settings. This ensures users receive video ads with the expected audio volume.

The device volume, controlled through volume buttons or OS-level volume slider, determines the volume for device audio output. However, apps can independently adjust volume levels relative to the device volume to tailor the audio experience.

You can report the relative app volume to the Google Mobile Ads SDK by calling the SetApplicationVolume() method before loading the ad. Valid ad volume values range from 0.0 (silent) to 1.0 (current device volume). Here's an example of how to report the relative app volume to the SDK:

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

To inform the SDK that the app volume has been muted, call the SetApplicationMuted() method before loading the ad:

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

By default, the app volume is set to 1, the current device volume, and the app is not muted.

If your app has special requirements, you can set the optional ApplicationPreferences key gad_has_consent_for_cookies to zero to enable limited ads (LTD):

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

Android minification

This Unity publishing option lets you enable java code minification. If you enable minification you will also need to create a custom proguard file to keep classes referenced by the SDK.

  1. Enable Custom Proguard File

    Go to Project Settings > Player > Android > Publishing Settings > Build, and select:

    • Custom Proguard File
  2. Open /Assets/Plugins/Android/proguard-user.txt and add the following:

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

Disable crash reporting

The Google Mobile Ads SDK collects crash reports for debugging and analysis purposes. You can disable this crash reporting with the following code. The following sections describe how to disable crash reporting on Android and iOS.

Android

Add the <meta-data> tag with DISABLE_CRASH_REPORTING set to true in your app's AndroidManifest.xml file:

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

iOS

Call the DisableSDKCrashReporting method to disable crash reports on iOS:

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

Get Unity plugin version

To get the Unity SDK version, run the following:

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

Get platform version

The Google Mobile Ads SDK for Unity depends on the Android and iOS platform SDKs. To get the version of the platform SDK, run the following:

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