暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BaseIAPButton.cs 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using UnityEngine.Events;
  3. using UnityEngine.UI;
  4. namespace UnityEngine.Purchasing
  5. {
  6. /// <summary>
  7. /// A GUI component for exposing the current price and allow purchasing of In-App Purchases. Exposes configurable
  8. /// elements through the Inspector.
  9. /// </summary>
  10. public abstract class BaseIAPButton : MonoBehaviour
  11. {
  12. protected abstract bool ShouldConsumePurchase();
  13. protected abstract void OnTransactionsRestored(bool success, string error);
  14. protected abstract void OnPurchaseComplete(Product purchasedProduct);
  15. internal abstract void OnInitCompleted();
  16. protected abstract void AddButtonToCodelessListener();
  17. protected abstract void RemoveButtonToCodelessListener();
  18. protected abstract Button GetPurchaseButton();
  19. void Start()
  20. {
  21. var button = GetPurchaseButton();
  22. var productId = GetProductId();
  23. if (IsAPurchaseButton())
  24. {
  25. if (button)
  26. {
  27. button.onClick.AddListener(PurchaseProduct);
  28. }
  29. if (string.IsNullOrEmpty(productId))
  30. {
  31. Debug.LogError("IAPButton productId is empty");
  32. }
  33. else if (!CodelessIAPStoreListener.Instance.HasProductInCatalog(productId!))
  34. {
  35. Debug.LogWarning("The product catalog has no product with the ID \"" + productId + "\"");
  36. }
  37. }
  38. else if (IsARestoreButton())
  39. {
  40. if (button)
  41. {
  42. button.onClick.AddListener(Restore);
  43. }
  44. }
  45. }
  46. internal abstract string GetProductId();
  47. internal abstract bool IsAPurchaseButton();
  48. protected abstract bool IsARestoreButton();
  49. void OnEnable()
  50. {
  51. if (IsAPurchaseButton())
  52. {
  53. AddButtonToCodelessListener();
  54. if (CodelessIAPStoreListener.initializationComplete)
  55. {
  56. OnInitCompleted();
  57. }
  58. }
  59. }
  60. void OnDisable()
  61. {
  62. if (IsAPurchaseButton())
  63. {
  64. RemoveButtonToCodelessListener();
  65. }
  66. }
  67. void PurchaseProduct()
  68. {
  69. if (IsAPurchaseButton())
  70. {
  71. CodelessIAPStoreListener.Instance.InitiatePurchase(GetProductId());
  72. }
  73. }
  74. protected PurchaseProcessingResult ProcessPurchaseInternal(PurchaseEventArgs args)
  75. {
  76. OnPurchaseComplete(args.purchasedProduct);
  77. return ShouldConsumePurchase() ? PurchaseProcessingResult.Complete : PurchaseProcessingResult.Pending;
  78. }
  79. void Restore()
  80. {
  81. if (IsARestoreButton())
  82. {
  83. if (Application.platform == RuntimePlatform.WSAPlayerX86 ||
  84. Application.platform == RuntimePlatform.WSAPlayerX64 ||
  85. Application.platform == RuntimePlatform.WSAPlayerARM)
  86. {
  87. CodelessIAPStoreListener.Instance.GetStoreExtensions<IMicrosoftExtensions>()
  88. .RestoreTransactions();
  89. }
  90. else if (Application.platform == RuntimePlatform.IPhonePlayer ||
  91. Application.platform == RuntimePlatform.OSXPlayer ||
  92. Application.platform == RuntimePlatform.tvOS
  93. #if UNITY_VISIONOS
  94. || Application.platform == RuntimePlatform.VisionOS
  95. #endif
  96. )
  97. {
  98. CodelessIAPStoreListener.Instance.GetStoreExtensions<IAppleExtensions>()
  99. .RestoreTransactions(OnTransactionsRestored);
  100. }
  101. else if (Application.platform == RuntimePlatform.Android &&
  102. StandardPurchasingModule.Instance().appStore == AppStore.GooglePlay)
  103. {
  104. CodelessIAPStoreListener.Instance.GetStoreExtensions<IGooglePlayStoreExtensions>()
  105. .RestoreTransactions(OnTransactionsRestored);
  106. }
  107. else
  108. {
  109. Debug.LogWarning(Application.platform +
  110. " is not a supported platform for the Codeless IAP restore button");
  111. }
  112. }
  113. }
  114. }
  115. }