IAP Promo
内购推荐 (IAP Promo) 商品目录

内购推荐 (IAP Promo) 集成

概述

重要注意事项:游戏必须在初始化 Unity Ads 之前先初始化 Unity IAP 才能正常使用内购推荐 (IAP Promo)。

本集成指南涵盖四个主要步骤:

在 Unity Editor 中准备项目

设置 Unity 服务

要使用内购推荐 (IAP Promo),您需要:

1.配置项目以使用 Unity 服务。 2. Enable the Unity IAP SDK (1.2+) and Unity Ads SDK (2.3+) in your Project.

设置 Unity IAP

IAP Promo requires a supported version of the Unity IAP SDK (1.17+). To acquire the latest IAP SDK, either enable In-App Purchasing in the Services window (Window > Services), or import it from the Asset store. If you’re enabling it from the Services window, be sure to Import the Asset package when prompted.

在 Editor 的 Services 窗口中启用 Unity IAP
在 Editor 的 Services 窗口中启用 Unity IAP

请参阅有关设置 IAP 的文档以了解其他信息。

设置 Unity Ads

IAP Promo requires a supported version of the Unity Ads SDK. Unity recommends acquiring the latest Ads SDK (3.0+) by importing it from the Asset store.

请参阅设置 Unity Ads 以了解其他信息。

实现

在设置所需服务后,即可在游戏中实现它们。

实现 IAP

There are two options for initialization: codeless or scripting.

使用 Codeless IAP

Codeless IAP 能为您处理初始化。如果使用 Codeless IAP 初始化,必须在代码的其他位置调用 Unity Ads 初始化方法。

要使用 Codeless IAP,请填充__商品目录__ (Product Catalog),然后创建 IAP 监听器 (IAP Listener) 来获取该目录:

  1. In the Editor, select Window > UnityIAP > IAP Catalog to open the IAP Catalog window. This window lists all of your previously configured Products. You must have at least one Product configured in your Product Catalog. For a complete walkthrough on setting up Products, see Codeless IAP.

  2. In the IAP Catalog window, select App Store Export > Cloud JSON to export a local copy of the Product Catalog.
    Exporting an IAP Product Catalog to JSON

3.创建 IAP 监听器。选择 Window > Unity IAP > Create IAP Listener,然后将其添加到游戏的第一个场景。游戏一启动,监听器就会获取__商品目录。这样可以避免在游戏请求__推荐 (Promotions) 而__商品__没有准备好(因为无码按钮尚未出现在场景中)时发生错误。

使用脚本

If you do not use Codeless IAP, you must initialize Unity IAP manually through a script. See the following code example:

using UnityEngine;
using UnityEngine.Purchasing;

public class IAPManager : MonoBehaviour, IStoreListener {
  private IStoreController controller;
  
  //The following products must be added to the Product Catalog in the Editor:
  private const string coins100 = "100.gold.coins";
  private const string coins500 = "500.gold.coins";

  public int coin_count = 0;

  void Awake () { 
    StandardPurchasingModule module = StandardPurchasingModule.Instance (); 
    ProductCatalog catalog = ProductCatalog.LoadDefaultCatalog (); 
    ConfigurationBuilder builder = ConfigurationBuilder.Instance (module);
    IAPConfigurationHelper.PopulateConfigurationBuilder (ref builder, catalog); 
    UnityPurchasing.Initialize (this, builder); 
  }

  public void OnInitialized (IStoreController controller, IExtensionProvider extensions) { 
    this.controller = controller; Debug.Log ("Initialization Successful"); 
  }
  
  public void OnInitializeFailed(InitializationFailureReason error) { 
    Debug.Log ("UnityIAP.OnInitializeFailed (" + error + ")")
  }
  
  public void OnPurchaseFailed (Product item, PurchaseFailureReason reason) { 
    Debug.Log("UnityIAP.OnPurchaseFailed (" + item + ", " + reason + ")"); 
  }

  public PurchaseProcessingResult ProcessPurchase (PurchaseEventArgs e) {
    string purchasedItem = e.purchasedProduct.definition.id;
    
    switch (purchasedItem) { 
      case coins100: Debug.Log ("Congratulations, you are richer!"); 
      coin_count += 100; 
      Debug.Log ("IAPLog: Coin count: " + coin_count); 
      break; 
      
      case coins500: Debug.Log ("Congratulations, you are richer!"); 
      coin_count += 500; 
      Debug.Log ("IAPLog: Coin count: " + coin_count);
      break;
    }
    return PurchaseProcessingResult.Complete;
  }

  public void Buy(string productId) { 
    Debug.Log ("UnityIAP.BuyClicked (" + productId + ")");
    controller.InitiatePurchase (productId);
  }
}

实现 Unity Ads

You must also initialize Unity Ads, whether or not you use the Codeless or manual IAP initialization method. The following code sample illustrates an initialization method to invoke:

using UnityEngine;
using UnityEngine.Monetization;

public class AdManager : MonoBehaviour {

  public bool testMode = true;
  private const string adPlacement = "video";
  private const string promoPlacement = "promo";

  #if UNITY_IOS
    private string gameId = "0000000"; // Your iOS game ID here
  #elif UNITY_ANDROID
    private string gameId = "9999999"; // Your Android game ID here
  #else
    private string gameId = "0123456"; // Prevents Editor Errors
  #endif

  private void Awake () {
    if (Monetization.isSupported && !Monetization.isInitialized) {
      Monetization.Initialize (gameId, testMode); 
    }
  }

  public void ShowVideoAd () { 
    ShowAdPlacementContent ad = Monetization.GetPlacementContent (adPlacement) as ShowAdPlacementContent; ad.Show (); 
  }

  public void ShowPromo () { 
    PromoAdPlacementContent promo = Monetization.GetPlacementContent (promoPlacement) as PromoAdPlacementContent; promo.Show (); 
  }
}

在开发者控制面板 (Developer Dashboard) 中配置推荐 (Promotions)

Navigate to the Monetization section of the Operate Dashboard to configure your IAP Promo offers:

  • Use Placements to control when and how your Promotions display in-game. Follow the Placements guide to create and configure your Promo Placements for IAP content.
  • 使用商品界面可导入__商品目录__并管理每个商品的创意资源。
  • 定义推荐 (Promotions) 的参数,例如运行时间、包含的__广告位__和__商品__以及目标用户。

测试集成结果

通过实现以下示例代码来调用内购推荐 (IAP Promo) 内容:

public void ShowPromo()
{
    Advertisement.Show (placementID);
}

Press Play in the Editor to check that a test ad appears when the Placement makes its request. To see real promotional creative assets, you must build the game to a device in production mode.




IAP Promo
内购推荐 (IAP Promo) 商品目录