PubNative Android Plugin for Unity supports 2 ad formats:

Please follow this integration guide to configure PubNative ads.

Installation instructions

Requirements

  • Unity Project. - (This Plugin supports Unity 5 and up)
  • PubNative Unity Plugin. - It can be found here
  • An App Token provided in PubNative Dashboard.
  • A Placement Name configured and obtained from the PubNative Dashboard

Plugin integration

  • Open the project in the Unity Editor
  • With the project open, double-click on the downloaded PubNativeUnityPlugin file, all the required files will be imported into the project.
  • Make sure that the Assets/Plugins folder is created in it contains the Android folder inside.
  • Now the plugin is integrated and ready to begin the specific integration per ad format.

Caution

If you already have the project set up to be deployed in Android, importing the plugin might try to override your Manifest of gradle file. If that’s the case just keep your original file and add the dependencies from the plugin gradle file and the permissions from the Manifest.

Banner integration instructions

Creating the banner

The PNBanner should be created inside a script which is linked to a GameObject. It must be created using the PNBannerFactory createBanner() method which created a banner based on the platform that the application is being built for.

public static PNBanner createBanner(MonoBehaviour parent)

A sample of this instantiation is the following:

PNBanner banner = PNBannerFactory.createBanner (this);

In the previous sample this is an object which inherits from MonoBehaviour so it’s linked to a GameObject.

After creating the Banner you must set the appToken and placement properties:

banner.appToken = “App Token obtained from PubNative dashboard”;
banner.placement = “Placement Name configured and obtained from the PubNative Dashboard”;

Setting the listeners is also recommended to check the correct behavior of the ads. There are two listeners for PNBanner: ILoadListener() and ITrackListener().

public interface ILoadListener
{
    void OnLoadFinished ();
    void OnLoadFailed (Exception error);
} 

public interface ITrackListener
{
    void OnImpressionTracked ();
    void OnClickTracked ();
}

Adding a LoadListener to the PNBanner is mandatory since it will send callbacks when the ad was loaded or failed. After the ad is loaded then it can be displayed.

TrackListener is optional in case you want to receive callbacks when the ad has been impressed on screen or when the ad has been clicked.

Here’s an implementation sample:

banner.LoadListener = LOAD_LISTENER_INSTANCE;
banner.TrackListener = TRACK_LISTENER_INSTANCE;

Loading the banner

To load the Banner you should use the Load() method. This method must be invoked in the following way:

banner.Load ();

The result of this method will be received in the LoadListener callbacks.

Showing the banner

The Banner can be displayed in two positions on the screen: TOP or BOTTOM. After the ad is loaded successfully, the Show(Position position) method must be used to display the ad.

Here’s are samples of how to show the Banner on top or bottom of the screen.

banner.Show (PNBanner.Position.TOP); 
banner.Show (PNBanner.Position.BOTTOM);

Keep in mind, this method should be called always after receiving confirmation that the ad was loaded successfully. Otherwise, it’ll result in an error and nothing will be displayed.

Here is a complete code sample about how you can integrate the Banner

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BannerNative : MonoBehaviour, ILoadListener, ITrackListener
{
    private PNBanner banner;

    public string appToken;
    public string placement;

    [SerializeField]
    private Button _buttonLoadBanner;

    [SerializeField]
    private Button _buttonHideBanner;

    // Use this for initialization
    void Start ()
    {
        banner = PNBannerFactory.createBanner (this);
        banner.appToken = appToken;
        banner.placement = placement;
        banner.LoadListener = this;
        banner.TrackListener = this;
        _buttonLoadBanner.onClick.AddListener (RequestBanner);
        _buttonHideBanner.onClick.AddListener (HideBanner);
    }

    private void RequestBanner ()
    {
        if (banner != null) {
            banner.Load ();
        }
    }

    private void HideBanner ()
    {
        banner.Hide ();
    }

    public void OnLoadFinished ()
    {
        banner.Show (PNBanner.Position.TOP);
    }

    public void OnLoadFailed (Exception error)
    {
        // Handle error
    }

    public void OnImpressionTracked()
    {
        // Handle Impression
    }

    public void OnClickTracked ()
    {
        // Handle Click
    }
}

