Unity

Integrating InMobi SDK with Unity is easy.

You can get the following ad types from InMobi via Unity plugin on both Android and iOS apps:

  • Banner Ads
  • Interstitial (Static Interstitial and Full-screen Video Ads)
  • Rewarded Video Ads

You require the following to run the latest version of Unity plugin:

  • Unity 5 or later
  • To deploy on iOS - Xcode 12.0 and later, iOS 12 and later, InMobi iOS SDK 9.1.1 or later
  • To deploy on Android - Android OS version 4.1 (API level 16) and later, InMobi Android SDK 9.1.1 or later
SDK Version

iOS - 9.1.1

Android - 9.1.1

Size

iOS - 661 KB (.IPA Inflation)

Android - 350 KB (.APK Inflation)

Plugins

Unity Package for SDK 911

Adding the Plugin to your App

  1. Open your project in the Unity editor.
  2. Select Assets > Import Package > Custom Package, and then find the InMobiAdsUnityPlugin.unitypackage file you downloaded.

  3. Make sure all the files are selected, and then click Import.

Running your Project

  • iOS:
    • Navigate to File > Build Settings, select the iOS platform, and select Build to export an Xcode project and a corresponding CocoaPods xcworkspace.
    • Open up the exported xcworkspace, and then navigate to Product > Run in Xcode to run your project.
  • Android:
    • Navigate to File > Build Settings, select the Android platform, and then select Build and Run.

Unity Plugin API

You can use the common C# API in the InMobi Ads plugin to request banner, interstitial, and rewarded video ads. The code can be written once and deployed to both Android and iOS.

Providing Demographics Data

There are multiple APIs provided in InMobiPlugin Class to pass on Demographic information to InMobi SDK which helps us in better ad targeting.

Some of them are as follows:

//Set Age
public void SetAge(int age){
inmobiPluginClient.SetAge (age);
}
//Set PostalCode
public void SetPostalCode(string postalCode){
inmobiPluginClient.SetPostalCode (postalCode);
}
//Set Year of Birth
public void SetYearOfBirth(int yearOfBirth){
inmobiPluginClient.SetYearOfBirth (yearOfBirth);
}
//Set Gender GENDER_MALE or GENDER_FEMALE
public void SetGender(string gender){
inmobiPluginClient.SetGender (gender);
}

Initializing the InMobi SDK

You can use the InMobiPlugin Class to initialize the InMobi SDK. Account Id is required to initialize the InMobi SDK.

Here is the sample code to initialize the InMobi SDK:

private void initializeInMobiAds () {

//Account id used here is test account id   

InMobiPlugin inmobiPlugin = new InMobiPlugin(); 
    
    inmobiPlugin.OnInitializationCompleted += this.HandleInitializationStatus; 
    
    Dictionary<string, object=""> consentObject = new Dictionary<string, object="">(); 

#if UNITY_ANDROID 

consentObject.Add(InMobiPlugin.IM_GDPR_CONSENT_AVAILABLE, true); 

#elif (UNITY_5 && UNITY_IOS) || UNITY_IPHONE 

    consentObject.Add(InMobiPlugin.IM_GDPR_CONSENT_AVAILABLE, “true”); 
#endif 

    consentObject.Add(InMobiPlugin.IM_GDPR_CONSENT_IAB, "IAB_STRING"); 
    inmobiPlugin.Init("4028cb8b2c3a0b45012c406824e800ba", consentObject); 
}  
 
public void HandleInitializationStatus (object sender, SdkInitializationStatusArgs args) { 

Debug.Log("SDK Initialization status code : " + args.status.Code + " and reason : " + 
        args.status.Reason); 
}

Let’s understand a few things here before you proceed:

What is a consentObject? - A consentObject is a dictionary representation of all kinds of consent provided by the publisher to the SDK. The key is mandatory if you wish to monetize traffic from EEA region. You can read further on GDPR regulations here.

