Unity IAP IOS支付代码
时间: 2023-07-02 14:21:03 浏览: 202
以下是Unity中实现IOS支付的示例代码:
```csharp
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Purchasing;
using UnityEngine.UI;
public class IAPManager : MonoBehaviour, IStoreListener
{
private static IStoreController storeController;
private static IExtensionProvider extensionProvider;
public Text logText;
private void Start()
{
InitializePurchasing();
}
public void InitializePurchasing()
{
var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
builder.AddProduct("com.yourgame.product1", ProductType.Consumable, new IDs { { "com.yourgame.product1", AppleAppStore.Name } });
builder.AddProduct("com.yourgame.product2", ProductType.NonConsumable, new IDs { { "com.yourgame.product2", AppleAppStore.Name } });
builder.AddProduct("com.yourgame.product3", ProductType.Subscription, new IDs { { "com.yourgame.product3", AppleAppStore.Name } });
UnityPurchasing.Initialize(this, builder);
}
public void BuyProduct(string productId)
{
if (storeController != null)
{
Product product = storeController.products.WithID(productId);
if (product != null && product.availableToPurchase)
{
storeController.InitiatePurchase(product);
}
else
{
Log("BuyProductID: " + productId + " fail. Not purchasing product, either is not found or is not available for purchase");
}
}
else
{
Log("BuyProductID FAIL. Not initialized.");
}
}
public void RestorePurchases()
{
if (!IsInitialized())
{
return;
}
if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.OSXPlayer)
{
Log("RestorePurchases started ...");
var apple = extensionProvider.GetExtension<IAppleExtensions>();
apple.RestoreTransactions((result) =>
{
Log("RestorePurchases continuing: " + result + ". If no further messages, no purchases available to restore.");
});
}
else
{
Log("RestorePurchases FAIL. Not supported on this platform. Current = " + Application.platform);
}
}
public bool IsInitialized()
{
return storeController != null && extensionProvider != null;
}
public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
{
Log("OnInitialized: PASS");
storeController = controller;
extensionProvider = extensions;
}
public void OnInitializeFailed(InitializationFailureReason error)
{
Log("OnInitializeFailed InitializationFailureReason:" + error);
}
public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
{
Log(string.Format("OnPurchaseFailed: FAIL. Product: '{0}', PurchaseFailureReason: {1}", product.definition.id, failureReason));
}
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
{
Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));
// TODO: 按照购买的商品类型处理逻辑
return PurchaseProcessingResult.Complete;
}
private void Log(string message)
{
Debug.Log(message);
logText.text = message;
}
}
```
上述代码实现了Unity IAP插件的初始化、商品购买、恢复购买、支付回调等功能。在使用时,需要根据实际情况修改商品ID、商品类型、处理逻辑等信息。同时,需要在Unity IAP控制台中配置相应的商品信息,并在Xcode中进行支付设置和支付回调处理。
阅读全文