This guide is intended for publishers who want to monetize a Unity app.
Integrating the Google Mobile Ads Unity plugin into an app, which you will do here, is the first step toward displaying AdMob ads and earning revenue. Once the integration is complete, you can choose an ad format (such as native or rewarded video) to get detailed implementation steps.
Prerequisites
- Use Unity 2019 or higher
- To deploy to iOS
- Xcode 14.1 or higher
- Target iOS 11.0 or higher
- CocoaPods
- To deploy to Android
- Google Play Services 18.1.0 or higher
- Target Android API level 16 or higher
- Recommended: Create an AdMob account and register an Android and/or iOS app
Download the Mobile Ads Unity plugin
The Google Mobile Ads Unity plugin enables Unity developers to easily serve Google Mobile Ads on Android and iOS apps without having to write Java or Objective-C code. The plugin provides a C# interface for requesting ads that is used by C# scripts in your Unity project.
Use the links below to download the Unity package for the plugin or to take a look at its code on GitHub.
DOWNLOAD THE PLUGIN VIEW SOURCE
Import the Mobile Ads Unity plugin
Open your project in the Unity editor. Select Assets > Import Package >
Custom Package and find the GoogleMobileAdsPlugin.unitypackage
file you
downloaded.
Make sure all of the files are selected and click Import.
Include the Mobile Ads SDK
The Google Mobile Ads Unity plugin is distributed with the Unity Play Services Resolver library. This library is intended for use by any Unity plugin that requires access to Android specific libraries (e.g., AARs) or iOS CocoaPods. It provides Unity plugins the ability to declare dependencies, which are then automatically resolved and copied into your Unity project.
Follow the steps listed below to ensure your project includes the Mobile Ads SDK.
Android
In the Unity editor, select Assets > External Dependency Manager > Android
Resolver > Resolve. The Unity External Dependency Manager library will
copy the declared dependencies into the Assets/Plugins/Android
directory
of your Unity app.
iOS
No additional steps are required to include the Mobile Ads SDK into the Unity project.
- When using Unity 5.6 and above, an xcworkspace is generated that includes the required dependency libraries. Use the generated xcworkspace instead of the standard Xcode project.
- When using older versions of Unity, the dependencies are included inside the standard Xcode project.
Set your AdMob app ID
In the Unity editor, select Assets > Google Mobile Ads > Settings from the menu.
Enter your Android and iOS AdMob app ID in each field.
Raise ad events on the Unity main thread
The Google Mobile Ads SDK sometimes raises events on a different thread than the Unity main thread. This could cause problems when interacting with Unity objects from events dispatched from the Google Mobile Ads SDK. As a work around, you might need to add code to synchronize the Mobile Ads SDK events with the Unity main thread.
If you would like the Mobile Ads SDK to handle this threading concern
for you, set MobileAds.RaiseAdEventsOnUnityMainThread
to true
. This
will force the Mobile Ads SDK to raise all events and callbacks on the Unity
main thread.
...
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;
}
}
Initialize the Mobile Ads SDK
Before loading ads, have your app initialize the Mobile Ads SDK by calling
MobileAds.Initialize()
. This needs to be done only once, ideally at app
launch.
Here's an example of how to call Initialize()
within the Start()
method
of a script attached to a GameObject
:
...
using GoogleMobileAds.Api;
...
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
public void Start()
{
// Initialize the Google Mobile Ads SDK.
MobileAds.Initialize(initStatus => { });
}
}
If you're using mediation, wait until the callback occurs before loading ads, as this will ensure that all mediation adapters are initialized.
Select an ad format
The Mobile Ads SDK is now included in your Unity app when deploying to either the Android or iOS platform. You're now ready to implement an ad. AdMob offers a number of different ad formats, so you can choose the one that best fits your user experience needs.
Banner
Banner ads are rectangular image or text ads that occupy a spot within an app's layout. They stay on screen while users are interacting with the app, and can refresh automatically after a certain period of time. If you're new to mobile advertising, they're a great place to start.
Interstitial
Interstitials are full-screen ads that cover the interface of an app until closed by the user. They're best used at natural pauses in the flow of an app's execution, such as in between levels of a game or just after completing a task.
Native
Native is a component-based ad format that gives you the freedom to customize the way assets such as headlines and calls to action are presented in their apps. By choosing fonts, colors, and other details for yourself, you can create natural, unobtrusive ad presentations that can add to a rich user experience.
Rewarded
Rewarded video ads are full-screen video ads that users have the option of watching in full in exchange for in-app rewards.
Request app tracking transparency authorization
To display the app tracking transparency (ATT) authorization request for accessing the IDFA, use Unity's iOS 14 Advertising Support package.
Same app key
Prerequisites: Google Mobile Ads Unity Plugin 6.1.0 or higher
The Google Mobile Ads SDK introduces the same app key to help you deliver more relevant and personalized ads by using data collected from the app the user is using.
The same app key is enabled by default, but you can disable it with the following API:
public void Start() { RequestConfiguration requestConfiguration = new RequestConfiguration.Builder() .SetSameAppKeyEnabled(true).build(); MobileAds.SetRequestConfiguration(requestConfiguration); // Initialize the Google Mobile Ads SDK. MobileAds.Initialize(HandleInitCompleteAction); }