Page Summary
-
The
MobileAdsclass provides global settings for the Google Mobile Ads SDK in Unity. -
You can manually or automatically synchronize ad events with the Unity main thread to interact with Unity objects.
-
The SDK allows reporting app volume and mute status to video ads, which can impact ad eligibility and revenue.
-
Crash reporting for debugging and analysis can be disabled on both Android and iOS platforms.
-
You can retrieve the Unity plugin version and the underlying platform SDK version.
The MobileAds
class provides global settings for 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 need to synchronize the Google 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.
Recommended: Manually synchronize ad events
To synchronize ad events manually, use the ExecuteInUpdate
method on the main
thread. You should use the ExecuteInUpdate
method when interacting with
UnityEngine objects, and while the RaiseAdEventsOnUnityMainThread
property is
disabled.
The following example logs a background thread and runs an action to interact with UnityEngine objects:
// 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
);
}
});
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 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 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.
Consent for cookies
If your app has special requirements, you can set the optional ApplicationPreferences
key gad_has_consent_for_cookies
to zero to enable limited ads
:
// Enable limited ads
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.
-
Enable Custom Proguard File
Go to Project Settings > Player > Android > Publishing Settings > Build, and select:
- Custom Proguard File
-
Open
/Assets/Plugins/Android/proguard-user.txtand add the following:
-
keep
class
com
.
google
.
**
{
public
*
;
}
Disable crash reporting
Google Mobile Ads SDK collects crash reports for debugging and analysis purposes. To disable crash reporting, see the following sections for 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
());

