AI Search

统一

将 InMobi SDK 与 Unity 集成非常简单。

您可以通过 Unity 插件在 Android 和 iOS 应用上从 InMobi 获取以下几种广告类型:

  • 横幅广告
  • 插屏广告(静态插屏广告和全屏视频广告)
  • 激励视频广告

要运行最新版本的 Unity 插件,您需要以下条件:

  • Unity 5 或更高版本
  • 要在 iOS 上部署,需要 Xcode 12.0 及更高版本、iOS 12 及更高版本以及 InMobi iOS SDK 9.2.1 或更高版本。
  • 要在 Android 上部署,需要 Android 操作系统版本 4.1(API 级别 16)及更高版本,以及 InMobi Android SDK 9.2.1 或更高版本。
SDK 版本

iOS - 10.0.1

Android - 10.0.1

尺寸

iOS - 692 KB(.IPA 文件膨胀)

Android - 375.341 KB(.APK 膨胀)

插件

Unity 软件包,适用于 SDK 10.8.8 或更高版本

将插件添加到您的应用

  1. 在 Unity 编辑器中打开你的项目。
  2. 选择“资产导入包”> “自定义包”,然后搜索InMobiAdsUnityPlugin.unitypackage您下载的文件。

  3. 确保选中所有文件,然后单击“导入”

运行您的项目

  • iOS:
    • 导航至“文件” >“构建设置”,选择 iOS 平台,然后选择“构建”以导出 Xcode 项目和相应的 CocoaPods xcworkspace。
    • 打开导出的 xcworkspace,然后在 Xcode 中导航到“产品运行”来运行您的项目。
  • 安卓:
    • 导航至文件构建设置,选择 Android 平台,然后选择 构建并运行

Unity插件

您可以使用 InMobi Ads 插件中的通用 C# API 来请求横幅广告、插页式广告和激励视频广告。代码只需编写一次,即可部署到 Android 和 iOS 平台。

提供人口统计数据

Class中提供了多个 API,InMobiPlugin可以将人口统计信息传递给 InMobi SDK,这有助于我们更好地进行广告定向。

其中一些如下:

//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);
}

正在初始化 InMobi SDK

您可以使用InMobiPlugin此类初始化 InMobi SDK。初始化 InMobi SDK 需要账户 ID。

以下是初始化 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); 
}

在您继续之前,让我们先了解几件事:

什么是 consentObject? - consentObject是一个字典,用于表示发布商向 SDK 提供的所有类型的同意信息。如果您希望通过来自欧洲经济区 (EEA) 的流量获利,则此键是必需的。您可以在此处consentObject阅读更多关于 GDPR 法规的信息

钥匙 类型 推理
GDPR 同意 细绳 授权字符串是一串数字,用于标识广告技术供应商的授权状态。
该字符串必须符合此处所述的 IAB 合同要求。
gdpr_consent_available 字符串(iOS) 对于 iOS,该值应以字符串形式传递:“true”:用户已同意收集和使用数据。“false”:用户未同意收集和使用数据。
除“true”和“false”之外的任何值均无效,并将被视为用户未提供的值。 
可通过字符串常量 IM_GDPR_CONSENT_IAB 访问此键。 
布尔值(Android) 对于 Android 系统,该值应以布尔值形式传递:true 表示用户已同意收集和使用数据;false 表示用户未同意收集和使用数据。
除 true 和 false 之外的任何值均无效,并将被视为用户未提供的值。
此键可通过字符串常量 IM_GDPR_CONSENT_IAB 访问。
GDPR 细绳 请求是否受 GDPR 法规约束(0 = 否,1 = 是),省略表示未知。

笔记

根据《通用数据保护条例》(“GDPR”),在其应用上收集数据的发布商必须具备收集和处理欧洲经济区(“EEA”)用户个人数据的合法依据。请确保在向 InMobi 向欧洲地区发送广告请求之前,已获得用户的适当同意,并按照我们推荐的 SDK 实现方式进行相应说明。如果您未获得欧洲用户的任何同意,请勿传递任何用户的统计信息。

在向 InMobi 发出任何广告请求之前,必须先初始化 InMobi SDK。建议在应用启动时就进行初始化。

年龄限制 API

为了符合《儿童在线隐私保护法》(COPPA) 的规定,您可以指定用户是否为儿童。如果用户在美国未满 13 周岁,或在其他地区未满适用年龄,请设置为 true。


// Set age restriction for COPPA compliance
// Set to true if the user is a child (under 13 in US)
inMobiPlugin.SetAgeRestriction(true);

