Unity Engine Integration

Developers who have built their app on the Unity engine can now easily integrate the MoPub SDK along with our custom events using our set of plugins. Follow these steps:

Getting Started

  1. Download the latest build and unzip it.
  2. Import the MoPubUnityPlugin.unitypackage file into your Unity project.
  3. Choose the format you’d like to integrate and follow the instructions below for your format.

Integrating Banners

Here is how you would create a banner in the Unity Script file - make sure you only call this once in the lifecycle of your app:

    MoPub.createBanner( BANNER_ADUNIT_ID, MoPubAdPosition.BottomCenter );
To show or hide the banner (optional - the banner is shown by default) :
    MoPub.showBanner( true );
    MoPub.showBanner( false ); // hides the banner
Here are the banner-related events:
    public static event Action onAdLoadedEvent;
    public static event Action onAdFailedEvent;
    public static event Action onAdClickedEvent;
    public static event Action onAdExpandedEvent;
    public static event Action onAdCollapsedEvent;

Integrating Interstitials

To request and show an interstitial:

    MoPub.requestInterstitialAd( INTERSTITIAL_ADUNIT_ID );
    MoPub.showInterstitialAd( INTERSTITIAL_ADUNIT_ID );
Here are the interstitial related events:
    public static event Action onInterstitialLoadedEvent;
    public static event Action onInterstitialFailedEvent;
    public static event Action onInterstitialDismissedEvent;
    public static event Action onInterstitialExpiredEvent;
    public static event Action onInterstitialShownEvent;
    public static event Action onInterstitialClickedEvent

Integrating Rewarded Video

Here is how one would request and show a rewarded video:

    MoPub.initializeRewardedVideo();
    MoPub.requestRewardedVideo( ADUNIT_ID );
    MoPub.showRewardedVideo( ADUNIT_ID );
Here are the rewarded video related events:
    // Rewarded video events
    public static event Action onRewardedVideoLoadedEvent;
    public static event Action onRewardedVideoFailedEvent;
    public static event Action onRewardedVideoExpiredEvent;
    public static event Action onRewardedVideoFailedToPlayEvent;
    public static event Action onRewardedVideoReceivedRewardEvent;

Alternatively, one can specify the mediation settings like this:
Implement getMediationSettings as below for both android and IOS to return a list of settings. The keys in the key-value pairs should be as per the keys defined by the network.

    #if UNITY_IPHONE  
    // mediation settings vary based on platform so we use a simple helper  method to generate them here
    List getMediationSettings()
    {
        var adColonySettings = new MoPubMediationSetting( "AdColony" );
        adColonySettings.Add( "showPrePopup", false );
        adColonySettings.Add( "showPostPopup", true );

        var vungleSettings = new MoPubMediationSetting( "Vungle" );
        vungleSettings.Add( "userIdentifier", "the-user-id" );

        var mediationSettings = new List();
        mediationSettings.Add( adColonySettings );
        mediationSettings.Add( vungleSettings );

        return mediationSettings;
    }
    #else
    List getMediationSettings()
    {
        var adColonySettings = new MoPubMediationSetting( "AdColony" );
        adColonySettings.Add( "withConfirmationDialog", false );
        adColonySettings.Add( "withResultsDialog", true );

        var chartboostSettings = new MoPubMediationSetting( "Chartboost" );
        chartboostSettings.Add( "customId", "the-user-id" );

        var vungleSettings = new MoPubMediationSetting( "Vungle" );
        vungleSettings.Add( "userId", "the-user-id" );
        vungleSettings.Add( "cancelDialogBody", "Cancel Body" );
        vungleSettings.Add( "cancelDialogCloseButton", "Shut it Down" );
        vungleSettings.Add( "cancelDialogKeepWatchingButton", "Watch On" );
        vungleSettings.Add( "cancelDialogTitle", "Cancel Title" );

        var mediationSettings = new List();
        mediationSettings.Add( adColonySettings );
        mediationSettings.Add( chartboostSettings );
        mediationSettings.Add( vungleSettings );

        return mediationSettings;
    }

Integrating Third Party SDKs

The MoPub SDK can mediate most ad networks through our server-to-server integrations, pre-built adapters, and custom events capabilities. A full list of supported networks can be found here.

We offer pre-built adaptors for AdColony, AdMob, Chartboost, Facebook Audience Network, Millennial Media, UnityAds, and Vungle. To set these integrations up, follow the instructions below.

Android

  1. For Android, the developer does not need to import a separate Unity plugin for each of the networks - the adapters are all included by default.
  2. For the third-party networks, one also needs to place the network SDKs into the /Assets/Plugins folder within one’s Unity project. For Android, the path is [UNITY PROJECT ROOT]/Assets/Plugins/Android.

iOS

  1. Once one unzips the folder with the MoPub Unity plugins, the following files should appear:
  2. Import the Unity plugin for the networks necessary
  3. For the third-party networks, one also needs to place the network SDKs into the /Assets/Plugins folder within one’s Unity project. For iOS, the path is [UNITY PROJECT ROOT]/Assets/Editor/MoPub/NativeCode

Adding New Custom Events

Android
  1. Follow this guide to author the custom event class for Android
  2. Add the custom event adapters with their corresponding network SDKs as additional library jars to the existing libraries into the [UNITY PROJECT ROOT]/Assets/Plugins/Android folder. For ease of maintenance, we recommend keeping the custom event classes and the network SDKs as separate .jar files.
iOS
  1. Follow this guide to author the custom event class for iOS
  2. Create a separate folder in [UNITY PROJECT ROOT]/Assets/Editor/MoPub/NativeCode and add the SDKs and custom event files directly

Troubleshooting

  • As of the current latest Unity editor (5.0.2f1), occasionally the static libraries will not be added to the Build Phase settings correctly. If you encounter a build error about missing frameworks/libraries, remove and re-add them manually

Extras

Here’s how to report a conversion/app open event:

    MoPub.reportApplicationOpen("iTunesAppID");
Here’s how to enable/disable location passing:
    MoPub.enableLocationSupport( true );
    MoPub.enableLocationSupport( false );  // Disable
Note: the iAd adapters are bundled into the MoPub SDK and requires no steps from the developer.

Known limitations

  • Plugins for the Unity engine do not mediate native ad formats.
  • Developers should test to verify compatibility with other third party plugins.