您必须提供 IStoreListener
接口的实现,该接口 Unity IAPUnity 应用内购买的缩写
参见 词汇表 用于向您的应用通知与购买相关的事件。
调用 UnityPurchasing.Initialize
方法以启动初始化过程,并提供监听器实现和配置。
请注意,即使网络不可用,初始化也不会失败;Unity IAP 将继续在后台尝试初始化。只有在 Unity IAP 遇到不可恢复的问题(例如配置错误或在设备设置中禁用 IAP)时,初始化才会失败。
因此,Unity IAP 初始化可能需要一段任意的时间;如果用户处于飞行模式,则会无限期地进行初始化。您应该通过阻止用户在初始化未成功完成的情况下尝试进行购买来相应地设计您的商店。
using UnityEngine;
using UnityEngine.Purchasing;
public class MyIAPManager : IStoreListener {
private IStoreController controller;
private IExtensionProvider extensions;
public MyIAPManager () {
var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
builder.AddProduct("100_gold_coins", ProductType.Consumable, new IDs
{
{"100_gold_coins_google", GooglePlay.Name},
{"100_gold_coins_mac", MacAppStore.Name}
});
UnityPurchasing.Initialize (this, builder);
}
/// <summary>
/// Called when Unity IAP is ready to make purchases.
/// </summary>
public void OnInitialized (IStoreController controller, IExtensionProvider extensions)
{
this.controller = controller;
this.extensions = extensions;
}
/// <summary>
/// Called when Unity IAP encounters an unrecoverable initialization error.
///
/// Note that this will not be called if Internet is unavailable; Unity IAP
/// will attempt initialization until it becomes available.
/// </summary>
public void OnInitializeFailed (InitializationFailureReason error)
{
}
/// <summary>
/// Called when a purchase completes.
///
/// May be called at any time after OnInitialized().
/// </summary>
public PurchaseProcessingResult ProcessPurchase (PurchaseEventArgs e)
{
return PurchaseProcessingResult.Complete;
}
/// <summary>
/// Called when a purchase fails.
/// </summary>
public void OnPurchaseFailed (Product i, PurchaseFailureReason p)
{
}
}