发布商信号

出版商信号

发布商信号允许您传递上下文数据,以改进广告定位和收益。您可以传递有关用户行为、流量来源和其他属性的信息,从而帮助提升广告效果。

设置发布商信号

创建一个包含信号键值对的字典,然后将其传递给 SDK。此方法必须在 InMobi SDK 成功初始化后调用。请确保在每次广告请求之前调用此函数并更新值。


// Create a dictionary with all required signals and corresponding values
Dictionary<string, object=""> publisherSignals = new Dictionary<string, object="">();

// User monetization signals
publisherSignals.Add("iap", true);              // User has made IAP
publisherSignals.Add("ua_type", "paid");        // User is acquired via paid medium
publisherSignals.Add("iap_type", "minnow");     // User has made low purchases
publisherSignals.Add("iaa_type", "whale");      // User has seen high number of ads

// Session and impression depth signals
publisherSignals.Add("s_dep_rew", 4);           // User has seen 4 rewarded ads in current session
publisherSignals.Add("i_dep_rew", 10);          // User has seen 10 rewarded ads in lifetime

// Performance signals
publisherSignals.Add("ctr_rew", 9.25);          // Historical CTR for user is 9.25%
publisherSignals.Add("abp_5_rew", 0.01720);     // Average winning bid price for last 5 rewarded auctions was $0.01720, eCPM is $17.2

// Pass the signals to InMobi SDK
inMobiPlugin.PutPublisherSignals(publisherSignals);
</string,></string,>

获取出版商信号

您可以检索当前设置的信号,以验证正在传递哪些数据。


// Check what signals are set already
Dictionary<string, object=""> currentSignals = inMobiPlugin.GetPublisherSignals();</string,>

重置发布者信号

必要时清除用户的所有信号数据。


// Clear all the signals data for the user
inMobiPlugin.ResetPublisherSignals();

符合 GDPR 价值观

您必须在每个会话中提供 consentObject。SDK 不会持久化同意信息,它只会将 consentObject 保存在内存中。如果应用重新启动,SDK 将丢失 consentObject。在会话期间,可以按如下方式更新 consentObject:

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);

集成横幅广告

以下是创建和加载横幅广告所需的最简代码:

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;    // Called when ad is loaded successfully
this.bannerAd.OnAdLoadFailed += this.HandleAdLoadFailed;            // Called when ad fails to load
this.bannerAd.OnAdFetchSuccessful += this.HandleAdFetchSuccessful;  // Called when ad content is fetched successfully
this.bannerAd.OnAdFetchFailed += this.HandleAdFetchFailed;          // Called when ad fetch fails
this.bannerAd.OnAdImpression += this.HandleAdImpression;            // Called when ad impression is recorded
this.bannerAd.OnAdDisplayed += this.HandleAdDisplayed;              // Called when ad is displayed on screen
this.bannerAd.OnAdDismissed += this.HandleAdDismissed;              // Called when ad is dismissed
this.bannerAd.OnAdInteraction += this.HandleAdInteraction;          // Called when user interacts with ad
this.bannerAd.OnUserLeftApplication += this.HandleUserLeftApplication; // Called when user leaves app via ad
// Load a banner ad.
  this.bannerAd.LoadAd ();
}

InMobiAdPosition枚举类型指定横幅的放置位置。

笔记

iOS 和 Android 平台都需要一个唯一的广告位 ID。其余 API 在两个平台上保持一致。

添加 SetExtras 方法


// Set extras for ad targeting. All values must be passed as string.
Dictionary<string, string=""> extras = new Dictionary<string, string="">();
extras.Add("tp", "admob");           // Mediation partner name
extras.Add("tp_ver", "19.0.0");      // Mediation partner SDK version
this.bannerAd.SetExtras(extras);

</string,></string,>

整合插页式广告

请注意,插屏广告和全屏视频广告的集成方式相同。以下是加载插屏广告所需的最简代码:

  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;    // Called when ad is loaded successfully
