Bắt đầu

The Google Mobile Ads Unity plugin lets Unity developers 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.

This guide is intended for publishers who want to monetize a Unity app.

Prerequisites

  • Use Unity 2019.4 or higher
  • To deploy to iOS
    • Xcode 15.3 or higher
    • Target iOS 12.0 or higher
    • CocoaPods
  • To deploy to Android
    • Minimum Android API level of 21 or higher
    • Target Android API level 34 or higher

Android API level is set in Project Settings > Player > Android > Other Settings > Other Settings.

Import the Mobile Ads for Unity plugin

OpenUPM-CLI

If you have the OpenUPM CLI installed, you can install the OpenUPM registry with the following command:

openupm add com.google.ads.mobile

Remove .unitypackage plugin assets

If you are migrating from using a .unitypackage to using OpenUPM, you must manually uninstall the assets from the old location. Remove the following directories that were imported from the .unitypackage:

  • Assets/ExternalDependencyManager
  • Assets/GoogleMobileAds
  • Assets/Plugins/Android/googlemobileads-unity.aar
  • Assets/Plugins/Android/GoogleMobileAdsPlugin
  • Assets/Plugins/iOS/GADUAdNetworkExtras
  • Assets/Plugins/iOS/unity-plugin-library.a

OpenUPM

  1. Open the package manager settings by selecting the Unity menu option Edit > Project Settings > Package Manager.
  2. Add OpenUPM as a scoped registry to the Package Manager window:

    Name: OpenUPM
    URL: https://package.openupm.com
    Scopes: com.google
    
  3. Open the package manager menu by selecting the Unity menu option Window > Package Manager.

  4. Set the manager scope drop-down to select My Registries.

  5. Select the Google Mobile Ads for Unity package from the package list and press Install.

Remove .unitypackage plugin assets

If you are migrating from using a .unitypackage to using OpenUPM, you must manually uninstall the assets from the old location. Remove the following directories that were imported from the .unitypackage:

  • Assets/ExternalDependencyManager
  • Assets/GoogleMobileAds
  • Assets/Plugins/Android/googlemobileads-unity.aar
  • Assets/Plugins/Android/GoogleMobileAdsPlugin
  • Assets/Plugins/iOS/GADUAdNetworkExtras
  • Assets/Plugins/iOS/unity-plugin-library.a

Import from GitHub

  1. Download the latest .unitypackage release from GitHub.
  2. Import the .unitypackage file by selecting the Unity menu option Assets > Import package > Custom Package and importing all items.

Include external dependencies

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, such as 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 these steps to ensure that your project includes all the dependencies:

Android

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

  • Custom Main Gradle Template
  • Custom Gradle Properties Template

In the Unity editor, select Assets > External Dependency Manager > Android Resolver > Resolve to have the Unity External Dependency Manager library copy the declared dependencies into the Assets/Plugins/Android directory of your Unity app.

iOS

Unity for iOS uses CocoaPods to identify and manage dependencies. Consult the CocoaPods documentation for specific requirements or troubleshooting steps related to your iOS dependencies.

The Google Mobile Ads Unity plugin dependencies are listed in Assets/GoogleMobileAds/Editor/GoogleMobileAdsDependencies.xml.

Set your Ad Manager app ID

In the Unity editor, select Assets > Google Mobile Ads > Settings from the menu.

Enter your Android and iOS Ad Manager app ID in each field.

Initialize the SDK

Before loading ads, have your app initialize the Google 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 to ensure that all mediation adapters are initialized.

Select an ad format

The Google 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 makes for the best user experience.

Banner ad units display rectangular ads that occupy a portion of an app's layout. They can refresh automatically after a set period of time. This means users view a new ad at regular intervals, even if they stay on the same screen in your app. They're also the simplest ad format to implement.

Implement banner ads

Interstitial

Interstitial ad units show full-page ads in your app. Place them at natural breaks and transitions in your app's interface, such as after level completion in a gaming app.

Implement interstitial ads

Native

Native ads are ads where you can customize the way assets such as headlines and calls to action are presented in your apps. By styling the ad yourself, you can create a natural, unobtrusive ad presentations that can add to a rich user experience.

Implement native ads

Rewarded

Rewarded ad units enable users to play games, take surveys, or watch videos to earn in-app rewards, such as coins, extra lives, or points. You can set different rewards for different ad units, and specify the reward values and items the user received.

Implement rewarded ads