Ejemplo n.º 1
0
        private void CreateSlots(PresetInventory preset, GameObject root)
        {
            for (var row = 1; row <= preset.NumberRows; row++)
            {
                for (var column = 1; column <= preset.NumberColumns; column++)
                {
                    var slot = Object.Instantiate(preset.SlotPrefab, root.transform);
                    slot.name = GetSlotName(column, row);

                    if (preset.TypeInventory == DataInventory.TypeInventoryEnum.FreeSlots)
                    {
                        PrepareFreeSlot(slot, preset, column, row);
                    }

                    // ISlotRootComponent
                    var slotComponent = (ISlotRootComponent)slot.AddComponent(preset.TypeSlotRootComponent);
                    if (slotComponent == null)
                    {
                        throw new Exception($"Cant Add ISlotRootComponent component to slot: {slot}!");
                    }

                    slotComponent.Data.Column = column;
                    slotComponent.Data.Row    = row;

                    // SlotInputComponent
                    if (preset.TypeSlotInputComponent != null)
                    {
                        slot.AddComponent(preset.TypeSlotInputComponent);
                    }

                    // SlotDebugComponent
                    slot.AddComponent <SlotDebugComponent>();
                }
            }
        }
Ejemplo n.º 2
0
        public GameObject CreateInventory(PresetInventory preset)
        {
            var canvasInventory = CreateCanvasInventory(preset.NameInventory);
            var rootInventory   = CreateRootInventory(preset, canvasInventory);

            // Data
            var dataInventory      = new DataInventory(preset.NameInventory, preset.TypeInventory);
            var inventoryComponent = rootInventory.GetComponent <InventoryComponent>();

            inventoryComponent.Init(dataInventory);

            // Slots
            CreateSlots(preset, rootInventory);

            return(canvasInventory);
        }
Ejemplo n.º 3
0
        private GameObject CreateRootInventory(PresetInventory preset, GameObject canvasInventory)
        {
            var root = new GameObject(preset.NameInventory);

            root.transform.SetParent(canvasInventory.transform);
            root.transform.localPosition = Vector3.zero;
            root.transform.localScale    = new Vector3(1, 1, 1);

            root.AddComponent <InventoryComponent>();

            root.AddComponent <CanvasRenderer>();
            var rectTransform = root.AddComponent <RectTransform>();

            rectTransform.sizeDelta = new Vector2Int(preset.CellSize.x * preset.NumberColumns, preset.CellSize.y * preset.NumberRows);

            if (preset.TypeInventory != DataInventory.TypeInventoryEnum.FreeSlots)
            {
                var grid = root.AddComponent <GridLayoutGroup>();
                grid.cellSize = preset.CellSize;
            }
            return(root);
        }
Ejemplo n.º 4
0
 private void PrepareFreeSlot(GameObject slot, PresetInventory preset, int column, int row)
 {
     slot.transform.localPosition = new Vector2(preset.CellSize.x * (column - 1), preset.CellSize.y * (row - 1));
     slot.transform.localScale    = new Vector3(1, 1, 1);
     slot.transform.GetComponent <RectTransform>().sizeDelta = preset.CellSize;
 }