説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CodelessIAPButton.cs 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System;
  2. using UnityEngine.Events;
  3. using UnityEngine.Purchasing.Extension;
  4. using UnityEngine.UI;
  5. namespace UnityEngine.Purchasing
  6. {
  7. /// <summary>
  8. /// A GUI component for exposing the current price and allow purchasing of In-App Purchases. Exposes configurable
  9. /// elements through the Inspector.
  10. /// </summary>
  11. /// <seealso cref="CodelessIAPStoreListener"/>
  12. [AddComponentMenu("In-App Purchasing/IAP Button")]
  13. [HelpURL("https://docs.unity3d.com/Packages/com.unity.purchasing@latest")]
  14. public class CodelessIAPButton : BaseIAPButton
  15. {
  16. /// <summary>
  17. /// Type of event fired after a successful fetching the product information from the store.
  18. /// </summary>
  19. [Serializable]
  20. public class OnProductFetchedEvent : UnityEvent<Product>
  21. {
  22. }
  23. /// <summary>
  24. /// Type of event fired after a successful purchase of a product.
  25. /// </summary>
  26. [Serializable]
  27. public class OnPurchaseCompletedEvent : UnityEvent<Product>
  28. {
  29. }
  30. /// <summary>
  31. /// Type of event fired after a failed purchase of a product.
  32. /// </summary>
  33. [Serializable]
  34. public class OnPurchaseFailedEvent : UnityEvent<Product, PurchaseFailureDescription>
  35. {
  36. }
  37. /// <summary>
  38. /// Type of event fired after a restore transactions was completed.
  39. /// </summary>
  40. [Serializable]
  41. public class OnTransactionsRestoredEvent : UnityEvent<bool, string>
  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 CodelessButtonType buttonType = CodelessButtonType.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 restore transactions.
  61. /// </summary>
  62. [Tooltip("Event fired after a restore transactions.")]
  63. public OnTransactionsRestoredEvent onTransactionsRestored;
  64. /// <summary>
  65. /// Event fired after a successful purchase of this product.
  66. /// </summary>
  67. [Tooltip("Event fired after a successful purchase of this product.")]
  68. public OnPurchaseCompletedEvent onPurchaseComplete;
  69. /// <summary>
  70. /// Event fired after a failed purchase of this product.
  71. /// </summary>
  72. [Tooltip("Event fired after a failed purchase of this product.")]
  73. public OnPurchaseFailedEvent onPurchaseFailed;
  74. /// <summary>
  75. /// Event fired after a successful fetching the product information from the store.
  76. /// </summary>
  77. [Tooltip("Event fired after a successful fetching the product information from the store.")]
  78. public OnProductFetchedEvent onProductFetched;
  79. [Tooltip("Button that triggers purchase.")]
  80. public Button button;
  81. internal override string GetProductId()
  82. {
  83. return productId;
  84. }
  85. internal override bool IsAPurchaseButton()
  86. {
  87. return buttonType == CodelessButtonType.Purchase;
  88. }
  89. protected override bool IsARestoreButton()
  90. {
  91. return buttonType == CodelessButtonType.Restore;
  92. }
  93. protected override bool ShouldConsumePurchase()
  94. {
  95. return consumePurchase;
  96. }
  97. protected override void OnTransactionsRestored(bool success, string error)
  98. {
  99. onTransactionsRestored?.Invoke(success, error);
  100. }
  101. protected override void OnPurchaseComplete(Product purchasedProduct)
  102. {
  103. onPurchaseComplete?.Invoke(purchasedProduct);
  104. }
  105. /// <summary>
  106. /// Invoked on a failed purchase of the product associated with this button
  107. /// </summary>
  108. /// <param name="product">The <typeparamref name="Product"/> which failed to purchase</param>
  109. /// <param name="failureDescription">Information to help developers recover from this failure</param>
  110. public void OnPurchaseFailed(Product product, PurchaseFailureDescription failureDescription)
  111. {
  112. onPurchaseFailed?.Invoke(product, failureDescription);
  113. }
  114. protected override Button GetPurchaseButton()
  115. {
  116. return button;
  117. }
  118. protected override void AddButtonToCodelessListener()
  119. {
  120. CodelessIAPStoreListener.Instance.AddButton(this);
  121. }
  122. protected override void RemoveButtonToCodelessListener()
  123. {
  124. CodelessIAPStoreListener.Instance.RemoveButton(this);
  125. }
  126. internal override void OnInitCompleted()
  127. {
  128. var product = CodelessIAPStoreListener.Instance.GetProduct(productId);
  129. if (product != null)
  130. {
  131. onProductFetched.Invoke(product);
  132. }
  133. }
  134. /// <summary>
  135. /// Invoke to process a successful purchase of the product associated with this button.
  136. /// </summary>
  137. /// <param name="e">The successful <c>PurchaseEventArgs</c> for the purchase event. </param>
  138. /// <returns>The result of the successful purchase</returns>
  139. public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
  140. {
  141. return ProcessPurchaseInternal(args);
  142. }
  143. }
  144. }