Monetize Your Unity Game: How to Integrate AdMob Ads and In-App Purchases on Android and iOS

Monetize Your Unity Game: How to Integrate AdMob Ads and In-App Purchases on Android and iOS

1. Importing Necessary Packages

Step 1: Install Required Packages

  1. In the Unity Package Manager, install/update the latest 'In App Purchasing' package (com.unity.purchasing) from the Unity Registry.
  2. Download & import the latest Google Mobile Ads Unity Plugin v9.2.0 release from GitHub.
  3. Download the latest version of Google Mobile Ads mediation plugin for AppLovin. (Optional)

Step 2: Import Custom Scripts & Prefabs

Download and import the custom .unitypackage that includes all the necessary custom scripts and prefabs for setting up both AdMob ads and In-App Purchases. 

 

2. Including External Dependencies

Step 1: Configure Gradle Templates

In Unity, navigate to Project Settings > Player > Android > Publishing Settings > Build, and enable the following:

  • Custom Main Gradle Template
  • Custom Gradle Properties Template

Step 2: Resolve Android Dependencies

Go to Assets > External Dependency Manager > Android Resolver > Force Resolve to integrate all necessary dependencies.

Known Issue: If the External Dependency Manager does not appear in the Unity menu after installing the Google Mobile Ads Plugin, follow these steps:

  • Solution: Play your game in the Unity Editor, then stop it. This action typically activates the External Dependency Manager. If this doesn't work, restart Unity and try again.

 

3. Initialising AdMob and IAP Services 

Step 1: Setting Your AdMob App ID

Go to Assets > Google Mobile Ads > Settings, and input your Android and iOS AdMob App IDs in the relevant fields.

  • Enable: Optimize Initialization and Optimize Ad Loading.
  • Add: User Tracking Usage Description: "This identifier will be used to deliver personalized ads to you."

Step 2: Setup AdMob Initialisation Prefab

Drag the _GoogleMobileAdsInit.prefab into your Splash Scene and enter your Android and iOS ad unit IDs for all ad formats

Step 3: Setup IAP Initialisation Prefab

Drag the _IAPInit.prefab into your Splash Scene and enter your Android and iOS consumable and non-consumable product IDs.

 

4. Implementing AdMob Ads

Step 1: App Open Ads

App Open ads are automatically displayed in the following scenarios:

  • When the game is opened from the second time onward.
  • During transitions from the background to the foreground, if the app has been in the background for more than 5 seconds.

Known Issue: The AppStateEventNotifier.AppStateChanged event may not trigger when the app moves to the background or foreground on Android devices.

  • Solution: Update the GoogleMobileAdsDependencies.xml file with the appropriate link to resolve this issue.

Step 2: Banner Ads

  • To display adaptive banner ads in landscape mode, attach the BannerAdReferences.cs script to the relevant popups (such as Shop, Settings, Exit, Pause, Level Complete, or Failed) to enable or disable ads during transitions.
  • For adaptive banner ads in portrait mode, attach the BannerAdReferences.cs script to the canvas object to keep ads visible at all times.
  • Test for any UI overlap using the _DummyBannerCanvas prefab, and enable the appropriate portrait or landscape images based on the device orientation.

Step 3: Interstitial Ads

  • To display interstitial ads during scene transitions, call GoogleMobileAds.Sample.InterstitialAdController.instance.ShowAd(CallbackMethodName()); from any script.
  • Interstitial ads will not be shown if a rewarded ad has been displayed within the last 15 seconds.

Step 4: Rewarded Ads

  • To display a rewarded ad, call GoogleMobileAds.Sample.RewardedAdController.instance.ShowAd(CallbackMethodName()); from any script.
  • Drag the _RewardedAdReferences.prefab into both the menu and game scenes. Then, drag all rewarded ad buttons into the provided array to enable or disable interaction based on ad availability.
  • Use the RewardedAdPreferences.cs script to unlock a single item after watching multiple ads.

 

5. Implementing Unity IAP

  • Drag the _IAPReferences.prefab into both the menu and game scenes. Then, drag all price texts and buy buttons into the corresponding fields, and add callbacks directly from the editor.
  • Attach the ShopProductsCallbacks.cs script to any GameObject to handle callbacks.
  • To restore purchases, either call IAPManager.instance.RestorePurchases() or drag the corresponding public method RestorePurchases() from ShopProductsCallbacks.cs into the desired UI element.

 

    Back to blog

    Leave a comment

    Please note, comments need to be approved before they are published.