DummyAssetBundleStoreUI.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. public class DummyAssetBundleStoreUI : MonoBehaviour
  4. {
  5. const string ANDROID_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0c81kDu1X9i9cIPgFPhYYp99cnzi19yFZmO/RLkB5WNnJTE/iXmR+J7pF0gyTR5H83MpZtK4zMvo/ZtnPgmzaASbg2njYFxpNpt8XEmDv2/6ZoOvB6dZKbnnNd/Admjs/gTnGrtLza1uvXBNvefkw6PCV8dktjmLI6GuADiXobO+lmWygADKlFxN+nxY2gmEjBW0Z+FHASTAl82U7PxW+sbjfmuMQiehMKCqI99LLd/Xu5pWh66danvuJNT4+kmOr2rzrKkXisVyuKreoabp2R/cgu5HlLQyss1Na5ydorMDVWW0Rdvggd/4kugoYhFYr3U2qwc67iQdcX28a0wpQQIDAQAB";
  6. List<Skin> _skins = new List<Skin>();
  7. string[] _productIdentifiers = new string[] { "skin001", "skin002"};
  8. //#if UNITY_IPHONE
  9. // List<StoreKitTransaction> _pendingTransactions = new List<StoreKitTransaction>();
  10. //#elif UNITY_ANDROID
  11. // List<GooglePurchase> _purchases = null;
  12. //#endif
  13. void Start()
  14. {
  15. DebugViewManager.OnDebugView += OnDebugView;
  16. #if UNITY_EDITOR || UNITY_WEBPLAYER
  17. {
  18. var products = GetKnownProducts();
  19. OnReceivedProductList(products);
  20. }
  21. #elif UNITY_IPHONE
  22. {
  23. // this is used when you want to validate receipts on your own server or when dealing with iTunes hosted content
  24. // you must manually call StoreKitBinding.finishPendingTransactions when the download/validation is complete
  25. //StoreKitManager.autoConfirmTransactions = false;
  26. //StoreKitManager.productPurchaseAwaitingConfirmationEvent += OnProductPurchaseAwaitingConfirmationEvent;
  27. // you cannot make any purchases until you have retrieved the products from the server with the requestProductData method
  28. // we will store the products locally so that we will know what is purchaseable and when we can purchase the products
  29. IAP.requestProductData(_productIdentifiers, _productIdentifiers, OnReceivedProductList);
  30. }
  31. #elif UNITY_ANDROID
  32. {
  33. IAP.init(ANDROID_PUBLIC_KEY);
  34. GoogleIABManager.billingSupportedEvent += () =>
  35. {
  36. //Only query after billing supported was successful (otherwise "fetching product data failed: IAB helper is not set up. Can't perform operation: queryInventory")
  37. IAP.requestProductData(_productIdentifiers, _productIdentifiers, OnReceivedProductList);
  38. };
  39. GoogleIABManager.queryInventorySucceededEvent += (purchases, skus) =>
  40. {
  41. //_purchases = purchases;
  42. //AVDebug.Log("purchases :"+purchases.Count);
  43. //AVDebug.Log("skus :"+skus.Count);
  44. //foreach (GooglePurchase purchase in purchases)
  45. //{
  46. // foreach (Skin skin in _skins)
  47. // {
  48. // if (purchase.productId == skin.product.productId)
  49. // {
  50. // skin.GoogleSaidItsAlreadyPurchased();
  51. // }
  52. // }
  53. //}
  54. };
  55. }
  56. #endif
  57. }
  58. void OnDestroy()
  59. {
  60. DebugViewManager.OnDebugView -= OnDebugView;
  61. }
  62. List<IAPProduct> GetKnownProducts()
  63. {
  64. List<IAPProduct> products = new List<IAPProduct>();
  65. products.Add(new IAPProduct("skin001","Skin 001","Free","First skin","$"));
  66. return products;
  67. }
  68. void OnReceivedProductList(List<IAPProduct> products)
  69. {
  70. if (products != null)
  71. {
  72. AVDebug.Log("Received products: "+products.Count);
  73. } else
  74. {
  75. AVDebug.Log("No products received!!!... filling with known skins");
  76. products = GetKnownProducts();
  77. }
  78. //Add them in order as ordered in _productIdentifiers (screw you Android)
  79. Skin defaultSkin = new Skin(GetDefaultSkinProduct());
  80. _skins.Add(defaultSkin);
  81. foreach (string sku in _productIdentifiers)
  82. {
  83. foreach (IAPProduct p in products)
  84. {
  85. if (sku == p.productId)
  86. {
  87. Skin skin = new Skin(p);
  88. _skins.Add(skin);
  89. //#if UNITY_IPHONE
  90. //{
  91. // //process any unfinished transactions
  92. // foreach (StoreKitTransaction transaction in _pendingTransactions)
  93. // {
  94. // skin.OnProductPurchaseAwaitingConfirmationEvent(transaction);
  95. // }
  96. //}
  97. //#endif
  98. }
  99. }
  100. }
  101. //#if UNITY_IPHONE
  102. //{
  103. // _pendingTransactions.Clear();
  104. //}
  105. //#endif
  106. }
  107. //#if UNITY_IPHONE
  108. // void OnProductPurchaseAwaitingConfirmationEvent(StoreKitTransaction transaction)
  109. // {
  110. // //If the skins haven't been populated yet (i.e. OnReceivedProductList has not been called yet)...
  111. // //This happens when there are incomplete transactions, e.g. app was killed while download in progress and then app was relaunched
  112. // if (_skins.Count == 0)
  113. // {
  114. // //queue them
  115. // _pendingTransactions.Add(transaction);
  116. // }
  117. // }
  118. //#endif
  119. void OnDebugView()
  120. {
  121. foreach (var s in _skins)
  122. {
  123. GUILayout.BeginHorizontal();
  124. GUILayout.Label(s.product.title + " " + s.product.price + " ("+s.State+") ");
  125. switch (s.State)
  126. {
  127. case Skin.SkinState.Downloading:
  128. int percentage = (int)(s.SkinDownloadProgress * 100);
  129. GUILayout.Label(percentage.ToString()+"% downloaded");
  130. break;
  131. case Skin.SkinState.AvailableForPurchase:
  132. if (GUILayout.Button("Buy"))
  133. {
  134. s.Buy();
  135. }
  136. break;
  137. case Skin.SkinState.AvailableForApplying:
  138. if (GUILayout.Button("Apply"))
  139. {
  140. s.Apply();
  141. }
  142. break;
  143. }
  144. GUILayout.EndHorizontal();
  145. }
  146. //#if UNITY_IPHONE
  147. //{
  148. // if (GUILayout.Button("Restore iOS purchases"))
  149. // {
  150. // StoreKitBinding.restoreCompletedTransactions();
  151. // }
  152. // if (GUILayout.Button("Kill any pending transactions"))
  153. // {
  154. // StoreKitBinding.finishPendingTransactions();
  155. // }
  156. //}
  157. //#elif UNITY_ANDROID
  158. //{
  159. // if (_purchases != null && _purchases.Count > 0)
  160. // {
  161. // if (GUILayout.Button("Reset Google Purchases"))
  162. // {
  163. // foreach (GooglePurchase purchase in _purchases)
  164. // {
  165. // GoogleIAB.consumeProduct(purchase.productId);
  166. // }
  167. // }
  168. // }
  169. //}
  170. //#endif
  171. }
  172. IAPProduct GetDefaultSkinProduct()
  173. {
  174. #if UNITY_EDITOR || UNITY_WEBPLAYER
  175. return new IAPProduct("default", "Default", "0", "Default Skin", "$");
  176. #elif UNITY_IPHONE
  177. //Dictionary<string, object> dict = new Dictionary<string, object>();
  178. //dict["localizedTitle"] = "default";
  179. //dict["price"] = "";
  180. //dict["localizedDescription"] = "";
  181. //dict["productIdentifier"] = "default";
  182. //StoreKitProduct prod = StoreKitProduct.productFromDictionary(dict);
  183. return null;//new IAPProduct(prod);
  184. #elif UNITY_ANDROID
  185. Dictionary<string, object> dict = new Dictionary<string, object>();
  186. dict["title"] = "default";
  187. dict["price"] = "";
  188. dict["description"] = "";
  189. dict["productId"] = "default";
  190. //GoogleSkuInfo prod = new GoogleSkuInfo(dict);
  191. return null;
  192. #else
  193. AVDebug.LogError("Platform not supported to create default skin product");
  194. return null;
  195. #endif
  196. }
  197. }