// Use this for initialization void Start() { player = GameManager.instance.playerCharacter; player.onInventoryChanged += UpdateInventorySlots; player.onInventoryChanged += UpdateRecipeSlots; backgroundObject = transform.Find("Background").gameObject; slotsObject = backgroundObject.transform.Find("Slots").gameObject; slots = new List <InventoryUISlot>(); for (int i = 0; i < slotsObject.transform.childCount; i++) { GameObject child = slotsObject.transform.GetChild(i).gameObject; InventoryUISlot slot = child.GetComponent <InventoryUISlot>(); slot.slotIndex = slotsObject.transform.childCount - i - 1; slots.Insert(0, slot); } recipeSlotsObject = backgroundObject.transform.Find("Crafting").Find("RecipeSlots").gameObject; recipeSlots = new List <InventoryRecipeSlot>(); for (int i = 0; i < recipeSlotsObject.transform.childCount; i++) { GameObject child = recipeSlotsObject.transform.GetChild(i).gameObject; InventoryRecipeSlot recipeSlot = child.GetComponent <InventoryRecipeSlot>(); recipeSlots.Add(recipeSlot); } UpdateRecipeSlots(); }
public void UpdateUI() { for (int i = 0; i < inventory.inventorySize; i++) { InventoryUISlot slot = transform.GetChild(0).GetChild(i).GetComponent <InventoryUISlot>(); if (i < inventory.items.Count) { Item item = inventory.items[i]; if (item != null) { slot.Show(item); } else { slot.Remove(); } } else { slot.Remove(); } } foreach (Item item in inventory.items) { InventoryUISlot slot = transform.GetChild(0).GetChild(inventory.items.IndexOf(item)).GetComponent <InventoryUISlot>(); if (item != null) { slot.Show(item); } else { slot.Remove(); } } }
void CreateUIItem() { GameObject newInvItem = Instantiate(inventoryItemPrefab); // Fix weird scale bug // Assign GameItem (Latest added) GameItem gi = itemsInInventory[itemsInInventory.Count - 1]; newInvItem.GetComponent <InventoryItem>().myGameItem = gi; Transform panelTab = InventoryTab(gi); // Put to correct position InventoryUISlot invSlot = panelTab.GetChild(gi.invSlotNumber).GetComponent <InventoryUISlot>(); if (invSlot.TryPlaceItem(newInvItem.transform)) { invSlot.PlaceItemInSlot(newInvItem.transform); newInvItem.transform.localScale = Vector3.one; newInvItem.GetComponent <RectTransform>().localScale = Vector3.one; } else if (gi.invSlotNumber < panelTab.childCount) { itemsInInventory.Remove(gi); Debug.Log("Inventory item loading shares occupied slot. Deleting item..."); Destroy(newInvItem); } else { Debug.Log("Inventory Item Loaded: " + gi.itemProperties.itemType + " " + gi.itemProperties.displayedName + " at " + panelTab.name + " slot " + gi.invSlotNumber); } }
private void Start() { itemSlots.AddRange(InventoryUISlot.FindObjectsOfType <InventoryUISlot>()); panelPots = GameObject.Find("Panel_Pots").transform; panelSeeds = GameObject.Find("Panel_Seeds").transform; panelDecor = GameObject.Find("Panel_Decor").transform; }
// Specifically to check if there is a free slot to allow purchase of a new item public bool CheckIfFreeSlot(GameItem gameItem) { Transform targetTab = InventoryTab(gameItem); Debug.Log(targetTab); List <InventoryUISlot> slots = new List <InventoryUISlot>(); slots.AddRange(targetTab.GetComponentsInChildren <InventoryUISlot>()); for (int i = 0; i < slots.Count; i++) { if (slots[i].myItem == null) { gameItem.invSlotNumber = i; targetSlot = slots[i]; Debug.Log("Found free slot at slot " + i); return(true); } else if (i == slots.Count) { return(false); } } return(false); }
public override void OpenPanel() { base.OpenPanel(); //Delete any old Inventory Slot Buttons from my grid int childCount = inventoryGridContainer.transform.childCount; for (int i = 0; i < childCount; i++) { Transform currentChild = inventoryGridContainer.transform.GetChild(i); Destroy(currentChild.gameObject); } //find Inventory manager InventoryManager im = GameObject.FindObjectOfType <InventoryManager>(); if (im != null) { //get Inventory manager's current inventory //FOR EVERY INVENTORY ITEM foreach (InventoryItem currentItem in im.InventoryList) { //Spawn a new Inventory Slot Button //Add button to my Inventory Grid GameObject slotButton = Instantiate(inventorySlotPrefab, inventoryGridContainer.transform); InventoryUISlot currentUISlot = slotButton.GetComponent <InventoryUISlot>(); if (currentUISlot != null) { currentUISlot.AssignInventoryItem(currentItem); } } } }
void GenerateUIWindowTable(Vector2 startingPos, int numColumns, int numRows, int windowWidth, int windowHeight) { inventoryTable = new InventoryUISlot[numRows, numColumns]; Vector2 posToPlaceUIWindow = startingPos; for (int r = 0; r < inventoryTable.GetLength(0); r++) { for (int c = 0; c < inventoryTable.GetLength(1); c++) { InventoryUISlot inventorySlot = Instantiate(InventorySlotPrefab, posToPlaceUIWindow, Quaternion.identity); posToPlaceUIWindow.x += windowWidth; inventoryTable[r, c] = inventorySlot; inventorySlot.SetSize(windowWidth, windowHeight); //Set Colour. //CanvasRenderer canvasRendererOfWindow = inventorySlot.GetComponent<CanvasRenderer>(); //Color colourOfUIWindow = (c + r) % 2 == 0 ? Color.grey : Color.black; //canvasRendererOfWindow.SetColor(colourOfUIWindow); } posToPlaceUIWindow = new Vector2(startingPos.x, startingPos.y - (windowHeight * (r + 1))); } }
void PlaceInventoryObjectInSlot(InventoryUISlot selectedSlot) { // If slot is not free, return to previous slot if (selectedSlot.TryPlaceItem(heldObject)) { selectedSlot.PlaceItemInSlot(heldObject); ClearHeldObject(); HidePlacePointMarkers(); // Runs even if just doing things in inventory. Could be cleaner. } else { PlaceInventoryObjectInSlot(InvUI.lastUsedSlot); // Return to previous slot } }
public override void HoldDown() { if (currentScreen == GameStates.gameScreens.mainGame && cameraEngaged == false) { if (heldObject == null) { Transform heldItem = GetSelectedObject(); //FOR DEBUG ONLY if (heldItem != null) { debugText.text = "Held item: " + heldItem.name; } if (heldItem != null && heldItem.CompareTag("moveable") && !holdingMoveable) { PickUpObject(heldItem); } } // If held in lower third of screen, move world item to inventory if (holdingMoveable && screenTouchPos.y < Screen.height / 3) { MoveWorldItemToInventory(); } // If player takes inv item out of inv but wants to return it without placing in world... else if (holdingInventoryItem && screenTouchPos.y < Screen.height / 3) { MM.OpenInventory(); currentScreen = GameStates.gameScreens.inventory; } else if (holdingMoveable || holdingInventoryItem) { // if item held on side of screen, move //if (!cameraMoving && screenTouchPos.x < 20) //{ // SwipeLeft(); //} //if (!cameraMoving && screenTouchPos.x > (Screen.width -20)) //{ // SwipeRight(); //} } } else if (currentScreen == GameStates.gameScreens.inventory) { if (heldObject == null && GetSelectedGUIObject() != null) { if (GetSelectedGUIObject().GetComponent <InventoryUISlot>()) { InventoryUISlot targetUISlot = GetSelectedGUIObject().GetComponent <InventoryUISlot>(); InvUI.lastUsedSlot = targetUISlot; // To record where to send the item if dropped somewhere invalid heldObject = targetUISlot.TakeItemFromSlot(); holdingInventoryItem = true; } } // For taking Inv Item into the world if (holdingInventoryItem && screenTouchPos.y > Screen.height / 2) { MM.CloseInventory(); currentScreen = GameStates.gameScreens.mainGame; ShowPlacePointMarkers(); } } }