Monetize Unity 5 games on Windows Phone 8.X

Asked

Viewed 305 times

5

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

1 answer

7


To do this just follow the monetization steps of a common application of Windows Phone, 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 as the Unity 5, we should use the concept of interoperabilidade to display the ad at a certain point in the game.

Interoperabilidade is the capability of a system (computerised or not) to communicate transparently (or as closely as possible) with other system (similar or not).

Source: Wikipedia

I’ll show you how to include the ad Interstitial (the most common), for this must access its Development Panel and add an ad block to your game record.

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

Filing cabinet: 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);
        }
    }
}

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

using UnityEngine;
using System.Collections;

public class playermoviment : MonoBehaviour {

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

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

Filing cabinet: 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 method ShowInterstitialAd the moment we want to open the ad, we will also create public and static variables to control our ad, in case I will use the variables:

  • internet: will set if the internet of the device is not connected.
  • closeAds: will set if the ad was closed, to return to capture touch for the game.

Let’s go to the new code of 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 take in the Unity, now we just export our game to Windows Phone 8.X (suggest to Windows Phone 8.1, to be identical to mine), before opening the Visual Studio, download and install the Microsoft Universal Ad Client SDK: Download

With it installed, and the project opened on Visual Studio:

1 . Open the file Package.appxmanifest, on the flap Capabilities marque the option Internet(Client & Server).

2 . Click on References right click and go to Add Reference..., look for one that contains in the name "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 the method MainPage() after the line this.InitializeComponent(); call the events of that class Interop that we created in the Unity:

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

Follow the methods of events 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 line ad.RequestAd should be amended by id of your ad generated by Windows Development Panel. The ones I put there are the ones that Microsoft makes available for testing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.