Interstitial integration instructions

Creating the interstitial

The PNInterstitial should be created inside a script which is linked to a GameObject. It must be created using the PNInterstitialFactory createInterstitial() method which created an interstitial based on the platform that the application is being built for.

public static PNInterstitial createInterstitial(MonoBehaviour parent)

A sample of this this instantiation is the following:

PNInterstitial interstitial = PNInterstitialFactory.createInterstitial (this);

In the previous sample this is an object which inherits from MonoBehaviour so it’s linked to a GameObject.

After creating the Interstitial you must set the appToken and placement properties:

interstitial.appToken = “App Token obtained from PubNative dashboard”;
interstitial.placement = “Placement Name configured and obtained from the PubNative Dashboard”;

Setting the listeners is also recommended to check the correct behavior of the ads. There are three listeners for PNInterstitial: ILoadListener(), ITrackListener() and IViewListener().

public interface ILoadListener
{
    void OnLoadFinished ();
    void OnLoadFailed (Exception error);
} 

public interface ITrackListener
{
    void OnImpressionTracked ();
    void OnClickTracked ();
} 

public interface IViewListener
{
    void OnShown ();
    void OnHidden ();
}

Adding a LoadListener to the PNInterstitial is mandatory since it will send callbacks when the ad was loaded or failed. After the ad is loaded then it can be displayed.

TrackListener is optional in case you want to receive callbacks when the ad has been impressed on screen or when the ad has been clicked.

ViewListener is optional in case you want to receive callbacks when the Interstitial has been shown and hidden.

Here’s an implementation sample:

interstitial.LoadListener = LOAD_LISTENER_INSTANCE;
interstitial.TrackListener = TRACK_LISTENER_INSTANCE; 
interstitial.ViewListener = VIEW_LISTENER_INSTANCE;

Loading the interstitial

To load the Interstitial you should use the Load() method. This method must be invoked in the following way:

interstitial.Load ();

The result of this method will be received in the LoadListener callbacks.

Showing the interstitial

After the ad is loaded successfully, the method Show() must be used to display the ad.

Here’s is the sample of how to show interstitial on screen.

interstitial.Show ();

Keep in mind, this method should be called always after receiving confirmation that the ad was loaded successfully. Otherwise, it’ll result in an error and nothing will be displayed.

Here is a complete code sample about how you can integrate the Interstitial.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class InterstitialNative : MonoBehaviour, ILoadListener, ITrackListener, IViewListener
{
    private PNInterstitial interstitial;

    public string appToken;
    public string placement;

    [SerializeField]
    private Button _buttonLoadInterstitial;

    // Use this for initialization
    void Start ()
    {
        interstitial = PNInterstitialFactory.createInterstitial (this);
        interstitial.appToken = appToken;
        interstitial.placement = placement;
        interstitial.LoadListener = this;
        interstitial.TrackListener = this;
        interstitial.ViewListener = this;
        _buttonLoadInterstitial.onClick.AddListener (RequestInterstitial);
    }

    private void RequestInterstitial ()
    {
        if (interstitial != null) {
            interstitial.Load();
        }
    }

    public void OnLoadFinished ()
    {
        interstitial.Show ();
    }

    public void OnLoadFailed (Exception error)
    {
        // Handle error
    }

    public void OnImpressionTracked()
    {
        // Handle Impression
    }

    public void OnClickTracked ()
    {
        // Handle Click
    }

    public void OnShown ()
    {
        // Handle interstitial show
    }

    public void OnHidden ()
    {
        // Handle interstitial hide
    }
}

Demo app on github

On the github repo you can find the demo project for unity and the generated Android project.

https://drive.google.com/file/d/0B-5uf-3n9OfFNG5zRlVjUVQ1YjA/view?usp=sharing