メインコンテンツまでスキップ

应用内购买集成(Unity 插件)

iOS Billing SDK 实现了 Aptoide 计费。其 Unity 插件为 Unity 游戏提供了与 SDK 通信的接口。该插件构建于 Unity IAP v5 之上,将 AppCoins 注册为自定义商店提供方,因此您只需使用熟悉的 Unity IAP StoreController API,而在受支持的设备上由 AppCoins 负责底层计费。

在现有工程中接入 AppCoins​

📘

已经在使用 Unity IAP v5? AppCoins Unity 插件在设计上就是作为 Unity IAP v5 的自定义商店提供方构建的——StoreController、OnPurchasePending 和 ConfirmPurchase 就是 AppCoins 的 API。这是有意为之,目的是减少您的工作量。如果您已经在使用 Unity IAP v5,整个集成只需在现有 Connect() 调用之前加上一行代码。

在 Unity IAP v5 基础上接入 AppCoins

其他现有配置:

概览​

使用该插件后,您应用中的计费流程如下:

  1. 添加 AppCoins Unity 插件和 Unity IAP 包;
  2. 调用 AppCoinsIAP.ConfigureStoreAsync() 选择当前生效的计费商店;
  3. 通过 controller.FetchProducts() 注册您的商品;
  4. 使用标准的 Unity IAP v5 StoreController 连接商店;
  5. 通过 OnProductsFetched 事件接收已获取的应用内商品;
  6. 用户希望购买某个商品——调用 controller.PurchaseProduct(productId);
  7. 在 OnPurchasePending 中处理结果、发放商品,并通过 controller.ConfirmPurchase(order) 确认;
  8. 应用将商品发放给用户。

要求​

  1. Unity 2022.3 或更高版本。
  2. Unity IAP 包 v5.0 或更高版本(com.unity.purchasing)。
  3. 启用 AppCoins 计费需要 iOS 17.4 或更高版本。在更早的 iOS 版本上,Apple 计费仍可正常工作。

分步指南​

配置​

  1. 添加 AppCoins Unity 插件

    从 GitHub releases 页面 下载最新的 .unitypackage。

    将其导入工程的方法:

    1. 在 Unity 中依次选择 Assets → Import Package → Custom Package...
    2. 选择已下载的 .unitypackage 文件。
    3. 在 Import Unity Package 对话框中确认所有项目均已选中,然后点击 Import。

    插件文件将出现在 Assets/Plugins/iOS/AppCoinsSDKPlugin/ 下。

  2. 通过 Package Manager 添加 Unity IAP

    打开 Window → Package Manager,搜索 In App Purchasing(com.unity.purchasing),并安装 5.0 或更高版本。

实现​

插件配置完成后,在任何使用其 API 的文件中导入 AppCoins 命名空间:

using AppCoins.Unity;

1. 配置 AppCoins​

在连接商店之前调用 AppCoinsIAP.ConfigureStoreAsync()。该调用会选择将使用哪套计费系统,并注册相应的 Unity IAP 商店。

var selectedStore = await AppCoinsIAP.ConfigureStoreAsync(AppCoinsStoreMode.Automatic);
Debug.Log("Selected store: " + selectedStore);

AppCoinsStoreMode 枚举控制选择策略:

模式行为
Automatic在 iOS 17.4 及以上、且安装来源不是 App Store 或 TestFlight(包括模拟器)时使用 AppCoins;在更早的 iOS、App Store 和 TestFlight 上回落到 Apple。
AppCoins无论设备状态如何,始终使用 AppCoins 计费。
Apple始终使用 Apple 计费。

调用返回后,AppCoinsIAP.SelectedStore 的值为 AppCoinsIAP.AppCoinsStoreName("AppCoinsAppStore")或 AppCoinsIAP.AppleStoreName("AppleAppStore")。关于在 Automatic 模式下哪些安装来源会启用 AppCoins 计费的完整说明,请参见运行时检测的原理。

⚠️
必须在调用 controller.Connect() 之前调用并 await ConfigureStoreAsync。未先配置商店就调用 Connect() 会导致商店提供方错误或缺失。

2. 注册商品​

调用 FetchProducts 并传入 ProductDefinition 对象列表,以注册您的应用所销售的商品。

_controller.FetchProducts(new List<ProductDefinition>
{
new ProductDefinition("antifreeze", ProductType.Consumable),
});

3. 连接商店​

配置完成后,从 Unity IAP v5 获取 StoreController 并进行连接。请在调用 Connect() 之前订阅所需的事件。

事件委托签名:

