- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
NotThatSpecial · Apr 21, 2015 at 02:25 PM ·
inventory
Need help with Inventory stacker. PLEASE. Almost working!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Inventory : MonoBehaviour
{
public int slotsX, slotsY;
public GUISkin skin;
public List<Item> inventory = new List<Item>();
public List<Item> slots = new List<Item>();
private bool showInventory;
private ItemDatabase database;
private bool showTooltip;
private string tooltip;
public GameObject player, mainCamera;
public bool mouseLook;
public bool draggingItem;
public Item draggedItem;
public int prevIndex;
public int box;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
mainCamera = GameObject.FindGameObjectWithTag("CameraHead");
mouseLook = true;
for(int i = 0; i < (slotsX * slotsY); i++)
{
slots.Add(new Item());
inventory.Add(new Item());
}
database = GameObject.FindGameObjectWithTag("ItemDatabase").GetComponent<ItemDatabase>();
}
void Update()
{
Screen.showCursor = true;
if(mouseLook)
{
// player.GetComponent<MouseLook>().enabled = true;
// mainCamera.GetComponent<MouseLook>().enabled = true;
}
else
{
//player.GetComponent<MouseLook>().enabled = false;
// mainCamera.GetComponent<MouseLook>().enabled = false;
}
if(Input.GetKeyDown(KeyCode.I))
{
mouseLook = !mouseLook;
showInventory = !showInventory;
AddItem(0);
}
}
void OnGUI()
{
if(Network.peerType != NetworkPeerType.Disconnected)
{
tooltip = "";
GUI.skin = skin;
if(showInventory)
{
DrawInventory();
if(showTooltip)
GUI.Box(new Rect(Event.current.mousePosition.x + 15f,Event.current.mousePosition.y,200,200),tooltip);
}
if(draggingItem)
{
GUI.DrawTexture(new Rect(Event.current.mousePosition.x,Event.current.mousePosition.y,50,50),draggedItem.itemIcon);
}
}
}
void DrawInventory()
{
Event e = Event.current;
int i = 0;
for(int y = 0; y < slotsY; y++)
{
for(int x = 0; x < slotsX; x++)
{
Rect slotRect = new Rect(x * 60 + Screen.width/2 - slotsX * 30, y * 60 + Screen.height/2 - slotsY * 30,50, 50);
if(!slots[i].itemStackable)
GUI.Box (slotRect, "");
if(slots[i].itemStackable)
GUI.Box (slotRect, slots[i].itemStacked.ToString());
slots[i] = inventory[i];
box = i;
if(slots[i].itemName != null)
{
GUI.DrawTexture(slotRect, slots[i].itemIcon);
if(slotRect.Contains (e.mousePosition))
{
CreateTooltip(slots[i]);
showTooltip = true;
if(!slots[i].itemStackable)
{
if(e.button == 0 && e.type == EventType.mouseDrag && !draggingItem)
{
draggingItem = true;
prevIndex = i;
draggedItem = slots[i];
inventory[i] = new Item();
}
if(e.type == EventType.mouseUp && draggingItem)
{
inventory[prevIndex] = inventory[i];
inventory[i] = draggedItem;
draggingItem = false;
draggedItem = null;
}
if(e.isMouse && e.type == EventType.MouseDown && e.button == 1)
{
if(slots[i].itemType == Item.ItemType.Consumable)
{
UseConsumable(slots[i].itemID,i,true);
print ("USED CONSUMABLE: " + slots[i].itemName + "," + slots[i].itemID);
}
}
}
if(slots[i].itemStackable)
{
if(e.button == 0 && e.type == EventType.mouseDrag && !draggingItem)
{
draggingItem = true;
prevIndex = i;
draggedItem = slots[i];
inventory[i] = new Item();
}
if(e.type == EventType.mouseUp && draggingItem)
{
if(slots[i].itemID != draggedItem.itemID)
{
draggingItem = false;
inventory[prevIndex] = inventory[i];
inventory[i] = draggedItem;
draggedItem = new Item();
}
if(slots[i].itemID == draggedItem.itemID)
{
draggingItem = false;
inventory[i].itemStacked += draggedItem.itemStacked;
draggedItem = new Item();
}
}
if(e.isMouse && e.type == EventType.MouseDown && e.button == 1)
{
if(slots[i].itemType == Item.ItemType.Consumable)
{
UseConsumable(slots[i].itemID,i,true);
print ("USED CONSUMABLE: " + slots[i].itemName + "," + slots[i].itemID);
}
}
}
}
}
else
{
if(slotRect.Contains (e.mousePosition))
{
if(e.type == EventType.mouseUp && draggingItem)
{
Debug.Log("Placed " + draggedItem.itemName + " at " + i);
inventory[i] = draggedItem;
draggingItem = false;
draggedItem = null;
}
}
}
if(tooltip == "")
{
showTooltip = false;
}
i++;
}
}
}
private void UseConsumable(int id, int slot, bool deleteitem)
{
if(id == 2)
{
//increase stat
}
if(deleteitem)
{
if(inventory[slot].itemStacked > 1)
{
inventory[slot].itemStacked --;
}
else
{
inventory[slot] = new Item();
}
}
}
string CreateTooltip(Item item)
{
tooltip = item.itemName + "\n" + item.itemDesc + "\n" + item.itemPower;
return tooltip;
}
void RemoveItem(int id)
{
for (int i = 0; i < inventory.Count; i++)
{
if(inventory[i].itemID == id)
{
inventory[i] = new Item();
break;
}
}
}
void AddItem(int id)
{
for(int i = 0; i<inventory.Count; i++)
{
if(inventory[i].itemName == null)
{
for(int j = 0; j < database.items.Count; j++)
{
if(database.items[j].itemID == id)
{
inventory[i] = database.items[j];
}
}
break;
}
}
}
bool InventoryContains(int id)
{
bool result = false;
for(int i = 0; i<inventory.Count; i++)
{
result = inventory[i].itemID == id;
if(result)
{
break;
}
}
return result;
}
}
Whenever I try to stack the items that are the same items and have multiple of them in my inventory, it just multiplies the amounts. i tried adding the dragging items amount to the slots amount but that doesn't work and then the other items in the inventory show the same amount of items stacked. PLEASE HELP
Comment
Hey there, can i take a look at your Item script and ItemDatabase script?
Your answer
![](https://koobas.hobune.stream/wayback/20220613193635im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Confirming item pickup in inventory 0 Answers
Using enums to design an inventory system? 1 Answer
Issues with Inventory script 0 Answers