How-to integrate AdDuplex interstitial ads into a Unity game

We are seeing more and more Unity games joining our cross promotion network. Some of them are housing AdDuplex banners and some are already benefiting from the newly introduced interstitial ads.

Interstitial ads are best applicable to games, since they occupy the whole screen and are usually displayed during natural pauses in app’s flow. It is quite a trivial task to include AdDuplex Interstitial ads into a game that is created using Mono Game, XAML or some other tool that depend on a straightforward Windows Phone page manipulation in Visual Studio (or Microsoft Blend for that matter). For Unity3D developers it might not be as simple.

Below is a guide describing one of the ways to do this. It consists of two steps: configuring the Unity project and configuring the Visual Studio solution created by it.

Configuring the Unity project

  1. Register your Windows Phone app in AdDuplex system and write down your Ad unit ID and App key.
  2. In Unity create a specific C# script and name it Interop.cs. It’s purpose is to provide a public event handlers that can be used to communicate with the actual XAML code-behind.
  3. To load and show interstitial ad in Unity script call Interop.ShowInterstitial().
  4. To pre-load interstitial ad in Unity call Interop.LoadInsterstitial(). This is not required, since Interop.ShowInterstitial() can also download an ad, but it can be used to shorten loading times. 
  5. Subscribe to Interop.OnInterstitialClosed event to know when the user closes the ad. Be careful for what you do in the callback, because a lot of Unity engine functionality is non thread safe.
  6. Build the Unity project (SHIFT + CTRL + B) into a Windows Phone 8 project

Configuring the build in Visual Studio

  1. Find and open the generated Visual Studio Solution
  2. Go to Tools -> NuGet Package Manager -> Package Manager Console (or Tools -> Library Package Manager -> Package Manager Console when in Visual Studio 2012 or below)
  3. Run the following command in the console to install AdDuplex dependencies: Install-Package AdDuplexWP8 (If you are building to Windows Phone XAML 8.1 (Universal) package please refer to Windows Phone 8.1 XAML AdDuplex Interstitial Ad Installation and Usage and change the namespaces accordingly.
  4. Add the following line to the Application_Launching (or OnLaunched if you are on Windows Phone 8.1 XAML Universal build) method in the App.xaml.cs file. Don’t forget to use your real AppKey!
  5. Open the MainPage.xaml.cs file and create a member of InterstitialAd type.
  6. Locate the constructor method public MainPage() and add some code to subscribe to Interop events.
  7. Finally add code to manage the interstitials according to Interop commands and initialize the ad control. Don’t forget to fill in your AdUnitId here.

This is it. You can now build and test the project. Here is a sample project for Unity 5 that waits for a user tap to show an ad – GitHub AdDuplexUnity sample.

If you have ideas on improving this guide or need some help just drop a comment below or contact us via support.adduplex.com

Tweet about this on TwitterShare on FacebookShare on LinkedInShare on Reddit