Unity Iap の初期化
Unity Iapパッケージの初期化、基本設定の構成、設定の確認を行い、サポートされているプラットフォーム間でアプリケーション内課金の処理を開始する方法を説明します。
読み終わるまでの所要時間 4 分最終更新 8日前
Unity アプリ内課金(IAP)を使用する前に、IAP パッケージを初期化します。
プロジェクトで Unity Analytics または Unity Authentication を使用する場合は、IAP を初期化する前にまず Unity Gaming Services(UGS)を初期化する必要があります。次の「 Initialize Unity Gaming Services 」セクションにハウツーが記載されています。
Unity Gaming Services の初期化
UnityServices.InitializeAsync()Taskアプリ内課金の初期化
前提条件
購入する製品について、エディターでカタログを作成する の手順を実行します。初期化手順
初期化期化は、以下のステップで構成されます。- アプリストアのの取得。
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.OnProductsFetched += OnProductsFetched; m_StoreController.OnProductsFetchFailed += OnProductsFetchFailed; m_StoreController.OnPurchasesFetched += OnPurchasesFetched; 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 OnProductsFetched(リスト<Product>プロダクト) { // 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 OnPurchasesFetched(オーダーオーダー) { } /// <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 の取得
StoreControllerStoreControllerStoreController へのイベントハンドラーのアタッチ、設定、添付
ストアを正しく機能させるには、次のイベントにハンドラをアタッチ、設定、添付します。- OnStoreDisconnected
- OnProductsFetched
- OnProductsFetchFailed
- OnPurchasesFetched
- OnPurchasesFetchFailed
- OnPurchasePending
StoreControllerアプリストアへの接続
StoreController.Connect を呼び出してアプリストアに接続します。返されるTaskOnStoreDisconnectedフェッチプロダクト
製品が購入可能かどうかを検証するには、 StoreController.FetchProducts を呼び出します。成功すると、正常に返されたプロダクトのリストで
OnProductsFetchedOnProductsFetchFailedFetchProductsFetchProductsFetchProductsOnPurchasePendingGetProducts
GetProductsFetchProductsFetchProductsFetchProductsGetProducts購入の取得
StoreController.FetchPurchasesPendingOrdersConfirmedOrdersDeferredOrdersFetchPurchasesOnPurchasesFetchedOnPurchasesFetchFailedOrdersOnPurchasePending購入をオフラインで取得
Apple StoreKit 2 などの一部のプラットフォームライブラリは、製品データをキャッシュせずにオフラインアクセスのためのエンタイトルメントをデバイスにキャッシュします。このアプローチは推奨されませんが、OnProductsFetchFailedFetchPurchasesFetchPurchasestypeProductType.UnknownProductDefinition.idProductDefinition.storeSpecificIdGetPurchases
GetPurchasesFetchPurchasesFetchPurchasesGetProductsFetchProductsFetchPurchasesGetPurchases