Key Type Inference
gdpr_consent String A consent string is a series of numbers, which identifies the consent status of an Adtech Vendor.
The string must follow the IAB contracts as mentioned here.
gdpr_consent_available String (iOS) For iOS, the value should be passed as String: "true": User has provided consent to collect and use data. "false": User has not provided consent to collect and use data.
Any value other than “true” and “false” is invalid and will be treated as value not provided by user. 
This key can be accessed via string constant IM_GDPR_CONSENT_IAB. 
Bool (Android) For Android, the value should be passed as Bool: true: User has provided consent to collect and use data. false: User has not provided consent to collect and use data.
Any value other than true and false is invalid and will be treated as value not provided by user.
This key can be accessed via string constant IM_GDPR_CONSENT_IAB.
gdpr String Whether or not the request is subjected to GDPR regulations (0 = No, 1 = Yes), omission indicates Unknown.

Note

As part of the General Data Protection Regulation (“GDPR”) publishers who collect data on their apps, are required to have a legal basis for collecting and processing the personal data of users in the European Economic Area (“EEA”). Please ensure that you obtain appropriate consent from the user before making ad requests to InMobi for Europe and indicate the same by following our recommended SDK implementation. Please do not pass any demographics information of a user; if you do not have user consent from such user in Europe.

It is mandatory to initialize the InMobi SDK before making any ad request to InMobi. It is advised to do it at the start of the app itself.

Passing GDPR values

You must provide a consentObject in every session. SDK does not persist consent, it only keeps the consentObject in memory. If the app is relaunched, SDK will lose the consentObject. Within a session, a consentObject can be updated as below:

Dictionary<string, object=""> consentObject = new Dictionary<string, object="">(); 

    #if UNITY_ANDROID

        consentObject.Add(InMobiPlugin.IM_GDPR_CONSENT_AVAILABLE, true);

    #elif (UNITY_5 && UNITY_IOS) || UNITY_IPHONE

        consentObject.Add(InMobiPlugin.IM_GDPR_CONSENT_AVAILABLE, “true”); 

    #endif 

    consentObject.Add(InMobiPlugin.IM_GDPR_CONSENT_IAB, "IAB_STRING"); 

    inmobiPlugin.UpdateGdprConsentObject(consentObject);

Integrating Banner Ads

Following is the minimal code to create and load a banner ad:

private void RequestBanner ()
{
  // These placement Ids are configured to always serve test ads.
  #if UNITY_ANDROID
  string placementId = "1467162141987";
  #elif UNITY_IPHONE
  string placementId = "1464947431995";
  #endif
  // Create a 320x50 banner at the bottom of the screen.
  this.bannerAd = new BannerAd (placementId, 320, 50, (int)InMobiAdPosition.BottomCenter);
  // Register for ad events.
  this.bannerAd.OnAdLoadSucceeded += this.HandleOnAdLoadSucceeded;
  this.bannerAd.OnAdLoadFailed += this.HandleAdLoadFailed;
  this.bannerAd.OnAdDisplayed += this.HandleAdDisplayed;
  this.bannerAd.OnAdDismissed += this.HandleAdDismissed;
  this.bannerAd.OnAdInteraction += this.HandleAdInteraction;
  this.bannerAd.OnUserLeftApplication += this.HandleUserLeftApplication;
  // Load a banner ad.
  this.bannerAd.LoadAd ();
}

InMobiAdPosition enum specifies where to place the banner.

Note

Each platform, iOS and Android, requires a unique placement Id. Rest of the APIs remain same for both platforms.

Integrating Interstitial Ads

Please note that the integration for interstitial and full-screen video ads is same. Following is the minimal code to load an interstitial ad:

  private void RequestInterstitial ()
{
  // These placement Ids are configured to always serve test ads.
  #if UNITY_ANDROID
  string placementId = "1469137441636";
  #elif UNITY_IPHONE
  string placementId = "1467548435003";
  #endif
  // Create an interstitial.
  this.interstitialAd = new InterstitialAd (placementId);
  // Register for ad events.
  this.interstitialAd.OnAdLoadSucceeded += this.HandleOnAdLoadSucceeded;
  this.interstitialAd.OnAdLoadFailed += this.HandleAdLoadFailed;
  this.interstitialAd.OnAdDisplayed += this.HandleAdDisplayed;
  this.interstitialAd.OnAdDismissed += this.HandleAdDismissed;
  this.interstitialAd.OnAdInteraction += this.HandleAdInteraction;
  this.interstitialAd.OnUserLeftApplication += this.HandleUserLeftApplication;
  this.interstitialAd.OnAdReceived += this.HandleAdReceived;
  this.interstitialAd.OnAdDisplayFailed += this.HandleAdDisplayFailed;
  this.interstitialAd.OnAdWillDisplay += this.HandleAdWillDisplay;
  // Load an interstitial ad.
  this.interstitialAd.LoadAd ();
}

