初始化 Unity IAP
了解如何初始化 Unity IAP 包、配置基本设置以及验证设置以开始处理受支持平台上的应用内购。
阅读时间6 分钟最后更新于 20 天前
使用 Unity 应用内购 (IAP) 之前,请初始化 IAP 包。
如果要在项目中使用 Unity Analytics 或 Unity Authentication,必须先初始化 Unity Gaming Services (UGS),然后再初始化 IAP。请参阅下面的 Initialize Unity Gaming Services 部分以了解操作方法。
初始化 Unity Gaming Services
调用UnityServices.InitializeAsync()Task初始化应用内购
先决条件
完成 Create a catalog in Editor(为要购买的商品创建目录)中的步骤。初始化步骤
初始化包括以下步骤:- 获取应用商店的。
StoreController - 将事件监听器附加到。
StoreController - 连接到应用商店。
- 从应用商店获取商品。
- 从应用商店获取购买。
using System.Collections.Generic;using UnityEngine.Purchasing;public class MyIAPManager{ private StoreController m_StoreController; public MyIAPManager() { // Define products var catalogProvider = new CatalogProvider(); catalogProvider.AddProduct("100_gold_coins", ProductType.Consumable, new StoreSpecificIds() { {"100_gold_coins_google", GooglePlay.Name}, {"100_gold_coins_mac", MacAppStore.Name} }); // Get StoreController m_StoreController = UnityIAPServices.StoreController(); // Add event listeners m_StoreController.OnStoreDisconnected += OnStoreDisconnected; m_StoreController.OnProductsFetch += OnProductsFetch; m_StoreController.OnProductsFetchFailed += OnProductsFetchFailed; m_StoreController.OnPurchasesFetch += OnPurchasesFetch; m_StoreController.OnPurchasesFetchFailed += OnPurchasesFetchFailed; // Connect to store m_StoreController.Connect().ContinueWith(_ => { // Fetch products from store catalogProvider.FetchProducts( list => m_StoreController.FetchProducts(list) }); } /// <summary> /// Invoked when connection is lost to the current store, or on a Connect() failure. /// </summary> /// <param name="failure">Information regarding the failure.</param> private void OnStoreDisconnected(StoreConnectionFailureDescription failure) { } /// <summary> /// Invoked with products that are successfully fetched. /// </summary> /// <param name="products">Products successfully returned from the app store.</param> private void OnProductsFetch(List<Product> products) { // Fetch purchases for successfully retrieved products m_StoreController.FetchPurchases(); } /// <summary> /// Invoked when an attempt to fetch products has failed or when a subset of products failed to be fetched. /// </summary> /// <param name="failure">Information regarding the failure.</param> private void OnProductsFetchFailed(ProductFetchFailed failure) { } /// <summary> /// Invoked when previous purchases are fetched. /// </summary> /// <param name="orders">All active pending, completed, and deferred orders for previously fetched products.</param> private void OnPurchasesFetch(Orders orders) { } /// <summary> /// Invoked when an attempt to fetch previous purchases has failed. /// </summary> /// <param name="failure">Information regarding the failure.</param> private void OnPurchasesFetchFailed(PurchasesFetchFailureDescription failure) { } /// <summary> /// Invoked when a purchase needs to be processed and fulfilled. /// </summary> /// <param name="order">The order awaiting fulfillment.</param> private void OnPurchasePending(PendingOrder order) { }}
获取 StoreController
StoreControllerStoreController将事件处理程序附加到 StoreController
要使应用商店正常运行,请将处理程序附加到这些事件:- OnStoreDisconnected
- OnProductsFetch
- OnProductsFetchFailed
- OnPurchasesFetched
- OnPurchasesFetchFailed
- OnPurchasePending
StoreController连接到应用商店
调用 StoreController.Connect 以连接到应用商店。返回的TaskOnStoreDisconnected获取商品
要验证商品是否可供购买,请调用 StoreController.FetchProducts。成功时,将调用
OnProductsFetchedOnProductsFetchFailedFetchProductsFetchProductsFetchProductsOnPurchasePendingGetProducts
GetProductsFetchProductsFetchProductsFetchProductsGetProducts获取购买
调用StoreController.FetchPurchasesPendingOrdersConfirmedOrdersDeferredOrdersFetchPurchasesOnPurchasesFetchedOrdersOnPurchasesFetchFailedOnPurchasePending获取离线购买
某些平台库(例如 Apple StoreKit 2)会在设备上缓存授权以便离线访问,而不会缓存商品数据。虽然不建议使用此方法,但仍可以在OnProductsFetchFailedFetchPurchasesFetchPurchasestypeProductType.UnknownProductDefinition.idProductDefinition.storeSpecificIdGetPurchases
GetPurchasesFetchPurchasesFetchPurchasesGetProductsFetchProductsFetchPurchasesGetPurchases