本文档介绍了如何通过 now.gg Payments 快速集成流程,在 Unity 游戏中启用应用内购买功能。
以下是标准集成与快速集成的对比:
| 项目 | 快速集成 | 标准集成 |
|---|---|---|
| 集成流程 | 如果您的 IAP 实现基于 Unity IAP v4.x.x 或 v5.x.x,请使用快速集成流程。 | 对于其他所有支付集成需求,请使用 标准集成流程。 |
| 消耗型与订阅型商品 | 快速集成流程仅支持一次性消耗型商品。 | 标准集成流程同时支持消耗型商品和订阅型商品。 |
now.gg Payments Unity 模块以 Unity 包文件 NowGGUnitySdk.unitypackage 的形式提供。
将模块添加到您的 Unity 项目:
NowGGUnitySdk.unitypackage。
Assets/Plugins/Android 目录中。在成功导入 now.gg Payments 模块并添加依赖后,您可以按照本节说明在您的应用/游戏中实现 now.gg Payments。
配置 now.gg Purchasing 模块需要执行以下步骤:
StandardPurchasingModule.Instance() 替换为 now.gg 购买模块 NowGG.Sdk.NowGGPurchasingModule.Instance()。PaymentId 传递给 now.gg Payments 模块。示例代码如下:
public void Start()
{
/* Current code
var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
*/
// Replace with
var builder = ConfigurationBuilder.Instance(NowGG.Sdk.NowGGPurchasingModule.Instance());
NowGG.Sdk.NowGGPaymentsSdkManager.Instance.PaymentId = "<your_Payment_ID_here>";
}
StandardPurchasingModule 实例。PaymentId 是您应用的唯一标识符,可在 nowStudio 的 App Details 页面中查看。了解更多。使用 Unity IAP v5 配置 now.gg Payments 需要执行以下步骤:
NowGGStoreController.Init() 初始化 now.gg Store Controller,并传入应用的 PaymentId。await m_StoreController.Connect() 建立与商店的连接。m_StoreController.FetchProducts(...) 获取所需商品。
m_StoreController.FetchPurchases() 获取已有购买记录。示例代码如下:
StoreController m_StoreController;
async void InitializeIAP()
{
/* Current code
m_StoreController = UnityIAPServices.StoreController();
*/
// Replace with
m_StoreController = NowGGStoreController.Init(<"your_PaymentID_here">);
m_StoreController.OnPurchasePending += OnPurchasePending;
await m_StoreController.Connect();
m_StoreController.OnProductsFetched += OnProductsFetched;
m_StoreController.OnPurchasesFetched += OnPurchasesFetched;
var initialProductsToFetch = new List<ProductDefinition>
{
new(goldProductId, ProductType.Consumable),
new(diamondProductId, ProductType.Consumable)
};
m_StoreController.FetchProducts(initialProductsToFetch);
}