123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- using UnityEngine;
- using System.Collections.Generic;
- public class DummyAssetBundleStoreUI : MonoBehaviour
- {
- const string ANDROID_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0c81kDu1X9i9cIPgFPhYYp99cnzi19yFZmO/RLkB5WNnJTE/iXmR+J7pF0gyTR5H83MpZtK4zMvo/ZtnPgmzaASbg2njYFxpNpt8XEmDv2/6ZoOvB6dZKbnnNd/Admjs/gTnGrtLza1uvXBNvefkw6PCV8dktjmLI6GuADiXobO+lmWygADKlFxN+nxY2gmEjBW0Z+FHASTAl82U7PxW+sbjfmuMQiehMKCqI99LLd/Xu5pWh66danvuJNT4+kmOr2rzrKkXisVyuKreoabp2R/cgu5HlLQyss1Na5ydorMDVWW0Rdvggd/4kugoYhFYr3U2qwc67iQdcX28a0wpQQIDAQAB";
- List<Skin> _skins = new List<Skin>();
- string[] _productIdentifiers = new string[] { "skin001", "skin002"};
- //#if UNITY_IPHONE
- // List<StoreKitTransaction> _pendingTransactions = new List<StoreKitTransaction>();
- //#elif UNITY_ANDROID
- // List<GooglePurchase> _purchases = null;
- //#endif
- void Start()
- {
- DebugViewManager.OnDebugView += OnDebugView;
- #if UNITY_EDITOR || UNITY_WEBPLAYER
- {
- var products = GetKnownProducts();
- OnReceivedProductList(products);
- }
- #elif UNITY_IPHONE
- {
- // this is used when you want to validate receipts on your own server or when dealing with iTunes hosted content
- // you must manually call StoreKitBinding.finishPendingTransactions when the download/validation is complete
- //StoreKitManager.autoConfirmTransactions = false;
- //StoreKitManager.productPurchaseAwaitingConfirmationEvent += OnProductPurchaseAwaitingConfirmationEvent;
- // you cannot make any purchases until you have retrieved the products from the server with the requestProductData method
- // we will store the products locally so that we will know what is purchaseable and when we can purchase the products
- IAP.requestProductData(_productIdentifiers, _productIdentifiers, OnReceivedProductList);
- }
- #elif UNITY_ANDROID
- {
- IAP.init(ANDROID_PUBLIC_KEY);
- GoogleIABManager.billingSupportedEvent += () =>
- {
- //Only query after billing supported was successful (otherwise "fetching product data failed: IAB helper is not set up. Can't perform operation: queryInventory")
- IAP.requestProductData(_productIdentifiers, _productIdentifiers, OnReceivedProductList);
- };
- GoogleIABManager.queryInventorySucceededEvent += (purchases, skus) =>
- {
- //_purchases = purchases;
- //AVDebug.Log("purchases :"+purchases.Count);
- //AVDebug.Log("skus :"+skus.Count);
- //foreach (GooglePurchase purchase in purchases)
- //{
- // foreach (Skin skin in _skins)
- // {
- // if (purchase.productId == skin.product.productId)
- // {
- // skin.GoogleSaidItsAlreadyPurchased();
- // }
- // }
- //}
- };
- }
- #endif
- }
- void OnDestroy()
- {
- DebugViewManager.OnDebugView -= OnDebugView;
- }
- List<IAPProduct> GetKnownProducts()
- {
- List<IAPProduct> products = new List<IAPProduct>();
- products.Add(new IAPProduct("skin001","Skin 001","Free","First skin","$"));
- return products;
- }
- void OnReceivedProductList(List<IAPProduct> products)
- {
- if (products != null)
- {
- AVDebug.Log("Received products: "+products.Count);
- } else
- {
- AVDebug.Log("No products received!!!... filling with known skins");
- products = GetKnownProducts();
- }
- //Add them in order as ordered in _productIdentifiers (screw you Android)
- Skin defaultSkin = new Skin(GetDefaultSkinProduct());
- _skins.Add(defaultSkin);
- foreach (string sku in _productIdentifiers)
- {
- foreach (IAPProduct p in products)
- {
- if (sku == p.productId)
- {
- Skin skin = new Skin(p);
- _skins.Add(skin);
-
- //#if UNITY_IPHONE
- //{
- // //process any unfinished transactions
- // foreach (StoreKitTransaction transaction in _pendingTransactions)
- // {
- // skin.OnProductPurchaseAwaitingConfirmationEvent(transaction);
- // }
- //}
- //#endif
- }
- }
- }
- //#if UNITY_IPHONE
- //{
- // _pendingTransactions.Clear();
- //}
- //#endif
- }
- //#if UNITY_IPHONE
- // void OnProductPurchaseAwaitingConfirmationEvent(StoreKitTransaction transaction)
- // {
- // //If the skins haven't been populated yet (i.e. OnReceivedProductList has not been called yet)...
- // //This happens when there are incomplete transactions, e.g. app was killed while download in progress and then app was relaunched
- // if (_skins.Count == 0)
- // {
- // //queue them
- // _pendingTransactions.Add(transaction);
- // }
- // }
- //#endif
- void OnDebugView()
- {
- foreach (var s in _skins)
- {
- GUILayout.BeginHorizontal();
- GUILayout.Label(s.product.title + " " + s.product.price + " ("+s.State+") ");
- switch (s.State)
- {
- case Skin.SkinState.Downloading:
- int percentage = (int)(s.SkinDownloadProgress * 100);
- GUILayout.Label(percentage.ToString()+"% downloaded");
- break;
- case Skin.SkinState.AvailableForPurchase:
- if (GUILayout.Button("Buy"))
- {
- s.Buy();
- }
- break;
- case Skin.SkinState.AvailableForApplying:
- if (GUILayout.Button("Apply"))
- {
- s.Apply();
- }
- break;
- }
- GUILayout.EndHorizontal();
- }
- //#if UNITY_IPHONE
- //{
- // if (GUILayout.Button("Restore iOS purchases"))
- // {
- // StoreKitBinding.restoreCompletedTransactions();
- // }
- // if (GUILayout.Button("Kill any pending transactions"))
- // {
- // StoreKitBinding.finishPendingTransactions();
- // }
- //}
- //#elif UNITY_ANDROID
- //{
- // if (_purchases != null && _purchases.Count > 0)
- // {
- // if (GUILayout.Button("Reset Google Purchases"))
- // {
- // foreach (GooglePurchase purchase in _purchases)
- // {
- // GoogleIAB.consumeProduct(purchase.productId);
- // }
- // }
- // }
- //}
- //#endif
- }
- IAPProduct GetDefaultSkinProduct()
- {
- #if UNITY_EDITOR || UNITY_WEBPLAYER
- return new IAPProduct("default", "Default", "0", "Default Skin", "$");
- #elif UNITY_IPHONE
- //Dictionary<string, object> dict = new Dictionary<string, object>();
- //dict["localizedTitle"] = "default";
- //dict["price"] = "";
- //dict["localizedDescription"] = "";
- //dict["productIdentifier"] = "default";
- //StoreKitProduct prod = StoreKitProduct.productFromDictionary(dict);
- return null;//new IAPProduct(prod);
- #elif UNITY_ANDROID
- Dictionary<string, object> dict = new Dictionary<string, object>();
- dict["title"] = "default";
- dict["price"] = "";
- dict["description"] = "";
- dict["productId"] = "default";
- //GoogleSkuInfo prod = new GoogleSkuInfo(dict);
- return null;
- #else
- AVDebug.LogError("Platform not supported to create default skin product");
- return null;
- #endif
- }
- }
|