this.interstitialAd.OnAdLoadFailed += this.HandleAdLoadFailed;            // Called when ad fails to load
this.interstitialAd.OnAdFetchSuccessful += this.HandleAdFetchSuccessful;  // Called when ad content is fetched successfully
this.interstitialAd.OnAdFetchFailed += this.HandleAdFetchFailed;          // Called when ad fetch fails
this.interstitialAd.OnAdImpression += this.HandleAdImpression;            // Called when ad impression is recorded
this.interstitialAd.OnAdDisplayed += this.HandleAdDisplayed;              // Called when ad is displayed on screen
this.interstitialAd.OnAdDismissed += this.HandleAdDismissed;              // Called when ad is dismissed
this.interstitialAd.OnAdInteraction += this.HandleAdInteraction;          // Called when user interacts with ad
this.interstitialAd.OnUserLeftApplication += this.HandleUserLeftApplication; // Called when user leaves app via ad
  this.interstitialAd.OnAdDisplayFailed += this.HandleAdDisplayFailed;
  this.interstitialAd.OnAdWillDisplay += this.HandleAdWillDisplay;
  // Load an interstitial ad.
  this.interstitialAd.LoadAd ();
}

插页式广告需要明确显示,但在此之前,需要通过调用isReady ()如下所示的函数来检查插页式广告是否已准备就绪:

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

插页式广告的最佳展示位置是关卡末尾或会话末尾。

添加 SetExtras 方法


// Set extras for ad targeting. All values must be passed as string.
Dictionary<string, string=""> extras = new Dictionary<string, string="">();
extras.Add("tp", "admob");           // Mediation partner name
extras.Add("tp_ver", "19.0.0");      // Mediation partner SDK version
this.interstitialAd.SetExtras(extras);

</string,></string,>

整合激励视频广告

以下是加载激励视频广告的最小代码:

  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.OnAdFetchSuccessful += this.HandleAdFetchSuccessful;
  this.rewardedVideoAd.OnAdFetchFailed += this.HandleAdFetchFailed;
  this.rewardedVideoAd.OnAdDisplayed += this.HandleAdDisplayed;
  this.rewardedVideoAd.OnAdDismissed += this.HandleAdDismissed;
  this.rewardedVideoAd.OnAdImpression += this.HandleAdImpression;
  this.rewardedVideoAd.OnAdInteraction += this.HandleAdInteraction;
  this.rewardedVideoAd.OnUserLeftApplication += this.HandleUserLeftApplication;
  this.rewardedVideoAd.OnAdDisplayFailed += this.HandleAdDisplayFailed;
  this.rewardedVideoAd.OnAdWillDisplay += this.HandleAdWillDisplay;
  this.rewardedVideoAd.OnAdRewardActionCompleted += this.HandleRewardActionCompleted;
  this.rewardedVideoAd.LoadAd ();
}

激励视频广告的工作方式与插屏广告类似,需要在拨打电话前检查是否准备就绪show ()

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

事件处理

您可以为横幅广告、插页式广告和激励视频广告设置以下广告事件。

 #region callback handlers
   public void HandleOnAdLoadSucceeded(object sender, AdLoadSuccessEventArgs args)
{
    MonoBehaviour.print("HandleOnAdLoadSucceeded event received");
    // Access ad metadata from the event args
    MonoBehaviour.print("Creative ID: " + args.AdMetaInfo.CreativeID);
    MonoBehaviour.print("Bid: " + args.AdMetaInfo.Bid);
    MonoBehaviour.print("Bid Keyword: " + args.AdMetaInfo.BidKeyword);
}
   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);
}
 
public void HandleAdFetchSuccessful(object sender, AdFetchSuccessEventArgs args)
{
    // Called when ad content is successfully fetched
    // Access ad metadata from the event args
    MonoBehaviour.print("HandleAdFetchSuccessful event received");
    MonoBehaviour.print("Creative ID: " + args.AdMetaInfo.CreativeID);
    MonoBehaviour.print("Bid: " + args.AdMetaInfo.Bid);
}

public void HandleAdFetchFailed(object sender, AdFetchFailedEventArgs args)
{
    // Called when ad fetch fails
    MonoBehaviour.print("HandleAdFetchFailed event received");
    MonoBehaviour.print("Error: " + args.Error);
}

public void HandleAdImpression(object sender, EventArgs args)
{
    // Called when an ad impression is recorded
    MonoBehaviour.print("HandleAdImpression event received");
}

#endregion

笔记

args.Rewards特指激励视频广告)采用 JSON 字符串格式。

新的 EventArgs 类

班级 特性
广告获取成功事件参数 广告元数据信息
广告获取失败事件参数 错误(字符串)
广告加载成功事件参数 广告元数据信息

广告元数据属性

财产 类型 描述
创意ID 细绳 广告素材的唯一标识符
出价 双倍的 广告出价
竞价信息 JObject 补充投标信息
竞价关键词 细绳 与出价相关的关键词

本页内容

最后更新于 : 10 Sep, 2026