ADduplex and MS Advertising interstitial integration with Unity3D Win Store — February 17, 2016

ADduplex and MS Advertising interstitial integration with Unity3D Win Store

I will try to write this post in English.

This month at Roshka Studios we wanted to update our Windows Phone games ( Cquence, MiniChase and Escape Fast ) adding some improvements and changing to Windows Store Phone 8.1 Unity3D build. We also wanted to start working with ADduplex. The problem was that we didn’t find a plugin or a tutorial to implement ADduplex API in our Phone 8.1 project. You can find some tutorials here and here but I believe that they only work with the old Phone 8.0 build.

I found a very easy way to integrate ADduplex and MS Advertising in our Windows Store projects so lets get started!

1- We have to create a class to communicate between Unity and our VS project. We will use this class to show ADduplex and MS Advertising interstitial ads whenever we want. Lets call this class Interop.cs.


public static class Interop
 {
  public static Action OnShowInterstitial = delegate { };
  public static Action OnPreloadInterstitial = delegate { };
 static Interop()
 {
  OnShowInterstitial = delegate { };
  OnPreloadInterstitial = delegate {};
 }
 public static void PreloadInterstitialAd()
 {
  OnPreloadInterstitial ();
 }
 public static void ShowInterstitialAd()
 {
  OnShowInterstitial();
 }
}

From now on, when we want to display our interstitial ad in our game, We only have to put Interop.ShowInterstitialAd(); in any part of our code. If we want to preload our interstitial ad ( this is specially for MS Advertising Interstitial videos ), We can do it by using Interop.PreloadInterstitialAd(); in any part of our code.

2- Go to File -> Build Settings -> Select Windows Store -> Phone 8.1 -> check C# project and then click the magic Build button.

3- Download and install Microsoft Universal Ad Client SDK and ADduplex SDK

4- Open your game VS project. Right click in References -> Add Reference…

addReference

5- Find and select ADduplex SDK for Windows Phone 8.1 (XAML) and Ad Mediator SDK for Windows Phone 8.1 XAML -> Click OK

referenceManager

 

6- Add the following line at the beginning of the OnLaunched method in the App.xaml.cs file AdDuplex.Universal.Controls.WinPhone.XAML.AdDuplexClient.Initialize(“YOUR-ADDUPLEX-APP-KEY”);

protected override void OnLaunched(LaunchActivatedEventArgs args)
{
 AdDuplex.Universal.Controls.WinPhone.XAML.AdDuplexClient.Initialize("YOUR-ADDUPLEX-APP-KEY");
 splashScreen = args.SplashScreen;
 InitializeUnity(args.Arguments);
}

7- In MainPage.xaml.cs add the following line inside public MainPage()

 Interop.OnShowInterstitial += OnShowInterstitial;
 Interop.OnPreloadInterstitial += PreloadInterstitial;
 // This is just to randomly show first ADduplex or MS Interstitial video
 Random adsToShow = new Random();
 if (adsToShow.Next(0, 2) == 0)
 {
    showMS = true;
 }

8- Now add the following line in MainPage.xaml.cs

 AdDuplex.Universal.Controls.WinPhone.XAML.InterstitialAd interstitialAd;
 InterstitialAd MsAdvertising = new InterstitialAd();
 private string AdAppID = "YOUR-MS-APP-ID-(PUBCENTER)";
 private string AdUnitID = "YOUR-MS-AD-UNIT-ID-(PUBCENTER)";
 bool showMS = false;
 private void PreloadInterstitial()
 {
    // TestInterstitial();
    MsAdvertising.RequestAd(AdType.Video, AdAppID, AdUnitID);
 }

 private void TestInterstitial()
 {
    // Use these values to test if Ms Advertising is working
    AdAppID = "d25517cb-12d4-4699-8bdc-52040c712cab";
    AdUnitID = "11389925";
 }
 private void OnShowInterstitial()
 {
   AppCallbacks.Instance.InvokeOnUIThread(new AppCallbackItem(() =>
   {
     if (!showMS) // we will show Adduplex
     {
       if (interstitialAd == null)
       {
         interstitialAd = new AdDuplex.Universal.Controls.WinPhone.XAML.InterstitialAd("184571");
       }
       interstitialAd.ShowAd();
       showMS = true; // next time we will show MS Advertising
     }
     else // we will show MS Advertising
     {
       if (InterstitialAdState.Ready == MsAdvertising.State)
       {
         MsAdvertising.Show();
       }
       showMS = false;
     }
    }), false);
   }

8.1- And don’t forget to add the following line in your MainPage.xaml.cs file.

using Microsoft.Advertising.WinRT.UI;

9- Finally add internet capabilities in your manifest and you are ready to go!

manifestcapabilities

Of course you can extend/improve this code if you want. Additional info here
Some issues that I had

1- If for some reason you need to uninstall Microsoft Universal Adclient SDK, use program like Revo Uninstaller Pro to remove it.

2- Couldn’t make a functional build (MASTER) with VS Enterprise 2015, Succeed with VS Community 2015 …