Monetize Unity 5 games on Windows Phone 8.X

5

How do I display ads in my game made in Unity 5 to the Windows Phone 8.X platform?

    
asked by anonymous 17.01.2016 / 03:00

1 answer

7

To do this just follow the monetization steps of a common Windows Phone application, we have two types of ads:

  • Banner: A range of various sizes to be displayed constantly in the app.
  • Interstitial: A new format, where when called the whole screen is occupied by a product video.

But since the Unity 5 was quoted, we should use the interoperabilidade concept to display the ad at any point in the game.

  

Interoperabilidade is the capacity of a system (computerized or not)   to communicate transparently (or as closely as possible) with   another system (similar or not).

     

Source: Wikipedia

I'll show you how to include the Interstitial (the most common) ad, so go to your Dashboard and add one ad block in your game's registry.

In the case how it will be exported to Windows Phone we should do such interoperability by creating events that can be manipulated in Visual Studio , so let's create a script C# in Assets of our game:

File: Interop.cs

using UnityEngine;
using System.Collections;
using System;

public static class Interop
{
    /*
     * EventHandler é o tipo de evento trabalhado no desenvolvimento para WP.
     *
     * Criamos variáveis de evento para  mostrar e requirir o anúncio (você pode 
     * criar outras para outras ações, como ao fechar anúncio.
     */
    public static event EventHandler ShowInterstitialEvent;
    public static event EventHandler RequestInterstitialEvent;

    public static void ShowInterstitialAd()
    { /* Quando chamamos Interop.ShowInterstitialAd() ele chamará 
         ShowInterstitialEvent do outro sistema (no VS) */
        if (ShowInterstitialEvent != null)
        {
            ShowInterstitialEvent(null, EventArgs.Empty);
        }
    }

    public static void RequestInterstitialAd()
    { /* Quando chamamos Interop.RequestInterstitialAd() ele chamará 
         RequestInterstitialEvent do outro sistema (no VS) */
        if (RequestInterstitialEvent != null)
        {
            ShowInterstitialEvent(null, EventArgs.Empty);
        }
    }
}

RequestInterstitialAd should be called a screen before which you want to display your ad, in my case I called it in the character's move script:

using UnityEngine;
using System.Collections;

public class playermoviment : MonoBehaviour {

    void Start()
    {
        Interop.RequestInterstitialAd();
    }
}

The next step is to open script where we want to call the opening of our ad, in my case I created a gameover.cs where I will only go back to the game when there is click(touch) on the screen.

File: gameover.cs

using UnityEngine;
using System.Collections;

public class gameover : MonoBehaviour {

    void Update()
    {
        if (Input.GetMouseButtonDown (0)) {
            Application.LoadLevel("01"); //Volta para a scene do meu jogo
        }
    }
}

We should call our ShowInterstitialAd method the moment we want to open the ad, and we will create public and static variables to control our ad, in which case I will use the variables:

  • internet : will set whether the internet on the device is unlinked.
  • closeAds : will set whether the ad was closed, so we returned touching the game.

Let's go to the new code for gameover.cs :

using UnityEngine;
using System.Collections;

public class gameover : MonoBehaviour {

    /* Variáveis de controle
     * closeAds como false, pois o meu anúncio será chamado no Start da tela
     * internet como true (apenas para definir um valor padrão
     */
    public static bool closeAds = false; 
    public static bool internet = true;

    void Start () {

        if (internet) { /* iremos chamar o método de mostrar anúncio  
                           somente se a internet estiver ligada. */
            Interop.ShowInterstitialAd();
        }
    }

    void Update()
    {
        /* Captamos o controle (no caso, o touch reiniciar o jogo)
         * somente se a variável closeAds estiver como true 
         * (por isso declarei como false)
         */
        if (Input.GetMouseButtonDown (0) && closeAds) {
            Application.LoadLevel("01");
        }
    }
}

We finished the steps we should do in Unity , now we just export our game to Windows Phone 8.X (I suggest to Windows Phone 8.1 , to be identical to mine), before opening Visual Studio , download and install Microsoft Universal Ad Client SDK : Download

With it installed, and the project open in Visual Studio :

1. Open the file Package.appxmanifest , in the Capabilities check box     the Internet(Client & Server) option.

2. Right click References and go to Add Reference... , search for something containing " AdMediator ", mark it and click OK .

3. Open the file MainPage.xaml.cs , and add the following references:

using Microsoft.Advertising.WinRT.UI;
using System.Net.NetworkInformation;

4. Within the class MainPage : Page declare the variable:

private InterstitialAd ad = new InterstitialAd();

5. Now within method MainPage() after line this.InitializeComponent(); call events of that Interop class we created in Unity :

Interop.RequestInterstitialEvent += Interop_RequestInterstitialEvent;
Interop.ShowInterstitialEvent += Interop_ShowInterstitialEvent;

Follow the test methods already ready for testing:

void Interop_RequestInterstitialEvent(object sender, EventArgs e)
{  // Quando chamamos RequestInterstitialAd, no script de movimentação do player
    // Define aquela variável como true ou false (se há de fato internet no aparelho)
    gameover.internet = NetworkInterface.GetIsNetworkAvailable();

    if (gameover.internet)
    {  /* se a internet estiver ativa, ele marca closeAds como false, 
          para ser feito o bloqueio dos controles do game enquanto o 
          anúncio estiver na tela. */
        gameover.closeAds = false;
    }
    else
    {  /* se a internet estiver desligada, ele marca closeAds como true, 
          para aceitar controles na tela onde tem o script gameover. */
        gameover.closeAds = true;
    }
}

void Interop_ShowInterstitialEvent(object sender, EventArgs e)
{ // Quando chamamos ShowInterstitialAd, no gameover.cs
    if (gameover.internet)
    {   // Apenas se a internet estiver ligada ele requiri o anúncio
        ad.RequestAd(AdType.Video, "d25517cb-12d4-4699-8bdc-52040c712cab", "11389925");
        ad.AdReady += ad_AdReady; // cria evento para quando carrega-lo
        ad.ErrorOccurred += ad_ErrorOccurred; // evento de erro ao carrega-lo
    }
}
private void ad_AdReady(object sender, object e)
{  // Quando anúncio tiver sido carregado
    ad.Show(); // mostra anúncio
    ad.Completed += ad_Completed; // quando usuário espera tempo do anúncio
    ad.Cancelled += ad_Cancelled; // quando usuário cancela o anúncio
}

private void ad_ErrorOccurred(object sender, AdErrorEventArgs e)
{  // Quando ocorre erro ao carregar o anúncio
    gameover.closeAds = true; // ele seta a variável como true para permiter touch
}

private void ad_Completed(object sender, object e)
{  // Quando usuário visualiza totalmente o anúncio
    gameover.closeAds = true; // ele seta a variável como true para permiter touch
}

private void ad_Cancelled(object sender, object e)
{  // Quando usuário cancela anúncio (apertando Escape ou no X)
    gameover.closeAds = true; // ele seta a variável como true para permiter touch
}

Remembering that the ad.RequestAd line should be changed by placing the id of your ad generated by Windows Development Panel . The ones I put there are the ones Microsoft makes available for testing.

    
17.01.2016 / 03:00