Ejemplo n.º 1
0
 private void handleInventoryUpdated(IAPInventory inventory)
 {
     if (inventory.uid == this.uid)
     {
         Debug.Log("handleInventoryUpdated: " + inventory.uid + " lock: " + inventory.isLocked());
         UpdateTemplate(inventory);
     }
 }
Ejemplo n.º 2
0
        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);
                    }
                }
            }
        }