Esempio n. 1
0
        private void InstantiateItemIconBackground()
        {
            var itemList = GameManager.Player.Inventory.GetItemListAsReadOnly(x =>
                                                                              x.ItemType == UI_Menu.Instance.Inventory.Left.ItemTypeDrawer.CurrentlyActiveItemType);
            var itemCount           = itemList.Count;
            var spacing             = 10.0f;
            var iconSize            = 128;
            var horizontalMaxSize   = RectTransform.sizeDelta.x;
            var horizontalShowLimit = 1;

            var horizontalSize = (iconSize * horizontalShowLimit) + ((horizontalShowLimit - 1) * spacing);

            while (horizontalSize < (horizontalMaxSize - (spacing * 2)))
            {
                horizontalShowLimit++;
                horizontalSize = (iconSize * horizontalShowLimit) + ((horizontalShowLimit - 1) * spacing);
            }

            horizontalShowLimit--;
            horizontalSize = (iconSize * horizontalShowLimit) + ((horizontalShowLimit - 1) * spacing);

            var horizontalRemainingSlots = horizontalShowLimit;
            var position = new Vector3((horizontalSize * -0.5f) + (iconSize * 0.5f),
                                       (RectTransform.sizeDelta.y * 0.5f) - (iconSize * 0.5f) - spacing);

            float offsetY = 0;

            for (int i = 0; i < itemCount; i++)
            {
                UI_Menu_Inventory_Left_ItemIcon itemIcon = null;
                if (itemIconsPool.Count == 0)
                {
                    // ===============================================================================================
                    // Create Parent
                    // ===============================================================================================
                    var go = new GameObject(itemList[i].ItemName);
                    var rt = go.AddComponent <RectTransform>();
                    rt.SetParent(RectTransform);
                    rt.localPosition = position;
                    rt.sizeDelta     = new Vector2(iconSize, iconSize);
                    itemIcon         = go.AddComponent <UI_Menu_Inventory_Left_ItemIcon>();
                }
                else
                {
                    // ===============================================================================================
                    // Load from pool
                    // ===============================================================================================
                    itemIcon = itemIconsPool.Pop();
                    itemIcon.gameObject.SetActive(true);
                    itemIcon.RectTransform.SetParent(RectTransform);
                    itemIcon.RectTransform.localPosition = position;
                }

                // ===============================================================================================
                // Init ItemIcon and register it to itemiconlist
                // ===============================================================================================
                itemIcon.Init(itemList[i]);
                itemIconsList.Add(itemIcon);
                var itemPreview = itemIcon.ItemRenderer.InstantiateItemPreview(new Vector2(i, offsetY));
                instantiatedPrefabDic.Add(itemPreview, itemIcon.ItemRenderer.RawImage.texture as RenderTexture);

                // ===============================================================================================
                // Register this to EquippedItemIcon
                // ===============================================================================================
                UI_Menu_Inventory_Left_EquippedItemIcon.MarkItemAsEquippedItem(itemIcon);

                // ===============================================================================================
                // Update position
                // ===============================================================================================
                horizontalRemainingSlots--;

                if (horizontalRemainingSlots == 0)
                {
                    horizontalRemainingSlots = horizontalShowLimit;
                    position.y -= spacing + iconSize;
                    position.x  = (horizontalSize * -0.5f) + (iconSize * 0.5f);

                    offsetY += 1.0f;
                }
                else
                {
                    position.x += spacing + iconSize;
                }
            }

            // ===============================================================================================
            // Render texture for the first time after instantiation finished
            // ===============================================================================================
            RenderCameraForTheFirstTime();
        }