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.
 
 
 

209 lines
7.2 KiB

  1. #if UNITY_PURCHASING
  2. using UnityEngine.Events;
  3. using UnityEngine.UI;
  4. using System.IO;
  5. using System.Collections.Generic;
  6. namespace UnityEngine.Purchasing
  7. {
  8. [RequireComponent(typeof(Button))]
  9. [AddComponentMenu("Unity IAP/IAP Button")]
  10. [HelpURL("https://docs.unity3d.com/Manual/UnityIAP.html")]
  11. public class IAPButton : MonoBehaviour
  12. {
  13. public enum ButtonType
  14. {
  15. Purchase,
  16. Restore
  17. }
  18. [System.Serializable]
  19. public class OnPurchaseCompletedEvent : UnityEvent<Product>
  20. {
  21. };
  22. [System.Serializable]
  23. public class OnPurchaseFailedEvent : UnityEvent<Product, PurchaseFailureReason>
  24. {
  25. };
  26. [HideInInspector]
  27. public string productId;
  28. [Tooltip("The type of this button, can be either a purchase or a restore button")]
  29. public ButtonType buttonType = ButtonType.Purchase;
  30. [Tooltip("Consume the product immediately after a successful purchase")]
  31. public bool consumePurchase = true;
  32. [Tooltip("Event fired after a successful purchase of this product")]
  33. public OnPurchaseCompletedEvent onPurchaseComplete;
  34. [Tooltip("Event fired after a failed purchase of this product")]
  35. public OnPurchaseFailedEvent onPurchaseFailed;
  36. [Tooltip("[Optional] Displays the localized title from the app store")]
  37. public Text titleText;
  38. [Tooltip("[Optional] Displays the localized description from the app store")]
  39. public Text descriptionText;
  40. [Tooltip("[Optional] Displays the localized price from the app store")]
  41. public Text priceText;
  42. void Start()
  43. {
  44. Button button = GetComponent<Button>();
  45. if (buttonType == ButtonType.Purchase)
  46. {
  47. if (button)
  48. {
  49. button.onClick.AddListener(PurchaseProduct);
  50. }
  51. if (string.IsNullOrEmpty(productId))
  52. {
  53. Debug.LogError("IAPButton productId is empty");
  54. }
  55. if (!CodelessIAPStoreListener.Instance.HasProductInCatalog(productId))
  56. {
  57. Debug.LogWarning("The product catalog has no product with the ID \"" + productId + "\"");
  58. }
  59. }
  60. else if (buttonType == ButtonType.Restore)
  61. {
  62. if (button)
  63. {
  64. button.onClick.AddListener(Restore);
  65. }
  66. }
  67. }
  68. void OnEnable()
  69. {
  70. if (buttonType == ButtonType.Purchase)
  71. {
  72. CodelessIAPStoreListener.Instance.AddButton(this);
  73. if (CodelessIAPStoreListener.initializationComplete) {
  74. UpdateText();
  75. }
  76. }
  77. }
  78. void OnDisable()
  79. {
  80. if (buttonType == ButtonType.Purchase)
  81. {
  82. CodelessIAPStoreListener.Instance.RemoveButton(this);
  83. }
  84. }
  85. void PurchaseProduct()
  86. {
  87. if (buttonType == ButtonType.Purchase)
  88. {
  89. Debug.Log("IAPButton.PurchaseProduct() with product ID: " + productId);
  90. CodelessIAPStoreListener.Instance.InitiatePurchase(productId);
  91. }
  92. }
  93. void Restore()
  94. {
  95. if (buttonType == ButtonType.Restore)
  96. {
  97. if (Application.platform == RuntimePlatform.WSAPlayerX86 ||
  98. Application.platform == RuntimePlatform.WSAPlayerX64 ||
  99. Application.platform == RuntimePlatform.WSAPlayerARM)
  100. {
  101. CodelessIAPStoreListener.Instance.ExtensionProvider.GetExtension<IMicrosoftExtensions>()
  102. .RestoreTransactions();
  103. }
  104. else if (Application.platform == RuntimePlatform.IPhonePlayer ||
  105. Application.platform == RuntimePlatform.OSXPlayer ||
  106. Application.platform == RuntimePlatform.tvOS)
  107. {
  108. CodelessIAPStoreListener.Instance.ExtensionProvider.GetExtension<IAppleExtensions>()
  109. .RestoreTransactions(OnTransactionsRestored);
  110. }
  111. else if (Application.platform == RuntimePlatform.Android &&
  112. StandardPurchasingModule.Instance().appStore == AppStore.SamsungApps)
  113. {
  114. CodelessIAPStoreListener.Instance.ExtensionProvider.GetExtension<ISamsungAppsExtensions>()
  115. .RestoreTransactions(OnTransactionsRestored);
  116. }
  117. else if (Application.platform == RuntimePlatform.Android &&
  118. StandardPurchasingModule.Instance().appStore == AppStore.CloudMoolah)
  119. {
  120. CodelessIAPStoreListener.Instance.ExtensionProvider.GetExtension<IMoolahExtension>()
  121. .RestoreTransactionID((restoreTransactionIDState) =>
  122. {
  123. OnTransactionsRestored(
  124. restoreTransactionIDState != RestoreTransactionIDState.RestoreFailed &&
  125. restoreTransactionIDState != RestoreTransactionIDState.NotKnown);
  126. });
  127. }
  128. else
  129. {
  130. Debug.LogWarning(Application.platform.ToString() +
  131. " is not a supported platform for the Codeless IAP restore button");
  132. }
  133. }
  134. }
  135. void OnTransactionsRestored(bool success)
  136. {
  137. Debug.Log("Transactions restored: " + success);
  138. }
  139. /**
  140. * Invoked to process a purchase of the product associated with this button
  141. */
  142. public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)
  143. {
  144. Debug.Log(string.Format("IAPButton.ProcessPurchase(PurchaseEventArgs {0} - {1})", e,
  145. e.purchasedProduct.definition.id));
  146. onPurchaseComplete.Invoke(e.purchasedProduct);
  147. return (consumePurchase) ? PurchaseProcessingResult.Complete : PurchaseProcessingResult.Pending;
  148. }
  149. /**
  150. * Invoked on a failed purchase of the product associated with this button
  151. */
  152. public void OnPurchaseFailed(Product product, PurchaseFailureReason reason)
  153. {
  154. Debug.Log(string.Format("IAPButton.OnPurchaseFailed(Product {0}, PurchaseFailureReason {1})", product,
  155. reason));
  156. onPurchaseFailed.Invoke(product, reason);
  157. }
  158. internal void UpdateText()
  159. {
  160. var product = CodelessIAPStoreListener.Instance.GetProduct(productId);
  161. if (product != null)
  162. {
  163. if (titleText != null)
  164. {
  165. titleText.text = product.metadata.localizedTitle;
  166. }
  167. if (descriptionText != null)
  168. {
  169. descriptionText.text = product.metadata.localizedDescription;
  170. }
  171. if (priceText != null)
  172. {
  173. priceText.text = product.metadata.localizedPriceString;
  174. }
  175. }
  176. }
  177. }
  178. }
  179. #endif