How-to: Integrate AdDuplex into a Unity3d game

Since the introduction of Unity3d for Windows Phone 8 and Windows Store Apps in July 2013 developers are asking for a way to integrate AdDuplex into their awesome games.
At the moment of this writing no AdDuplex plugin exists in the Unity Asset Store. This is one of the high priority tasks on our to-do list, but it’s not there yet. However, it is possible to add AdDuplex control to your game manually and quite a few developers have already implemented it. This is done by simply building the project and adding some code using Visual Studio. The trick to achieve communication between Unity and the AdDuplex ad control is to provide public events via Unity scripts for the XAML page code-behind to subscribe to.

Following is a specific example on how to add and communicate with the AdControl from a Unity game for Windows Phone 8. Communication is illustrated by implementing a capability to hide the ads from your Unity script.

  1. In Unity create a specific C# script and name it Interop.cs. It’s purpose is to provide a public event handler that can be used to communicate with the actual XAML code-behind.
  2. Create a method that fires the CollapseAdControl event. Fill the script with the following code:
    using UnityEngine;
    using System.Collections;
    using System;
    
    public static class Interop {
    
    public static event EventHandler CollapseAdControl;
    public static void RaiseCollapsedAdControl() {
    if (CollapseAdControl != null) {
    CollapseAdControl(null, null);
    }
    }
    }

  3. Build the Unity project (SHIFT + CTRL + B)
  4. Find and open the generated Visual Studio Solution
  5. Go to Tools -> Library Package Manager -> Package Manager Console
  6. Run the following command to install AdDuplex dependencies:
    PM> Install-Package AdDuplexWP8
    

  7. Add the adControl to your game:
    <DrawingSurfaceBackgroundGrid x:Name="DrawingSurfaceBackground" Loaded="DrawingSurfaceBackground_Loaded">
    
    <adduplex:AdControl x:Name="adDuplexAd" AppId="YOUR_AppId_HERE"/>
    </DrawingSurfaceBackgroundGrid>

  8. Hook up to Interop.CollapseAdControl event in the MainPage.xaml.cs
    Interop.CollapseAdControl += Interop_CollapseAdControl;
    

  9. Add the event handler code
    void Interop_CollapseAdControl(object sender, EventArgs e)
    {
    
    adDuplexAd.Visibility = System.Windows.Visibility.Collapsed;
    }

2 thoughts on “How-to: Integrate AdDuplex into a Unity3d game

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s