partner-platforms-guidelines | Unity

Integrating Unity with the InMobi SDK is now 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 8.0 and later, iOS 7 and later, InMobi iOS SDK 600 or later
  • To deploy on Android - Android OS versions 4.0.1 (API level 15) and later, InMobi Android SDK 600 or later
SDK Version

iOS - 6.2.1

Android - 6.2.4

Size

iOS - 584 KB (.IPA Inflation)

Android - 330 KB (.APK Inflation)

Plugin

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 then choose 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 choose 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 ("4028cb8b2c3a0b45012c406824e800ba");
	}
	

Note: 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.

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.