반응형
- 코드 작성 (구버전용)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Purchasing;
public class IAPManager : MonoBehaviour, IStoreListener
{
private static IStoreController mStoreController;
private static IExtensionProvider mStroeExtensionProvider;
public static string Product_1 = "shop.cristalpack100";
void Start()
{
if (mStoreController == null)
{
InitializePurchasing();
}
}
void InitializePurchasing() //초기화
{
if (IsInitialized()) return;
var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
builder.AddProduct(Product_1, ProductType.Consumable);
UnityPurchasing.Initialize(this, builder);
}
void IStoreListener.OnInitialized(IStoreController controller, IExtensionProvider extensions)
{
Debug.Log("OnInitialized : PASS");
mStoreController = controller;
mStroeExtensionProvider = extensions;
}
void IStoreListener.OnInitializeFailed(InitializationFailureReason error)
{
Debug.Log("IAPManager Initialize : Fail");
}
private bool IsInitialized()
{
return mStoreController != null && mStroeExtensionProvider != null;
}
private void BuyProductID(string ID) //구매하기
{
if (IsInitialized())
{
Product product = mStoreController.products.WithID(ID);
if (product != null && product.availableToPurchase)
{
mStoreController.InitiatePurchase(product);
Debug.Log(string.Format("Purchasing product asychronously: '{0}'", product.definition.id));
}
else
{
Debug.Log("BuyProductID: Fail");
}
}
else
{
Debug.Log("BuyProductID: Fail. Not initialized");
}
}
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args) //구매 확인중
{
if (isSuccess)
{
if (args.purchasedProduct.definition.id.Equals(Product_1)) Debug.Log("상품1 구매 성공");
}
else
{
Debug.Log("상품1 구매 실패");
}
if (string.Equals(args.purchasedProduct.definition.id, Product_1, System.StringComparison.Ordinal))
{
Debug.Log("상품1 아이템 지급");
}
return PurchaseProcessingResult.Complete;
}
void IStoreListener.OnPurchaseFailed(Product i, PurchaseFailureReason reason) //구매실패
{
Debug.Log(string.Format("OnPurchaseFailed: FAIL. Product: '{0}', PurchaseFailureReason: '{1}'", i.definition.id, reason));
}
}
반응형