There has never been a better time to integrate the Amazon Mobile Ads API into your game and now with full support for Unity, it’s easier then ever. In this post I’ll walk you through how to get everything up and running as well as how to build a simple test in order to make sure ads correctly load and display. Before we get started, you’ll want to read over this page and follow instructions 1 – 5. This will cover setting up the application key in the developer portal, setting up your tax information as well as downloading and installing the SDK. Once you’ve followed those steps you should be ready to test out that everything works. For this post, I’m using an empty Unity project. You can do the same steps in your own game as well. With your Unity project open and the Amazon Mobile Ads SDK plugin imported, we are ready to write some code.
You’ll want to make sure your project folder looks like this:
Here you can see that we have the Amazon Mobile Ads SDK inside of the plugins folder along with the Android and iOS native code that integrates with the Amazon Mobile Ads SDK.
Let’s create an empty GameObject in our scene and call it AdTester. Then create a new C# script with the same name and attach it to the GameObject we just created.
At the top of our AdTester we’ll need to import the ad classes. Put the following near the top of the script with the other using statements:
using com.amazon.mas.cpt.ads;
Now we’ll want to store the Android and iOS key as well as a reference to the AmazonMobileAds class. Add the following properties to our class:
public string androidKey;
public string iosKey;
private IAmazonMobileAds mobileAds;
On the AdManager GameObject in your scene, make sure to add your iOS and Android keys you got from the Amazon Developer Portal.
This step is very important. If you don’t have the right keys you won’t be able to load any ads.
Now let’s go back to our AdTester class and create a new method to set up our app keys:
public void SetAppKey(){
// Create a reference to the mobile ads instance
mobileAds = AmazonMobileAdsImpl.Instance;
// Create new key
ApplicationKey key = new ApplicationKey ();
// Set key based on OS
#if UNITY_ANDROID
key.StringValue = androidKey;
#elif UNITY_IPHONE
key.StringValue = iosKey;
#endif
// Pass in the key
mobileAds.SetApplicationKey (key);
}
As you can see, we simply get a reference to the AmazonMobileAds class via its Instance getter. Then we set up the key instance and set its value based on if we are running the Android or Unity build of the game. Finally, we pass in the key to the SetApplicationKey method of the ads instance.
Now we’ll want to enable testing mode on the ad instance. This is an optional step but good to do when first testing out that everything works in your game, especially if you’re located outside of one of the Amazon Mobile Ads API supported countries (US, UK, Germany, France, Spain, Italy, Japan). Add the following method:
public void EnableTesting(){
//Create should enable instance
ShouldEnable enable = new ShouldEnable ();
enable.BooleanValue = true;
mobileAds.EnableTesting (enable);
mobileAds.EnableLogging (enable);
}
Here we create a ShouldEnable instance which will wrap a Boolean value. Then we pass it into the EnableTesting method on our ad instance. You can also enable verbose logging by passing in the same enable instance to the EnableLogging method, which is helpful for troubleshooting. It’s important to note that you should always set these to false when releasing your game.
Now it’s time to create our test ad. We’ll do a floating ad that is docked to the top of the screen and centered. Add the following method to our class:
public void DisplayFloatingAd(){
// Configure placement for the ad
Placement placement = new Placement ();
placement.Dock = Dock.TOP;
placement.HorizontalAlign = HorizontalAlign.CENTER;
placement.AdFit = AdFit.FIT_AD_SIZE;
// This method returns an Ad object, which you must save and keep track of
Ad response = mobileAds.CreateFloatingBannerAd(placement);
// This method returns a LoadingStarted object
LoadingStarted newResponse = mobileAds.LoadAndShowFloatingBannerAd(response);
}
While this is just one type of ad, you can always test out a full screen interstitial or other layout options by looking at the ad’s documentation page here.
At this point, all that’s left to do is call these methods in the correct order. Simply add the following to your start method:
SetAppKey ();
EnableTesting (); //Remove before release
DisplayFloatingAd ();
Now you are ready to run and test your game. If you are on Android, make sure to create a new AndroidManifest from the sample provided in the SDK. If you are on iOS follow the rest of the steps on configuring the native plugin to correctly link it up in Xcode. Deploy the game to your testing device and you should see an ad at the top of the screen.
The ad will automatically rotate based on the orientation of the device and your game’s layout settings. And that’s everything you need to know to quickly get Unity Ads up and running in your game. Make sure you take advantage of our special offer to get $6 CPM when you run Amazon Ads in your game from September through November.
The Amazon Mobile Ad network is offering a limited time opportunity to earn a guaranteed $6 CPM for interstitial ads on qualifying apps*. All qualifying iOS and Android apps that integrate the Amazon Mobile Ads API and send the ad request for the first time on or after July 14, 2015 will receive a guaranteed $6 CPM on interstitial ads in September, October and November (up to 1 million impressions per app per month). Interstitial impressions served across all supported devices (iOS, Android, and Fire OS tablets and phones), supported countries (US, UK, Germany, France, Italy, Spain and Japan), and supported stores (Apple App Store, Google Play and Amazon Appstore) qualify. Click here to learn more.
For more information about Amazon Ads, check out the following links:
- Jesse Freeman (@jessefreeman)
Want the latest app and game news delivered to your inbox? Stay in the loop on the latest industry best practices, Amazon promotions, and new launches by subscribing to our weekly blog summary here.