事件处理方法签名
OnStoreConnectedvoid OnConnected()
OnProductsFetchedvoid OnProductsFetched(List<Product> products)
OnPurchasePendingvoid OnPurchasePending(PendingOrder order)
OnPurchaseConfirmedvoid OnPurchaseConfirmed(Order order)
OnPurchaseFailedvoid OnPurchaseFailed(FailedOrder order)
OnProductsFetchFailedvoid OnProductsFetchFailed(ProductFetchFailed failure)
using System.Linq;
using UnityEngine;
using UnityEngine.Purchasing;
using AppCoins.Unity;

private StoreController _controller;

private async void Start()
{
// Must happen before Connect
await AppCoinsIAP.ConfigureStoreAsync(AppCoinsStoreMode.Automatic);

_controller = UnityIAPServices.StoreController();

_controller.OnProductsFetched += OnProductsFetched;
_controller.OnPurchasePending += OnPurchasePending;

// Register the products your app sells
_controller.FetchProducts(new List<ProductDefinition>
{
new ProductDefinition("antifreeze", ProductType.Consumable),
});

// Re-fire OnPurchasePending for any unfinished purchases found by FetchPurchases()
_controller.ProcessPendingOrdersOnPurchasesFetched(true);

await _controller.Connect();

// Trigger recovery of unfinished purchases — results arrive via OnPurchasePending
_controller.FetchPurchases();
}

4. 获取商品​

OnProductsFetched 事件会在 Connect() 完成后触发。请用它来填充您的商店界面。

private void OnProductsFetched(List<Product> products)
{
foreach (var product in products)
{
Debug.Log($"Product: {product.definition.id} — {product.metadata.localizedPriceString}");
}
}
⚠️
只有在您的应用于 Aptoide Connect 上通过审核之后,才能获取应用内商品。

5. 购买​

要发起购买,请调用 PurchaseProduct 并传入商品 ID。插件会处理购买流程,并通过 OnPurchasePending 事件返回结果。

public void BuyProduct(string productId)
{
_controller.PurchaseProduct(productId);
}

在 OnPurchasePending 中处理结果:

private void OnPurchasePending(PendingOrder order)
{
var product = order.CartOrdered.Items().FirstOrDefault()?.Product;
if (product == null) return;

// Deliver the item to the user before confirming
GiveItemToUser(product.definition.id);

// Then confirm (consume) the purchase
_controller.ConfirmPurchase(order);
}

6. 确认购买​

ConfirmPurchase 等同于完成或消耗一笔购买。请在成功向用户发放商品之后调用。在该调用完成之前,订单不会最终确定。

⚠️
每次购买成功后都要调用 _controller.ConfirmPurchase(order)。未确认的购买会保持未完成状态,并在 24 小时后自动退款。
private void OnPurchasePending(PendingOrder order)
{
var product = order.CartOrdered.Items().FirstOrDefault()?.Product;
if (product == null) return;

// 1. Give the item to the user first
GiveItemToUser(product.definition.id);

// 2. Only then confirm the purchase
_controller.ConfirmPurchase(order);
}

private void GiveItemToUser(string productId)
{
// Your game logic to grant the item
Debug.Log($"Delivering product: {productId}");
}

7. 在启动时处理未完成的购买​

⚠️
请在应用每次启动时检查未完成的购买。这些是用户已经付款但尚未确认的购买——例如应用在购买流程中崩溃的情况。未确认的购买将在 24 小时后自动退款。

请在 Connect() 之前调用 _controller.ProcessPendingOrdersOnPurchasesFetched(true),并在 Connect() 之后调用 _controller.FetchPurchases()。所有未完成的购买都会通过 OnPurchasePending 重新到达——与新购买使用同一个处理方法,无需单独的恢复处理逻辑。

// In Start(), before Connect():
_controller.ProcessPendingOrdersOnPurchasesFetched(true);

await _controller.Connect();

// After Connect(), trigger recovery — recovered orders arrive via OnPurchasePending:
_controller.FetchPurchases();

完整的最小示例​

下面这个类在单个 MonoBehaviour 中演示了完整集成,沿用了 TrivialDrive 示例的模式。

using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Purchasing;
using AppCoins.Unity;

