Unity IAP 초기화
Unity IAP 패키지를 초기화하고, 필수 설정을 구성하고, 지원되는 플랫폼 인앱 구매 처리를 시작하기 위한 설정을 검증하는 방법을 알아봅니다.
읽는 시간 2분최근 업데이트: 한 달 전
Unity IAP(인앱 구매)를 사용하기 전에 IAP 패키지를 초기화합니다.
프로젝트에서 Unity 애널리틱스 또는 Unity Authentication을 사용하려면 IAP를 초기화하기 전에 먼저 UGS(Unity Gaming Services)를 초기화해야 합니다. 사용 방법은 아래의 Unity Gaming Services 초기화 섹션을 참조하십시오.
Unity Gaming Services 초기화
모든 Unity Gaming Services를 초기화하려면 를 호출합니다. 이 메서드는 초기화 진행 상황을 트래킹하는 데 사용할 수 있는 를 반환합니다.
UnityServices.InitializeAsync()Task자세한 내용은 초기화 예시를 참고하십시오. 완전히 작동하는 샘플을 보려면 06 Initialize Gaming Services 샘플을 임포트합니다(패키지 관리자 > 인앱 구매 > 샘플).
자세한 내용은 Services Core API를 참고하십시오.
인앱 구매 초기화
필수 조건
구매할 제품의 에디터 카탈로그 만들기의 단계를 완료합니다.
초기화 단계
초기화는 다음 단계로 구성됩니다.
- 앱 스토어에 대한 얻기.
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 실패) { } /// <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 실패) { } /// <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 실패) { } /// <summary> /// Invoked when a purchase needs to be processed and fulfilled. /// </summary> /// <param name="order">The order awaiting fulfillment.</param> private void OnPurchasePending(PendingOrder 순서) { }}
StoreController 가져오기
StoreControllerStoreControllerStoreController에 이벤트 핸들러 연결
스토어가 올바르게 작동하려면 다음 이벤트에 핸들러를 연결합니다.
- OnStoreDisconnected
- OnProductsFetched
- OnProductsFetchFailed
- OnPurchasesFetched
- OnPurchasesFetchFailed
- OnPurchasePending
프로젝트에 인앱 구매를 연동할 때 추가 핸들러를 구현해야 할 수 있습니다. 를 통해 제공되는 모든 이벤트 목록은 이벤트를 참조하십시오.
StoreController앱 스토어 연결
앱 스토어에 연결하려면 StoreController.Connect를 호출합니다. 연결이 완료되거나 실패하면 반환된 가 해결됩니다. 연결에 실패하면 이벤트가 호출됩니다. IAP 기능을 사용하려면 스토어에 연결해야 합니다.
TaskOnStoreDisconnected상품 페치
제품을 구매할 수 있는지 확인하려면 StoreController.FetchProducts에 문의하십시오. 성공하면 성공적으로 반환된 제품 목록과 함께 이벤트가 호출됩니다. 실패 시 가 호출됩니다.
OnProductsFetchedOnProductsFetchFailed구매는 성공적으로 반환된 제품에 대해서만 가져오거나 시작할 수 있습니다. 런타임 중에 에 여러 번 호출할 수 있지만, 이전 요청이 완료될 때까지 기다린 후 다시 에 호출해야 합니다.
FetchProductsFetchProductsApple 앱 스토어를 사용하는 경우 가 완료된 후 처리되지 않은 주문에 대해 를 호출할 수 있습니다.
FetchProductsOnPurchasePendingGetProducts(제품)
GetProductsFetchProductsFetchProductsFetchProductsGetProducts구매 페치
StoreController.FetchPurchasesPendingOrdersConfirmedOrdersDeferredOrdersFetchPurchasesOnPurchasesFetchedOnPurchasesFetchFailedOrdersOnPurchasePending인앱 구매 기능을 사용하려면 반드시 필요하지는 않습니다. 하지만 예기치 않은 동작이 발생할 수 있으므로 새 구매를 시작하기 전에 기존 구매를 가져와서 처리하는 것이 좋습니다.
런타임 중에 제품을 추가로 가져오면 구매를 다시 가져와야 합니다.
오프라인 구매 페치
Apple의 StoreKit 2와 같은 일부 플랫폼 라이브러리는 제품 데이터를 캐싱하지 않고도 오프라인 액세스 권한을 기기에 캐싱합니다. 이 접근 방식은 권장되지 않지만 콜백 후에도 를 호출할 수 있습니다. 이 경우, 는 구매 정보를 반환하지만, 연결된 제품은 의 를 가집니다. 또한 및 모두 스토어별 식별자로 설정됩니다.
OnProductsFetchFailedFetchPurchasesFetchPurchasesProductType.UnknowntypeProductDefinition.idProductDefinition.storeSpecificIdGetPurchases(구매)
GetPurchasesFetchPurchasesFetchPurchasesGetProductsFetchProductsFetchPurchasesGetPurchases코드리스 IAP 자동 초기화
코드리스 IAP 설정 방법에 대한 지침은 코드리스 IAP 설정을 참조하십시오.
코드리스 IAP를 위한 자동 Unity 게임 서비스 초기화
코드리스 IAP를 사용하는 경우 IAP Catalog 창 하단의 Automatically initialize Unity Gaming Services 체크박스를 선택하여 Unity Gaming Services 자동 초기화를 활성화합니다.
이렇게 하면 애플리케이션이 시작될 때 Unity Gaming Services가 즉시 초기화됩니다.
이 기능을 사용하려면 **Automatically initialize UnityIAPServices (recommended)**를 활성화해야 합니다. IAP 카탈로그에 이 체크박스가 표시되지 않으면 카탈로그 창에 제품을 아직 추가하지 않았기 때문일 수 있습니다.
그러면 기본 초기화 옵션으로 Unity Gaming Services가 초기화됩니다. 일부 서비스에는 특정 초기화 옵션이 필요하며 기본 구성에서는 작동하지 않을 수 있습니다. 커스텀 옵션이 필요한 경우 위에서 설명한 코딩된 API 사용하여 Unity Gaming Services를 초기화합니다.