IAPButton.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using UnityEngine.Events;
  2. using UnityEngine.UI;
  3. namespace UnityEngine.Purchasing
  4. {
  5. /// <summary>
  6. /// A GUI component for exposing the current price and allow purchasing of In-App Purchases. Exposes configurable
  7. /// elements through the Inspector.
  8. /// </summary>
  9. /// <seealso cref="CodelessIAPStoreListener"/>
  10. [RequireComponent(typeof(Button))]
  11. [AddComponentMenu("In-App Purchasing/IAP Button")]
  12. [HelpURL("https://docs.unity3d.com/Manual/UnityIAP.html")]
  13. public class IAPButton : MonoBehaviour
  14. {
  15. /// <summary>
  16. /// The type of this button, can be either a purchase or a restore button.
  17. /// </summary>
  18. public enum ButtonType
  19. {
  20. /// <summary>
  21. /// This button will display localized product title and price. Clicking will trigger a purchase.
  22. /// </summary>
  23. Purchase,
  24. /// <summary>
  25. /// This button will display a static string for restoring previously purchased non-consumable
  26. /// and subscriptions. Clicking will trigger this restoration process, on supported app stores.
  27. /// </summary>
  28. Restore
  29. }
  30. /// <summary>
  31. /// Type of event fired after a successful purchase of a product.
  32. /// </summary>
  33. [System.Serializable]
  34. public class OnPurchaseCompletedEvent : UnityEvent<Product>
  35. {
  36. };
  37. /// <summary>
  38. /// Type of event fired after a failed purchase of a product.
  39. /// </summary>
  40. [System.Serializable]
  41. public class OnPurchaseFailedEvent : UnityEvent<Product, PurchaseFailureReason>
  42. {
  43. };
  44. /// <summary>
  45. /// Which product identifier to represent. Note this is not a store-specific identifier.
  46. /// </summary>
  47. [HideInInspector]
  48. public string productId;
  49. /// <summary>
  50. /// The type of this button, can be either a purchase or a restore button.
  51. /// </summary>
  52. [Tooltip("The type of this button, can be either a purchase or a restore button.")]
  53. public ButtonType buttonType = ButtonType.Purchase;
  54. /// <summary>
  55. /// Consume the product immediately after a successful purchase.
  56. /// </summary>
  57. [Tooltip("Consume the product immediately after a successful purchase.")]
  58. public bool consumePurchase = true;
  59. /// <summary>
  60. /// Event fired after a successful purchase of this product.
  61. /// </summary>
  62. [Tooltip("Event fired after a successful purchase of this product.")]
  63. public OnPurchaseCompletedEvent onPurchaseComplete;
  64. /// <summary>
  65. /// Event fired after a failed purchase of this product.
  66. /// </summary>
  67. [Tooltip("Event fired after a failed purchase of this product.")]
  68. public OnPurchaseFailedEvent onPurchaseFailed;
  69. /// <summary>
  70. /// Displays the localized title from the app store.
  71. /// </summary>
  72. [Tooltip("[Optional] Displays the localized title from the app store.")]
  73. public Text titleText;
  74. /// <summary>
  75. /// Displays the localized description from the app store.
  76. /// </summary>
  77. [Tooltip("[Optional] Displays the localized description from the app store.")]
  78. public Text descriptionText;
  79. /// <summary>
  80. /// Displays the localized price from the app store.
  81. /// </summary>
  82. [Tooltip("[Optional] Displays the localized price from the app store.")]
  83. public Text priceText;
  84. void Start()
  85. {
  86. Button button = GetComponent<Button>();
  87. if (buttonType == ButtonType.Purchase)
  88. {
  89. if (button)
  90. {
  91. button.onClick.AddListener(PurchaseProduct);
  92. }
  93. if (string.IsNullOrEmpty(productId))
  94. {
  95. Debug.LogError("IAPButton productId is empty");
  96. }
  97. if (!CodelessIAPStoreListener.Instance.HasProductInCatalog(productId))
  98. {
  99. Debug.LogWarning("The product catalog has no product with the ID \"" + productId + "\"");
  100. }
  101. }
  102. else if (buttonType == ButtonType.Restore)
  103. {
  104. if (button)
  105. {
  106. button.onClick.AddListener(Restore);
  107. }
  108. }
  109. }
  110. void OnEnable()
  111. {
  112. if (buttonType == ButtonType.Purchase)
  113. {
  114. CodelessIAPStoreListener.Instance.AddButton(this);
  115. if (CodelessIAPStoreListener.initializationComplete)
  116. {
  117. UpdateText();
  118. }
  119. }
  120. }
  121. void OnDisable()
  122. {
  123. if (buttonType == ButtonType.Purchase)
  124. {
  125. CodelessIAPStoreListener.Instance.RemoveButton(this);
  126. }
  127. }
  128. void PurchaseProduct()
  129. {
  130. if (buttonType == ButtonType.Purchase)
  131. {
  132. CodelessIAPStoreListener.Instance.InitiatePurchase(productId);
  133. }
  134. }
  135. void Restore()
  136. {
  137. if (buttonType == ButtonType.Restore)
  138. {
  139. if (Application.platform == RuntimePlatform.WSAPlayerX86 ||
  140. Application.platform == RuntimePlatform.WSAPlayerX64 ||
  141. Application.platform == RuntimePlatform.WSAPlayerARM)
  142. {
  143. CodelessIAPStoreListener.Instance.GetStoreExtensions<IMicrosoftExtensions>()
  144. .RestoreTransactions();
  145. }
  146. else if (Application.platform == RuntimePlatform.IPhonePlayer ||
  147. Application.platform == RuntimePlatform.OSXPlayer ||
  148. Application.platform == RuntimePlatform.tvOS)
  149. {
  150. CodelessIAPStoreListener.Instance.GetStoreExtensions<IAppleExtensions>()
  151. .RestoreTransactions(OnTransactionsRestored);
  152. }
  153. else if (Application.platform == RuntimePlatform.Android &&
  154. StandardPurchasingModule.Instance().appStore == AppStore.GooglePlay)
  155. {
  156. CodelessIAPStoreListener.Instance.GetStoreExtensions<IGooglePlayStoreExtensions>()
  157. .RestoreTransactions(OnTransactionsRestored);
  158. }
  159. else
  160. {
  161. Debug.LogWarning(Application.platform.ToString() +
  162. " is not a supported platform for the Codeless IAP restore button");
  163. }
  164. }
  165. }
  166. void OnTransactionsRestored(bool success)
  167. {
  168. //TODO: Add an invocation hook here for developers.
  169. }
  170. /// <summary>
  171. /// Invoke to process a successful purchase of the product associated with this button.
  172. /// </summary>
  173. /// <param name="e">The successful <c>PurchaseEventArgs</c> for the purchase event. </param>
  174. /// <returns>The result of the successful purchase</returns>
  175. public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)
  176. {
  177. onPurchaseComplete.Invoke(e.purchasedProduct);
  178. return (consumePurchase) ? PurchaseProcessingResult.Complete : PurchaseProcessingResult.Pending;
  179. }
  180. /// <summary>
  181. /// Invoked on a failed purchase of the product associated with this button
  182. /// </summary>
  183. /// <param name="product">The <typeparamref name="Product"/> which failed to purchase</param>
  184. /// <param name="reason">Information to help developers recover from this failure</param>
  185. public void OnPurchaseFailed(Product product, PurchaseFailureReason reason)
  186. {
  187. onPurchaseFailed.Invoke(product, reason);
  188. }
  189. internal void UpdateText()
  190. {
  191. var product = CodelessIAPStoreListener.Instance.GetProduct(productId);
  192. if (product != null)
  193. {
  194. if (titleText != null)
  195. {
  196. titleText.text = product.metadata.localizedTitle;
  197. }
  198. if (descriptionText != null)
  199. {
  200. descriptionText.text = product.metadata.localizedDescription;
  201. }
  202. if (priceText != null)
  203. {
  204. priceText.text = product.metadata.localizedPriceString;
  205. }
  206. }
  207. }
  208. }
  209. }