162 lines
5.1 KiB
C#
162 lines
5.1 KiB
C#
using System.Text;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.Purchasing;
|
|
using UnityEngine.Purchasing.Extension;
|
|
|
|
public class IAPManager : MonoBehaviour, IDetailedStoreListener
|
|
{
|
|
private IStoreController storeController; //구매 과정을 제어하는 함수 제공자
|
|
private IExtensionProvider storeExtensionProvider; //여러 플랫폼을 위한 확장 처리 제공자
|
|
|
|
private StringBuilder sb = new StringBuilder();
|
|
|
|
public UnityAction<string> OnProcessPurchase;
|
|
|
|
public UnityAction OnProcessPurchaseFailed;
|
|
|
|
private bool isInitialize = false;
|
|
|
|
private void Start()
|
|
{
|
|
if (GameManager.Network != null)
|
|
{
|
|
if (GameManager.Network.IsOnline)
|
|
{
|
|
InitUnityIAP(); //Start 문에서 초기화 필수
|
|
}
|
|
|
|
GameManager.Network.OnNetworkOnline += OnNetworkOnline;
|
|
}
|
|
}
|
|
|
|
/* Unity IAP를 초기화하는 함수 */
|
|
private void InitUnityIAP()
|
|
{
|
|
ConfigurationBuilder builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
|
|
|
|
/* 구글 플레이 상품들 추가 */
|
|
builder.AddProduct("com.fgb.adsremove", ProductType.Consumable);
|
|
builder.AddProduct("com.fgb.cash200", ProductType.Consumable);
|
|
builder.AddProduct("com.fgb.cash1500", ProductType.Consumable);
|
|
builder.AddProduct("com.fgb.cash3300", ProductType.Consumable);
|
|
builder.AddProduct("com.fgb.heart20", ProductType.Consumable);
|
|
builder.AddProduct("com.fgb.heart45", ProductType.Consumable);
|
|
builder.AddProduct("com.fgb.heart75", ProductType.Consumable);
|
|
|
|
UnityPurchasing.Initialize(this, builder);
|
|
|
|
isInitialize = true;
|
|
}
|
|
|
|
/* 구매하는 함수 */
|
|
public void Purchase(string productId)
|
|
{
|
|
Product product = storeController.products.WithID(productId); //상품 정의
|
|
|
|
if (product != null && product.availableToPurchase) //상품이 존재하면서 구매 가능하면
|
|
{
|
|
storeController.InitiatePurchase(product); //구매가 가능하면 진행
|
|
}
|
|
else //상품이 존재하지 않거나 구매 불가능하면
|
|
{
|
|
Debug.Log("상품이 없거나 현재 구매가 불가능합니다");
|
|
}
|
|
}
|
|
|
|
/* 초기화 성공 시 실행되는 함수 */
|
|
public void OnInitialized(IStoreController controller, IExtensionProvider extension)
|
|
{
|
|
Debug.Log("초기화에 성공했습니다");
|
|
|
|
storeController = controller;
|
|
storeExtensionProvider = extension;
|
|
}
|
|
|
|
/* 초기화 실패 시 실행되는 함수 */
|
|
public void OnInitializeFailed(InitializationFailureReason error)
|
|
{
|
|
sb.Clear();
|
|
sb.Append("IAPManager: OnInitializeFailed: ").Append(error.ToString());
|
|
|
|
Debug.LogError(sb.ToString());
|
|
}
|
|
|
|
public void OnInitializeFailed(InitializationFailureReason error, string message)
|
|
{
|
|
sb.Clear();
|
|
sb.Append("IAPManager: OnInitializeFailed: ").Append(error.ToString()).Append(": ").Append(message);
|
|
|
|
Debug.LogError(sb.ToString());
|
|
}
|
|
|
|
/* 구매에 실패했을 때 실행되는 함수 */
|
|
public void OnPurchaseFailed(Product product, PurchaseFailureDescription failureDescription)
|
|
{
|
|
OnProcessPurchase?.Invoke("FailPurchase");
|
|
}
|
|
|
|
public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
|
|
{
|
|
sb.Clear();
|
|
sb.Append("IAPManager: OnPurchaseFailed: ").Append(product.ToString()).Append(failureReason.ToString());
|
|
|
|
Debug.LogError(sb.ToString());
|
|
|
|
OnProcessPurchase?.Invoke("FailPurchase");
|
|
}
|
|
|
|
/* 구매를 처리하는 함수 */
|
|
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
|
|
{
|
|
Debug.Log("IAPManager: SuccessPurchase");
|
|
|
|
AddProductItem(args.purchasedProduct.definition.id);
|
|
|
|
OnProcessPurchase?.Invoke(args.purchasedProduct.definition.id);
|
|
|
|
return PurchaseProcessingResult.Complete;
|
|
}
|
|
|
|
private void OnNetworkOnline(bool isOnline)
|
|
{
|
|
if(isOnline == true && isInitialize == false)
|
|
{
|
|
InitUnityIAP();
|
|
}
|
|
}
|
|
|
|
public void AddProductItem(string id)
|
|
{
|
|
switch (id)
|
|
{
|
|
case "com.fgb.adsremove":
|
|
GameManager.DB.RemoveADS();
|
|
GameManager.ADS.HideBanner();
|
|
|
|
GameManager.UI.ShowNStackPopup(EPopupType.ADSRemovePopup);
|
|
break;
|
|
|
|
case "com.fgb.heart20":
|
|
GameManager.DB.AddHeart(20, this.name);
|
|
break;
|
|
case "com.fgb.heart45":
|
|
GameManager.DB.AddHeart(45, this.name);
|
|
break;
|
|
case "com.fgb.heart75":
|
|
GameManager.DB.AddHeart(75, this.name);
|
|
break;
|
|
|
|
case "com.fgb.cash200":
|
|
GameManager.DB.AddKey(200, this.name);
|
|
break;
|
|
case "com.fgb.cash1500":
|
|
GameManager.DB.AddKey(1500, this.name);
|
|
break;
|
|
case "com.fgb.cash3300":
|
|
GameManager.DB.AddKey(3300, this.name);
|
|
break;
|
|
}
|
|
}
|
|
}
|