Beispiel #1
0
    void GenerateShopItemsUI()
    {
        //Loop throw save purchased items and make them as purchased in the Database array
        for (int i = 0; i < GameDataManager.GetAllPurchasedCharacter().Count; i++)
        {
            int purchasedCharacterIndex = GameDataManager.GetPurchasedCharacter(i);
            characterDB.PurchaseCharacter(purchasedCharacterIndex);
        }

        //Delete itemTemplate after calculating item's Height :
        itemHeight = ShopItemsContainer.GetChild(0).GetComponent <RectTransform> ().sizeDelta.y;
        Destroy(ShopItemsContainer.GetChild(0).gameObject);
        //DetachChildren () will make sure to delete it from the hierarchy, otherwise if you
        //write ShopItemsContainer.ChildCount you w'll get "1"
        ShopItemsContainer.DetachChildren();

        //Generate Items
        for (int i = 0; i < characterDB.CharactersCount; i++)
        {
            //Create a Character and its corresponding UI element (uiItem)
            Character       character = characterDB.GetCharacter(i);
            CharacterItemUI uiItem    = Instantiate(itemPrefab, ShopItemsContainer).GetComponent <CharacterItemUI> ();

            //Move item to its position
            uiItem.SetItemPosition(Vector2.down * i * (itemHeight + itemSpacing));

            //Set Item name in Hierarchy (Not required)
            uiItem.gameObject.name = "Item" + i + "-" + character.name;

            //Add information to the UI (one item)
            uiItem.SetCharacterName(character.name);
            uiItem.SetCharacterImage(character.image);
            uiItem.SetCharacterSpeed(character.speed);
            uiItem.SetCharacterPower(character.power);
            uiItem.SetCharacterPrice(character.price);

            if (character.isPurchased)
            {
                //Character is Purchased
                uiItem.SetCharacterAsPurchased();
                uiItem.OnItemSelect(i, OnItemSelected);
            }
            else
            {
                //Character is not Purchased yet
                uiItem.OnItemPurchase(i, OnItemPurchased);
            }

            //Resize Items Container
            ShopItemsContainer.GetComponent <RectTransform> ().sizeDelta =
                Vector2.up * ((itemHeight + itemSpacing) * characterDB.CharactersCount + itemSpacing);

            //you can use VerticalLayoutGroup with ContentSizeFitter to skip all of this :
            //(moving items & resizing the container)
        }
    }
Beispiel #2
0
    void GenerateShopItemUI()
    {
        //delete ItemTemplate after calculating items height
        itemHeight = ShopItemContainer.GetChild(0).GetComponent <RectTransform>().sizeDelta.y;
        Destroy(ShopItemContainer.GetChild(0).gameObject);
        ShopItemContainer.DetachChildren();

        //Generate item
        for (int i = 0; i < characterDB.CharactersCount; i++)
        {
            Character       character = characterDB.GetCharacter(i);
            CharacterItemUI uiItem    = Instantiate(ItemPrefab, ShopItemContainer).GetComponent <CharacterItemUI> ();

            //move item to its position
            uiItem.SetItemPosition(Vector2.down * i * (itemHeight + itemSpacing));

            //set item name in hierarchy
            uiItem.gameObject.name = "Item" + i + "-" + character.name;

            //Add information to the UI (one time)
            uiItem.SetCharacterName(character.name);
            uiItem.SetCharacterImage(character.image);
            uiItem.SetCharacterPrice(character.price);

            if (character.isPurchased)
            {
                //character is purchased
                uiItem.SetCharacterAsPurchased();
                uiItem.OnItemSelect(i, OnItemSelected);
            }
            else
            {
                uiItem.SetCharacterPrice(character.price);
                uiItem.OnItemPurchase(i, OnItemPurchase);
            }

            //Resize item container
            ShopItemContainer.GetComponent <RectTransform>().sizeDelta =
                Vector2.up * ((itemHeight + itemSpacing) * characterDB.CharactersCount + itemSpacing);
        }
    }