Interstitial ads need to be shown explicitly, but before that check if interstitial ad is ready or not by calling isReady () as depicted below:

  if (this.interstitialAd.isReady ()) {
   this.interstitialAd.Show ();
  } else {
   MonoBehaviour.print ("Interstitial is not ready yet");
  }

A good place to show an interstitial ad is end of a level or end of session.

Integrating Rewarded Video Ads

Following is a minimal code to load a rewarded video ad:

  private void RequestRewardBasedVideo ()
{
  // These placement Ids are configured to always serve test ads.
  #if UNITY_ANDROID
  string placementId = "1453753057988";
  #elif UNITY_IPHONE
  string placementId = "1465883204802";
  #endif
  this.rewardedVideoAd = new RewardedVideoAd (placementId);
  // Register for ad events.
  this.rewardedVideoAd.OnAdLoadSucceeded += this.HandleOnAdLoadSucceeded;
  this.rewardedVideoAd.OnAdLoadFailed += this.HandleAdLoadFailed;
  this.rewardedVideoAd.OnAdDisplayed += this.HandleAdDisplayed;
  this.rewardedVideoAd.OnAdDismissed += this.HandleAdDismissed;
  this.rewardedVideoAd.OnAdInteraction += this.HandleAdInteraction;
  this.rewardedVideoAd.OnUserLeftApplication += this.HandleUserLeftApplication;
  this.rewardedVideoAd.OnAdReceived += this.HandleAdReceived;
  this.rewardedVideoAd.OnAdDisplayFailed += this.HandleAdDisplayFailed;
  this.rewardedVideoAd.OnAdWillDisplay += this.HandleAdWillDisplay;
  this.rewardedVideoAd.OnAdRewardActionCompleted += this.HandleRewardActionCompleted;
  this.rewardedVideoAd.LoadAd ();
}

Rewarded video ads work in a similar way to interstitial ads and need to be checked whether they are ready before calling show ().

  private void ShowRewardBasedVideo ()
{
  if (this.rewardedVideoAd.isReady ()) {
   this.rewardedVideoAd.Show ();
  } else {
   MonoBehaviour.print ("Rewarded video ad is not ready yet");
  }
}

Handling Events

You can set the following ad events on a banner, interstitial, and rewarded video ad.

 #region callback handlers
   public void HandleOnAdLoadSucceeded (object sender, EventArgs args)
{
  MonoBehaviour.print ("HandleOnAdLoadSucceded event received");
}
   public void HandleAdLoadFailed (object sender, AdLoadFailedEventArgs args)
{
  MonoBehaviour.print ("HandleAdLoadFailed event received with message: " + args.Error);
}
   public void HandleAdDisplayed (object sender, EventArgs args)
{
  MonoBehaviour.print ("HandleAdDisplayed event received");
}
  public void HandleAdDismissed (object sender, EventArgs args)
{
  MonoBehaviour.print ("HandleAdDismissed event received");
}
  public void HandleAdInteraction (object sender, AdInteractionEventArgs args)
{
  MonoBehaviour.print ("HandleAdDismissed event received " + args.Message);
}
  public void HandleUserLeftApplication (object sender, EventArgs args)
{
  MonoBehaviour.print ("HandleUserLeftApplication event received");
}
#endregion
#region Interstitial specific callback handlers
public void HandleAdReceived (object sender, EventArgs args)
{
  MonoBehaviour.print ("HandleAdReceived event received");
}
  public void HandleAdWillDisplay (object sender, EventArgs args)
{
  MonoBehaviour.print (
   "HandleAdWillDisplay event received with message: ");
}
  public void HandleAdDisplayFailed (object sender, EventArgs args)
{
  MonoBehaviour.print ("HandleAdDisplayFailed event received");
}
   #endregion
  #region RewardBasedVideo specific callback handlers
   public void HandleRewardActionCompleted (object sender, AdRewardActionCompletedEventArgs args)
{
  MonoBehaviour.print (
   "HandleRewardActionCompleted event received for " + args.Rewards);
}
  #endregion

Note

The args.Rewards, specific to rewarded video ad, is in JSON String format.

Was this page helpful?

On This Page