Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

IAPButton.cs 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. /// <seealso cref="CodelessIAPStoreListener"/>
  11. [Obsolete("IAPButton is deprecated, please use CodelessIAPButton instead.", false)]
  12. [RequireComponent(typeof(Button))]
  13. [AddComponentMenu("In-App Purchasing/IAP Button (legacy)", int.MaxValue)]
  14. [HelpURL("https://docs.unity3d.com/Manual/UnityIAP.html")]
  15. public class IAPButton : BaseIAPButton
  16. {
  17. /// <summary>
  18. /// The type of this button, can be either a purchase or a restore button.
  19. /// </summary>
  20. public enum ButtonType
  21. {
  22. /// <summary>
  23. /// This button will display localized product title and price. Clicking will trigger a purchase.
  24. /// </summary>
  25. Purchase,
  26. /// <summary>
  27. /// This button will display a static string for restoring previously purchased non-consumable
  28. /// and subscriptions. Clicking will trigger this restoration process, on supported app stores.
  29. /// </summary>
  30. Restore
  31. }
  32. /// <summary>
  33. /// Type of event fired after a successful purchase of a product.
  34. /// </summary>
  35. [Serializable]
  36. public class OnPurchaseCompletedEvent : UnityEvent<Product>
  37. {
  38. }
  39. /// <summary>
  40. /// Type of event fired after a failed purchase of a product.
  41. /// </summary>
  42. [Serializable]
  43. public class OnPurchaseFailedEvent : UnityEvent<Product, PurchaseFailureReason>
  44. {
  45. }
  46. /// <summary>
  47. /// Type of event fired after a restore transactions was completed.
  48. /// </summary>
  49. [Serializable]
  50. public class OnTransactionsRestoredEvent : UnityEvent<bool, string>
  51. {
  52. }
  53. /// <summary>
  54. /// Which product identifier to represent. Note this is not a store-specific identifier.
  55. /// </summary>
  56. [HideInInspector]
  57. public string productId = "";
  58. /// <summary>
  59. /// The type of this button, can be either a purchase or a restore button.
  60. /// </summary>
  61. [Tooltip("The type of this button, can be either a purchase or a restore button.")]
  62. public ButtonType buttonType = ButtonType.Purchase;
  63. /// <summary>
  64. /// Consume the product immediately after a successful purchase.
  65. /// </summary>
  66. [Tooltip("Consume the product immediately after a successful purchase.")]
  67. public bool consumePurchase = true;
  68. /// <summary>
  69. /// Event fired after a restore transactions.
  70. /// </summary>
  71. [Tooltip("Event fired after a restore transactions.")]
  72. public OnTransactionsRestoredEvent onTransactionsRestored = null;
  73. /// <summary>
  74. /// Event fired after a successful purchase of this product.
  75. /// </summary>
  76. [Tooltip("Event fired after a successful purchase of this product.")]
  77. public OnPurchaseCompletedEvent onPurchaseComplete = null;
  78. /// <summary>
  79. /// Event fired after a failed purchase of this product.
  80. /// </summary>
  81. [Tooltip("Event fired after a failed purchase of this product.")]
  82. public OnPurchaseFailedEvent onPurchaseFailed = null;
  83. /// <summary>
  84. /// Displays the localized title from the app store.
  85. /// </summary>
  86. [Tooltip("[Optional] Displays the localized title from the app store.")]
  87. public Text titleText;
  88. /// <summary>
  89. /// Displays the localized description from the app store.
  90. /// </summary>
  91. [Tooltip("[Optional] Displays the localized description from the app store.")]
  92. public Text descriptionText;
  93. /// <summary>
  94. /// Displays the localized price from the app store.
  95. /// </summary>
  96. [Tooltip("[Optional] Displays the localized price from the app store.")]
  97. public Text priceText;
  98. internal override string GetProductId()
  99. {
  100. return productId;
  101. }
  102. internal override bool IsAPurchaseButton()
  103. {
  104. return buttonType == ButtonType.Purchase;
  105. }
  106. protected override bool IsARestoreButton()
  107. {
  108. return buttonType == ButtonType.Restore;
  109. }
  110. protected override bool ShouldConsumePurchase()
  111. {
  112. return consumePurchase;
  113. }
  114. protected override void OnTransactionsRestored(bool success, string error)
  115. {
  116. onTransactionsRestored?.Invoke(success, error);
  117. }
  118. protected override void OnPurchaseComplete(Product purchasedProduct)
  119. {
  120. onPurchaseComplete?.Invoke(purchasedProduct);
  121. }
  122. /// <summary>
  123. /// Invoked on a failed purchase of the product associated with this button
  124. /// </summary>
  125. /// <param name="product">The <typeparamref name="Product"/> which failed to purchase</param>
  126. /// <param name="reason">Information to help developers recover from this failure</param>
  127. public void OnPurchaseFailed(Product product, PurchaseFailureReason reason)
  128. {
  129. onPurchaseFailed?.Invoke(product, reason);
  130. }
  131. protected override Button GetPurchaseButton()
  132. {
  133. return GetComponent<Button>();
  134. }
  135. protected override void AddButtonToCodelessListener()
  136. {
  137. CodelessIAPStoreListener.Instance.AddButton(this);
  138. }
  139. protected override void RemoveButtonToCodelessListener()
  140. {
  141. CodelessIAPStoreListener.Instance.RemoveButton(this);
  142. }
  143. internal override void OnInitCompleted()
  144. {
  145. UpdateAllTexts();
  146. }
  147. void UpdateAllTexts()
  148. {
  149. var product = CodelessIAPStoreListener.Instance.GetProduct(productId);
  150. if (product != null)
  151. {
  152. if (titleText != null)
  153. {
  154. titleText.text = product.metadata.localizedTitle;
  155. }
  156. if (descriptionText != null)
  157. {
  158. descriptionText.text = product.metadata.localizedDescription;
  159. }
  160. if (priceText != null)
  161. {
  162. priceText.text = product.metadata.localizedPriceString;
  163. }
  164. }
  165. }
  166. /// <summary>
  167. /// Invoke to process a successful purchase of the product associated with this button.
  168. /// </summary>
  169. /// <param name="e">The successful <c>PurchaseEventArgs</c> for the purchase event. </param>
  170. /// <returns>The result of the successful purchase</returns>
  171. public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)
  172. {
  173. return ProcessPurchaseInternal(e);
  174. }
  175. }
  176. }