public class AppCoinsIAPManager : MonoBehaviour
{
private StoreController _controller;

private async void Start()
{
// Step 1: Configure the store (must come before Connect)
var selectedStore = await AppCoinsIAP.ConfigureStoreAsync(AppCoinsStoreMode.Automatic);
Debug.Log("Billing store selected: " + selectedStore);

// Step 2: Get the controller and subscribe to events
_controller = UnityIAPServices.StoreController();
_controller.OnProductsFetched += OnProductsFetched;
_controller.OnPurchasePending += OnPurchasePending;

// Step 3: Register the products your app sells
_controller.FetchProducts(new List<ProductDefinition>
{
new ProductDefinition("antifreeze", ProductType.Consumable),
});

// Step 4: Re-fire OnPurchasePending for any unfinished purchases found by FetchPurchases()
_controller.ProcessPendingOrdersOnPurchasesFetched(true);

// Step 5: Connect to the store
await _controller.Connect();

// Step 6: Trigger recovery of unfinished purchases — results arrive via OnPurchasePending
_controller.FetchPurchases();
}

private void OnProductsFetched(List<Product> products)
{
foreach (var product in products)
{
Debug.Log($"Available product: {product.definition.id} — {product.metadata.localizedPriceString}");
}
}

public void BuyProduct(string productId)
{
_controller.PurchaseProduct(productId);
}

private void OnPurchasePending(PendingOrder order)
{
var product = order.CartOrdered.Items().FirstOrDefault()?.Product;
if (product == null) return;

// Deliver the item before confirming
GiveItemToUser(product.definition.id);

// Confirm (consume) the purchase
_controller.ConfirmPurchase(order);

Debug.Log($"Purchase completed and confirmed: {product.definition.id}");
}

private void GiveItemToUser(string productId)
{
// Your game logic to grant the purchased item
Debug.Log($"Delivering item to user: {productId}");
}
}

测试​

若要在开发期间测试 SDK 集成,您需要模拟应用通过 Aptoide 分发的状态,这样才能在开发设备上启用 AppCoins 计费。

请在 Xcode 中执行以下步骤:

  1. 在目标的 build settings 中搜索「Marketplaces」。
  2. 在 Deployment 下,将 Marketplaces(或 Alternative Distribution - Marketplaces)键设置为 com.aptoide.ios.store。
  3. 在您的 scheme 中进入 Run 标签页,再进入 Options 标签页,在 Distribution 下拉框中选择 com.aptoide.ios.store。

更多信息请参阅 Apple 官方文档:https://developer.apple.com/documentation/appdistribution/distributing-your-app-on-an-alternative-marketplace#Test-your-app-during-development

在同一构建中测试两套计费系统​

若要在同一个构建中切换测试 AppCoins 计费与 Apple 计费,请在 Safari 中使用以下深度链接:

操作深度链接
强制使用 AppCoins 计费{bundleId}.iap://wallet.appcoins.io/default/mode?value=appcoins
强制使用 Apple 计费{bundleId}.iap://wallet.appcoins.io/default/mode?value=apple
恢复自动检测{bundleId}.iap://wallet.appcoins.io/default/mode?value=automatic
显示当前模式{bundleId}.iap://wallet.appcoins.io/default/info

请将 {bundleId} 替换为您应用的 bundle identifier。模式变更会在应用重启后仍然保留。info 深度链接会以 3 秒浮层显示当前生效的模式。

⚠️

为防止滥用,模式覆盖对从 Apple App Store 安装的构建无效。

沙盒​

为验证您的计费集成是否配置成功,我们提供了沙盒环境,您可以在其中模拟购买并确认商品配置是否正确。该环境的使用文档请见:沙盒

类定义​

AppCoinsIAP​

插件的主入口点,负责在 Unity IAP 连接之前完成商店配置。

静态属性:

  • AppCoinsIAP.SelectedStore —— ConfigureStoreAsync 完成后所选择的商店名称。
  • AppCoinsIAP.AppCoinsStoreName —— 常量值 "AppCoinsAppStore"。
  • AppCoinsIAP.AppleStoreName —— 常量值 "AppleAppStore"。

静态方法:

  • await AppCoinsIAP.ConfigureStoreAsync(AppCoinsStoreMode mode) —— 选择计费商店并将其注册到 Unity IAP,返回所选商店名称。必须在 StoreController.Connect() 之前调用。

AppCoinsStoreMode​

用于控制配置时选择哪个计费商店的枚举。

值说明
Automatic在 iOS 17.4 及以上、且安装来源不是 App Store 或 TestFlight(包括模拟器)时使用 AppCoins;在更早的 iOS、App Store 和 TestFlight 上回落到 Apple。参见运行时检测的原理。
AppCoins始终使用 AppCoins 计费。
Apple始终使用 Apple 计费。

AppCoinsIAPBootstrap​

一个内部类,在 iOS 启动时通过 [RuntimeInitializeOnLoadMethod] 自动将 AppCoins 商店注册到 Unity IAP。开发者无需进行任何操作——它会在任何场景加载之前自动运行。

AppCoinsOrderInfo(收据)​

购买收据数据通过标准的 Unity IAP PendingOrder.Info 属性访问:

  • order.Info.Receipt —— 包含完整验证数据的 JSON 字符串。收据结构和服务端验证流程请参见购买验证。
  • order.Info.TransactionID —— 订单 UID 字符串。