In our quest of building 1 app every 30 days, the question of monetization has come up a handful of times. Do we integrate some sort of in-app purchases (IAP)? Do we run ads via AdMob? Do we care? Damn right we care! We may be doing these 30 day projects with the primary goal of self education, but education doesn’t pay the bills. Money does.
For us it comes down to IAP or ads. For Flappy Kitten the answer was pretty easy. Because Flappy Kitten is designed to be a very simple game that you only play for a couple minutes, purchasing some sort of power-up or advancement just didn’t fit the mold. Ads, on the other hand, are a perfect fit. Relatively hands off from our side, very little programming needed to get them going, and revenue will have a direct correlation to the success of Flappy Kitten. The more users we gain, the more new games we can encourage them to play, the more friends they invite… the more ads we will serve, and therefore, the more revenue we will see.
So, lets get down to it! Here’s how I went about getting AdMob functional in the Android build of Flappy Kitten.
Get The Tools
The first step is to download the Google Mobile Ads Unity Plugin v2.0 and install it within Unity. You do this by going to the Assets > Import Package > Custom Package and then browsing to the package you downloaded. Import all and you’ll be ready to start setting up your ad within Unity!
This part is pretty simple as well. In the case of Flappy Kitten we simply wanted an ad to appear at the top of the screen. Fortunately the demo script in the GitHub project does exactly what we needed with just a few tweaks. Grab a copy of the demo script and add it to your project.
Once the script has been added to your project, add it as a component to the Camera on your Scene:
Now that you have the script added, let’s open it in your editor and make a few changes. The first thing you are going to need to do is to update your adUnitIds in the script.
Setting Up AdMob And The Script
To get these ids you are going to need an AdMob account. If you don’t already have an AdMob account you can create one by clicking the “Sign up With AdMob” on the AdMob home page.
Once you have an account you will need to create at least one ad unit for Android within AdMob. Click the +Monetize new app button in the upper left of the home screen, follow the prompts, and that’s it.
One thing to note during this process is that you will be prompted to select either Banner or Interstitial for your ad format. Go with Banner. Honestly, I don’t know what an Interstitial ad is, but from what I have read, the plugin does not fully support it yet.
At the end you will be presented with an Ad Unit Id. Copy and paste this value into the Android adUnitId parameter in the script shown above.
If you also plan to support iOS, now would be a good time to get an ad created for iOS and set its id as well.
If you run your scene in Unity at this point, you’re going to have a bad time.
In the discussions I read leading up to finding this plugin I saw promises of being able to see test ads within Unity while testing, and the Start script above had my hopes pretty high for this to happen. Unfortunately it doesn’t quite seem to be working. At least not for me. But hey, this version of the plugin is pretty new from what I can tell, so we’ll cut the devs some slack right?
So let’s get to seeing some ads!
Go back to the demo script we added the adUnitIds to and let’s do some clean up. Start by completely removing the OnGUI method. Then, at the end of your Start method add calls to the RequestBanner and ShowBanner methods. When you are done your Start method should look something like this:
And that’s it for script changes. Pretty easy right?
Building For Android
The final step in this process is to get the Android build working. Prior to adding Google Ads to Flappy Kitten, I had successfully built and deployed the app a handful of times, but had a couple issues getting it to build once I added ads.
The first issue I ran into was that I was missing the Google Play Services SDK Add-on. This was a pretty simple fix in that I just needed to open the SDK Manager in Android Studio and install it.
With that added, I tried to build for Android again and ran into a new error. The fix for that was to add a copy of the ‘sdk/extras/google/google_play_services/libproject/google-play-services_lib’ folder into the ‘Plugins/Android’ folder within my Unity project. This particular issue was the toughest thing for me to track down as a newb Unity / Android developer, so if there’s any one thing you take away from this write up, this is probably it.
So finally, that should be all you need to do to get AdMob ads running in your Unity built Android application. Adding support for iOS is very simple as well since this plugin supports both platforms. All you will need is the AdMob adUnitId set.
If you have additional scenes that you want to add ads to, it’s as simple as adding the script above to the Camera on your scene and letting it run. And making changes to the appearance seems to be pretty straightforward as well.
If you have any questions or see that I missed a step, please leave a comment below.