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 _skins = new List(); string[] _productIdentifiers = new string[] { "skin001", "skin002"}; //#if UNITY_IPHONE // List _pendingTransactions = new List(); //#elif UNITY_ANDROID // List _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 GetKnownProducts() { List products = new List(); products.Add(new IAPProduct("skin001","Skin 001","Free","First skin","$")); return products; } void OnReceivedProductList(List 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 dict = new Dictionary(); //dict["localizedTitle"] = "default"; //dict["price"] = ""; //dict["localizedDescription"] = ""; //dict["productIdentifier"] = "default"; //StoreKitProduct prod = StoreKitProduct.productFromDictionary(dict); return null;//new IAPProduct(prod); #elif UNITY_ANDROID Dictionary dict = new Dictionary(); 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 } }