|
|
@@ -1,57 +1,106 @@ |
|
|
|
using System; |
|
|
|
锘縰sing System; |
|
|
|
using UnityEngine; |
|
|
|
using UnityEngine.Purchasing; |
|
|
|
using UnityEngine.Purchasing.Extension; |
|
|
|
|
|
|
|
public class IAPManager : MonoBehaviour, IDetailedStoreListener |
|
|
|
{ |
|
|
|
public static IAPManager Instance { get; private set; } // 鍗曚緥 |
|
|
|
|
|
|
|
private IStoreController controller; |
|
|
|
private IExtensionProvider extensions; |
|
|
|
|
|
|
|
private string subscriptionId = "quewenYueKa"; |
|
|
|
|
|
|
|
|
|
|
|
void Awake() |
|
|
|
{ |
|
|
|
// 鍗曚緥鍒濆�鍖� |
|
|
|
if (Instance == null) |
|
|
|
{ |
|
|
|
Instance = this; |
|
|
|
DontDestroyOnLoad(gameObject); // 鍙�€夛細闃叉�鍒囨崲鍦烘櫙琚�攢姣� |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
Destroy(gameObject); // 閬垮厤閲嶅�鍒涘缓 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void Start() |
|
|
|
{ |
|
|
|
var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance()); |
|
|
|
builder.AddProduct(subscriptionId, ProductType.Subscription); |
|
|
|
|
|
|
|
UnityPurchasing.Initialize(this, builder); // 使用新版接口 |
|
|
|
UnityPurchasing.Initialize(this, builder); // 浣跨敤鏂扮増鎺ュ彛 |
|
|
|
} |
|
|
|
|
|
|
|
public void OnInitialized(IStoreController controller, IExtensionProvider extensions) |
|
|
|
{ |
|
|
|
this.controller = controller; |
|
|
|
this.extensions = extensions; |
|
|
|
Debug.Log("IAP 初始化成功"); |
|
|
|
Debug.Log("IAP 鍒濆�鍖栨垚鍔�"); |
|
|
|
} |
|
|
|
|
|
|
|
public void OnInitializeFailed(InitializationFailureReason error) |
|
|
|
{ |
|
|
|
Debug.LogError("IAP 初始化失败: " + error); |
|
|
|
Debug.LogError("IAP 鍒濆�鍖栧け璐�: " + error); |
|
|
|
} |
|
|
|
|
|
|
|
public void OnInitializeFailed(InitializationFailureReason error, string message) |
|
|
|
{ |
|
|
|
Debug.LogError($"详细失败信息: {error} - {message}"); |
|
|
|
Debug.LogError($"璇︾粏澶辫触淇℃伅: {error} - {message}"); |
|
|
|
} |
|
|
|
|
|
|
|
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args) |
|
|
|
{ |
|
|
|
if (args.purchasedProduct.definition.id == subscriptionId) |
|
|
|
{ |
|
|
|
Debug.Log("订阅成功!"); |
|
|
|
// 解锁功能 |
|
|
|
Debug.Log("璁㈤槄鎴愬姛锛�"); |
|
|
|
// 瑙i攣鍔熻兘 |
|
|
|
} |
|
|
|
return PurchaseProcessingResult.Complete; |
|
|
|
} |
|
|
|
|
|
|
|
public void OnPurchaseFailed(Product product, PurchaseFailureDescription failureDescription) |
|
|
|
{ |
|
|
|
Debug.LogError($"购买失败: {failureDescription.message}"); |
|
|
|
Debug.LogError($"璐�拱澶辫触: {failureDescription.message}"); |
|
|
|
} |
|
|
|
|
|
|
|
public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason) |
|
|
|
{ |
|
|
|
Debug.LogError($"购买失败: {failureReason}"); |
|
|
|
Debug.LogError($"璐�拱澶辫触: {failureReason}"); |
|
|
|
} |
|
|
|
|
|
|
|
public bool IsSubscribed() |
|
|
|
{ |
|
|
|
if (controller == null) return false; |
|
|
|
|
|
|
|
Product product = controller.products.WithID(subscriptionId); |
|
|
|
if (product != null && product.hasReceipt) |
|
|
|
{ |
|
|
|
// 瀵逛簬璁㈤槄鍟嗗搧锛宧asReceipt 閫氬父鎰忓懗鐫€鐢ㄦ埛宸茬粡璁㈤槄 |
|
|
|
Debug.Log("鐢ㄦ埛宸茶�闃咃紒"); |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
Debug.Log("鐢ㄦ埛鏈��闃呫€�"); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
public void BuySubscription() |
|
|
|
{ |
|
|
|
if (controller == null) return; |
|
|
|
|
|
|
|
Product product = controller.products.WithID(subscriptionId); |
|
|
|
if (product != null && product.availableToPurchase) |
|
|
|
{ |
|
|
|
controller.InitiatePurchase(product); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
Debug.LogWarning("璁㈤槄鍟嗗搧鏈�噯澶囧ソ"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |