Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.48 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Purchasing;
  6. using UnityEngine.SceneManagement;
  7. using UnityEngine.UI;
  8.  
  9. namespace IAP
  10. {
  11. public class Purchaser : MonoBehaviour, IStoreListener
  12. {
  13. private static IStoreController m_StoreController; // The Unity Purchasing system.
  14. private static IExtensionProvider m_StoreExtensionProvider; // The store-specific Purchasing subsystems.
  15.  
  16. public static string productRemoveAds = "removeads";
  17.  
  18. void Start()
  19. {
  20. // If we haven't set up the Unity Purchasing reference
  21. if (m_StoreController == null)
  22. {
  23. // Begin to configure our connection to Purchasing
  24. InitializePurchasing();
  25. }
  26. }
  27.  
  28. public void InitializePurchasing()
  29. {
  30. if (IsInitialized())
  31. {
  32. return;
  33. }
  34. var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
  35. builder.AddProduct(productRemoveAds, ProductType.NonConsumable);
  36. UnityPurchasing.Initialize(this, builder);
  37. }
  38.  
  39. private bool IsInitialized()
  40. {
  41. // Only say we are initialized if both the Purchasing references are set.
  42. return m_StoreController != null && m_StoreExtensionProvider != null;
  43. }
  44.  
  45. public void BuyRemoveAds()
  46. {
  47. BuyProductID(productRemoveAds);
  48. }
  49.  
  50. void BuyProductID(string productId)
  51. {
  52. // If Purchasing has been initialized ...
  53. if (IsInitialized())
  54. {
  55. // ... look up the Product reference with the general product identifier and the Purchasing
  56. // system's products collection.
  57. Product product = m_StoreController.products.WithID(productId);
  58.  
  59. // If the look up found a product for this device's store and that product is ready to be sold ...
  60. if (product != null && product.availableToPurchase)
  61. {
  62. Debug.Log(string.Format("Purchasing product asychronously: '{0}'", product.definition.id));
  63. // ... buy the product. Expect a response either through ProcessPurchase or OnPurchaseFailed
  64. // asynchronously.
  65. m_StoreController.InitiatePurchase(product);
  66. }
  67. // Otherwise ...
  68. else
  69. {
  70. // ... report the product look-up failure situation
  71. Debug.Log("BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase");
  72. }
  73. }
  74. // Otherwise ...
  75. else
  76. {
  77. // ... report the fact Purchasing has not succeeded initializing yet. Consider waiting longer or
  78. // retrying initiailization.
  79. Debug.Log("BuyProductID FAIL. Not initialized.");
  80. }
  81. }
  82.  
  83. // Apple currently requires explicit purchase restoration for IAP, conditionally displaying a password prompt.
  84. public void RestorePurchases()
  85. {
  86. if (!IsInitialized())
  87. {
  88. Debug.Log("RestorePurchases FAIL. Not initialized.");
  89. return;
  90. }
  91.  
  92. if (Application.platform == RuntimePlatform.IPhonePlayer ||
  93. Application.platform == RuntimePlatform.OSXPlayer)
  94. {
  95. Debug.Log("RestorePurchases started ...");
  96.  
  97. // Fetch the Apple store-specific subsystem.
  98. var apple = m_StoreExtensionProvider.GetExtension<IAppleExtensions>();
  99. // Begin the asynchronous process of restoring purchases. Expect a confirmation response in
  100. // the Action<bool> below, and ProcessPurchase if there are previously purchased products to restore.
  101. apple.RestoreTransactions((result) =>
  102. {
  103. // The first phase of restoration. If no more responses are received on ProcessPurchase then
  104. // no purchases are available to be restored.
  105. Debug.Log("RestorePurchases continuing: " + result + ". If no further messages, no purchases available to restore.");
  106. });
  107. }
  108. else
  109. {
  110. // We are not running on an Apple device. No work is necessary to restore purchases.
  111. Debug.Log("RestorePurchases FAIL. Not supported on this platform. Current = " + Application.platform);
  112. }
  113. }
  114.  
  115. #region Store Listener
  116.  
  117. public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
  118. {
  119. // Purchasing has succeeded initializing. Collect our Purchasing references.
  120. Debug.Log("OnInitialized: PASS");
  121.  
  122. // Overall Purchasing system, configured with products for this application.
  123. m_StoreController = controller;
  124. // Store specific subsystem, for accessing device-specific store features.
  125. m_StoreExtensionProvider = extensions;
  126. }
  127.  
  128.  
  129. public void OnInitializeFailed(InitializationFailureReason error)
  130. {
  131. // Purchasing set-up has not succeeded. Check error for reason. Consider sharing this reason with the user.
  132. Debug.Log("OnInitializeFailed InitializationFailureReason:" + error);
  133. }
  134.  
  135.  
  136. public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
  137. {
  138. // remove ads product has been purchased by this user.
  139. if (String.Equals(args.purchasedProduct.definition.id, productRemoveAds, StringComparison.Ordinal))
  140. {
  141. Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));
  142. PlayerPrefs.SetInt("RemoveAds", 1);
  143. PlayerPrefs.Save();
  144. SceneManager.LoadScene(SceneManager.GetActiveScene().name);
  145. }
  146. // Or ... an unknown product has been purchased by this user. Fill in additional products here....
  147. else
  148. {
  149. Debug.Log(string.Format("ProcessPurchase: FAIL. Unrecognized product: '{0}'", args.purchasedProduct.definition.id));
  150. }
  151.  
  152. // Return a flag indicating whether this product has completely been received, or if the application needs
  153. // to be reminded of this purchase at next app launch. Use PurchaseProcessingResult.Pending when still
  154. // saving purchased products to the cloud, and when that save is delayed.
  155. return PurchaseProcessingResult.Complete;
  156. }
  157.  
  158.  
  159. public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
  160. {
  161. // A product purchase attempt did not succeed. Check failureReason for more detail. Consider sharing
  162. // this reason with the user to guide their troubleshooting actions.
  163. Debug.Log(string.Format("OnPurchaseFailed: FAIL. Product: '{0}', PurchaseFailureReason: {1}", product.definition.storeSpecificId, failureReason));
  164. }
  165.  
  166. #endregion
  167.  
  168. public static bool HasPurchasedRemoveAds()
  169. {
  170. if (m_StoreController == null) return false;
  171. return PlayerPrefs.HasKey("RemoveAds");
  172. }
  173.  
  174. }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement