How to set up Google AdMod for Unity 3d?

To enable Google Ads in your Unity game, you would need to do the following

Requirements:

– Unity 4 or higher (link)
– Google Mobile Ads SDK (link)

Setup

1. Go to Google’s Game Developers page by visiting the following URL
Google Game Developer page

2. Navigate to the ‘Unity‘ section on the page

3. There will be two buttons (e.g.  “Download the plugin” and “View Source Code“) on this section.  Click on the “Download the plugin” button.  This will bring you to a GitHub page where you can download the ‘GoogleMobileAds‘ unity package.  Find and download the “GoogleMobileAds.unitypackage‘ file.

4. Import the “GoogleMobileAds.unitypackage‘ file to your Unity project.
e.g. In Unity, Select Assets > Import Package > Custom Package and find the GoogleMobileAdsPlugin.unitypackage

5. Click the “Import” button at the popup

6. IMPORTANT:  Once imported, you would need to edit the “AndroidManifest.xml” file inside the “Assets/Plugins/Android/AndroidManifest.xml” folder.
e.g. Change the following line

<meta-data android:name=”com.google.android.gms.version” android:value=”@integer/google_play_services_version”/>

to

<meta-data android:name=”com.google.android.gms.version” android:value=”6587000“/>

You can find the correct value in your Android SDK folder on your computer.   Mine is the following:

C:\Program Files (x86)\Android\android-sdk\extras\google\google_play_services\libproject\google-play-services_lib\res\values\version.xml

7. Edit the default “AndroidManifest.xml” file inside the <android_sdk>/extras/google/google_play_services/libproject/google-play-services_lib/” folder to have the same value as the “AndroidManifest.xml” file inside the “Assets/Plugins/Android” folder. Otherwise, you would get an error later.

8. Add the “Google Play Services Library” from your computer into your project.  You can do this by dragging the following folder from your computer to Unity “Assets/Plugins/Android” folder.

e.g. Drag “<android_sdk>/extras/google/google_play_services/libproject/google-play-services_lib/” to Unity “Assets/Plugins/Android” folder.

9. Try building it now.  If done correctly, it should build without any errors.

Adding Ads

10. Back to the Google’s Game Developers page, you can follow the instructions on the “Unity Plugin API” section and create a new script with some basic Banner Request.  Add that to your Main Camera in Unity.

11. You can also use the “GoogleMobileAdsDemoScript.cs” demo script from the Google HelloWorld example.

*Note: Once thing to make sure is to replace and add YOUR Google AdMob Unit Id instead.  AND, to all three locations

e.g.  Find and replace with your AdMob Unit Id
#if UNITY_ANDROID
string adUnitId = “ca-app-pub-9044402088937556/1721466523“;
#elif UNITY_IPHONE
string adUnitId = “ca-app-pub-9044402088937556/1721466523“;
#else
string adUnitId = “ca-app-pub-9044402088937556/1721466523“;
#endif

12. If you are using the “GoogleMobileAdsDemoScript.cs” file example file and are not planning on using testAds or have the test ID of your device, you will need to change the following from Google’s source example.

e.g.  On the “GoogleMobileAdsDemoScript.cs” – RequestBanner section ,

Change:

bannerView.LoadAd(createAdRequest);

To:

AdRequest request = new AdRequest.Builder().Build();
bannerView.LoadAd(request);

13. If you are planning on adding Interstitial Ads, then you would need to do the same as for Banner Ads.

14. Build your game and you should be able to see ads now.

Note: It might take some time for adds to show up if you just signed up to an AdMob account.
You can also use this adUnitId to test: ca-app-pub-9044402088937556/1721466523.  This is one of our test adUnitId.

 

Below are some errors I ran into:

Error 1

– Error building Player: CommandInvokationFailure: Failed to re-package resources
AndroidManifest.xml:9: error: Error: No resource found that matches the given name (at ‘value’ with value ‘@integer/google_play_services_version’).

Fix: Go to your Goolge SDK folder and copy the value to manually add to to the AndroidManifest.xml file in your Unity3d Assets\Plugins\Android folder
C:\Program Files (x86)\Android\android-sdk\extras\google\google_play_services\libproject\google-play-services_lib\res\values\version.xml

e.g.
Change
<meta-data android:name=”com.google.android.gms.version” android:value=”@integer/google_play_services_version”/>
to
<meta-data android:name=”com.google.android.gms.version” android:value=”6587000″/>

Error 2

– Error building Player: CommandInvokationFailure: Unable to merge android manifests

Trying to merge incompatible /manifest/application/meta-data[@name=com.google.android.gms.version] element:
<meta-data
@android:name=”com.google.android.gms.version”
—    @android:value=”6587000″>
<meta-data
@android:name=”com.google.android.gms.version”
++    @android:value=”@integer/google_play_services_version”>

Fix: Make sure to add the same value to the default AndroidManifest.xml file in your Unity Assets folder

e.g.
Assets->Plugins->Android->google-play-services_lib\AndroidManifest.xml

 

Leave a comment