// Use this for initialization void Start() { if (uid != null) { IAPObject obj = null; // if (targetType == IAPType.Inventory) { obj = IAPInventoryManager.GetInventory(uid); } else if (targetType == IAPType.Currency) { obj = IAPInventoryManager.GetCurrency(uid); } if (obj != null) { UpdateTemplate(obj); // Update text Text[] txts = gameObject.GetComponentsInChildren <Text>(); // InAppPurchase foreach (Text txt in txts) { if (txt.name == "amount_consume") { txt.text = amount.ToString(); } } IAPUIUtility.AddButtonCallback( gameObject, (GameObject go) => { if (useConfirmDialog) { // Construct confirm msg string msg = IAPInventoryManager.uiSettings.consumeConfirmString.Replace("%title%", obj.title); msg = msg.Replace("%description%", obj.description); msg = msg.Replace("%amount_consume%", amount.ToString()); // Show confirm diaglog IAPInventoryManager.ShowConfirmDialog(msg, delegate(IAPDialog diag){ if (obj.Consume(amount)) { UpdateTemplate(obj); } } ); } else { obj.Consume(amount); } } ); } } }
private static void handlePackageSave(IAPObject obj) { _datastore.SetValue("packageList", obj.uid, obj.data); _datastore.Save(); if (OnPackageUpdated != null) { OnPackageUpdated.Invoke(obj as IAPPackage); } }
private static void handleGameLevelSave(IAPObject obj) { _datastore.SetValue("gameList", obj.uid, obj.data); _datastore.Save(); if (OnGameLevelUpdated != null) { OnGameLevelUpdated.Invoke(obj as IAPGameLevel); } }
private static void handleAbilitySave(IAPObject obj) { _datastore.SetValue("abilityList", obj.uid, obj.data); _datastore.Save(); if (OnAbilityUpdated != null) { OnAbilityUpdated.Invoke(obj as IAPAbility); } }
private static void handleInventorySave(IAPObject obj) { // Debug.Log("handleInventorySave: " + obj.uid); _datastore.SetValue("inventoryList", obj.uid, obj.data); _datastore.Save(); if (OnInventoryUpdated != null) { OnInventoryUpdated.Invoke(obj as IAPInventory); } }
// Callback for saving private static void handleCurrencySave(IAPObject obj) { //Debug.Log("handleCurrencySave: " + obj); _datastore.SetValue("currencyList", obj.uid, obj.data); _datastore.Save(); if (OnCurrencyUpdated != null) { OnCurrencyUpdated.Invoke(obj as IAPCurrency); } }
protected string GetText(IAPObject obj, IAPTextType type) { // Debug.Log("name " + obj.uid + " type " + type ); if ((obj is IAPPackage) && (obj as IAPPackage).fetchFromStore) { IAPPackage p = obj as IAPPackage; if (p.product != null) { switch (type) { case IAPTextType.uid: return(p.product.id); case IAPTextType.currency: return(p.product.isoCurrencyCode); case IAPTextType.price: return(p.product.priceString); case IAPTextType.title: return(string.IsNullOrEmpty(p.title)?p.product.title:p.title); case IAPTextType.description: return(string.IsNullOrEmpty(p.description)?p.product.description:p.description); } } } else { switch (type) { case IAPTextType.uid: return(obj.uid); case IAPTextType.amount: return(obj.amount.ToString()); case IAPTextType.currency: return(obj.currency); case IAPTextType.price: return(obj.price.ToString()); case IAPTextType.title: return(obj.title); case IAPTextType.description: return(""); } } return("error"); }
// Button callback private void handleButtonCallback(IAPObject obj) { if (useConfirmDialog) { // Construct confirm msg IAPCurrency currency = IAPInventoryManager.GetCurrency(obj.currency); if (currency != null) { string msg = IAPInventoryManager.uiSettings.iapConfirmString.Replace("%title%", obj.title); msg = msg.Replace("%description%", obj.description); msg = msg.Replace("%price%", obj.price.ToString()); msg = msg.Replace("%currency_title%", currency.title); msg = msg.Replace("%currency_description%", currency.description); // Show confirm diaglog IAPInventoryManager.ShowConfirmDialog(msg, delegate(IAPDialog diag){ // Debug.Log("available: " + obj.available); // Check if enough currency if ((obj.available != 0) && currency != null && currency.Consume(obj.price)) { obj.Refill(1); if (obj is IAPInventory) { obj.Unlock(); } UpdateTemplate(obj); } } ); } } else { // Get the currency IAPCurrency currency = IAPInventoryManager.GetCurrency(obj.currency); // Check if enough currency if (currency != null && currency.Consume(obj.price)) { obj.Refill(1); if (obj is IAPInventory) { obj.Unlock(); } UpdateTemplate(obj); } } }
// Helpers private void UpdateTextWithObject(Text txt, IAPObject obj, string name) { switch (name.ToLower()) { case "uid": txt.text = obj.uid; break; case "title": txt.text = obj.title; break; case "description": txt.text = obj.description; break; case "price": txt.text = obj.price.ToString(); break; case "currency": txt.text = obj.currency; break; case "amount": txt.text = obj.amount.ToString(); break; case "locked_text": if (obj is IAPInventory) { txt.gameObject.SetActive((obj as IAPInventory).isLocked()); } break; case "unlock_text": if (obj is IAPInventory) { txt.gameObject.SetActive(!(obj as IAPInventory).isLocked()); } break; } }
public override void Init(object[] inputs) { SIS.IAPManager.purchaseSucceededEvent += PurchaseSucceeded; SIS.IAPManager.purchaseFailedEvent += PurchaseFailed; mManager = (AFramework.IAP.IAPManager)inputs[0]; IAPGroup defaultGroup = new IAPGroup(); defaultGroup.id = defaultGroup.name = "default"; var currentPackage = mManager.PackageConfig; for (int i = 0; i < currentPackage.CurrentData.Length; ++i) { var data = currentPackage.CurrentData[i]; var item = new IAPObject(); item.id = data.PackageIdentifier.getString(); defaultGroup.items.Add(item); } mSystem = this.GetComponentInChildren <SIS.IAPManager>(); var prefab = Resources.Load <SIS.IAPManager>("IAPManager"); prefab.IAPs.Clear(); prefab.IAPs.Add(defaultGroup); mSystem = Instantiate(prefab, this.transform); #if UNITY_ANDROID mSystem.storeKeys.googleKey = System.Convert.ToBase64String(UnityEngine.Purchasing.Security.GooglePlayTangle.Data()); #endif StartCoroutine(CRWaitForProductUpdate()); mSystem.Initialize(); #if UNITY_EDITOR SIS.IAPManager.isDebug = true; var validator = mSystem.GetComponent <ReceiptValidator>(); if (validator != null) { Destroy(validator); } else { Debug.LogError("IAP does not have any receipt validator"); } #endif }
protected void UpdateTemplate(IAPObject obj) { if (obj != null) { // Update images Image[] imgs = gameObject.GetComponentsInChildren <Image>(); foreach (Image img in imgs) { if (img.name == "icon" && obj.icon != null) { img.sprite = obj.icon; } else if (obj is IAPInventory && img.name == "lock_icon") { IAPInventory obji = (IAPInventory)obj; img.gameObject.SetActive(obji.isLocked()); } else if (obj is IAPInventory && img.name == "unlock_icon") { IAPInventory obji = (IAPInventory)obj; img.gameObject.SetActive(!obji.isLocked()); } else if (img.name == "currency_icon") { IAPCurrency currency = IAPInventoryManager.GetCurrency(obj.currency); if (currency != null && currency.icon != null) { img.sprite = currency.icon; } } else if (img.name == "level") { if (targetType == IAPType.Ability) { IAPAbilityLevel lv = (obj as IAPAbility).GetCurrentLevel(); if (lv != null && lv.icon != null) { img.sprite = lv.icon; } } } } // For inventory if (obj is IAPInventory) { IAPInventory inventory = obj as IAPInventory; bool locked = inventory.isLocked(); Text[] inventoryText = gameObject.GetComponentsInChildren <Text>(); Debug.Log("UPDATE Tempate: " + uid + " obj: " + inventoryText.Length); foreach (Text txt in inventoryText) { string name = txt.name.ToLower(); Debug.Log("UPDATE Text: " + name + " locked: " + locked); if (name.StartsWith("locked")) { txt.enabled = locked; } else if (name.StartsWith("unlocked")) { txt.enabled = !locked; } } Button[] inventoryBtns = gameObject.GetComponentsInChildren <Button>(); foreach (Button btn in inventoryBtns) { string name = btn.name.ToLower(); Debug.Log("UPDATE button: " + name + " locked: " + locked); if (name.StartsWith("locked")) { btn.enabled = locked; } else if (name.StartsWith("unlocked")) { btn.enabled = !locked; } } if (inventory.isLocked()) { IAPUIUtility.SetButtonActive(true, gameObject, "locked_button"); IAPUIUtility.SetButtonActive(false, gameObject, "unlocked_button"); } else { IAPUIUtility.SetButtonActive(false, gameObject, "locked_button"); IAPUIUtility.SetButtonActive(true, gameObject, "unlocked_button"); } } // Update text Text[] txts = gameObject.GetComponentsInChildren <Text>(); // InAppPurchase if (targetType == IAPType.InAppPurchase) { IAPPackage package = (obj as IAPPackage); foreach (Text txt in txts) { string name = txt.name.ToLower(); if (name.StartsWith("content_")) { string[] ss = name.Split('_'); if (ss.Length == 3) { IAPPackageContent cc = package.GetContent(ss[1]); if (cc != null) { if (ss[2] == "amount") { txt.text = cc.amount.ToString(); } else { UpdateTextWithObject(txt, cc.obj, ss[2]); } } } } else if (package.fetchFromStore) { UpdateTextWithPackage(txt, package); } else { UpdateTextWithObject(txt, obj, name); } } // Ability } else if (targetType == IAPType.Ability) { IAPAbility ability = (obj as IAPAbility); foreach (Text txt in txts) { string name = txt.name.ToLower(); if (name == "price") { if (ability.isLocked() && ability.lockedString != null && ability.lockedString != "") { txt.text = ability.lockedString; } else { IAPAbilityLevel lv = ability.GetCurrentLevel(); if (lv != null) { if (ability.level == ability.levels.Count - 1 && ability.maxString != null && ability.maxString != "") { txt.text = ability.maxString; } else { txt.text = lv.price.ToString(); } } } } else { // Debug.Log("text " + txt + " name: " + name); UpdateTextWithObject(txt, obj, name); } } } else { foreach (Text txt in txts) { UpdateTextWithObject(txt, obj, txt.name); } } } }