top of page

DYNAMIC UNITY INVENTORY SYSTEM

ROLE

Solo

DESCRIPTION

This dynamic inventory asset is a tool that allows developers to quickly have a drag and drop cross trading inventory with currency.

TIP: Hover over images to play videos

Project Length

6 Weeks

GENRE

N.A.

PLATFORM

PC / MODULA ASSET

HOW IT WORKS

Inventory Component Overview

For every entity, player, enemy, shop etc, an 'Inventory' script can be assigned, this handles the backend of their inventory.  From it  these public variables can be modified:

  • Inventory size 

    • eg 5x5.

  • Shop item prefab

    • What spawns when an object is in an inventory slot

  • A row and column prefab

    • Can be customized with different layout options, eg center fill, left to right fill etc.

  • A reference to a tool tip prefab

    • This displays information about an inventory item when hovered over​

  • Slot prefab.

  • Inventory container, hot bar contained and tool bar container

    • Where the UI will be displayed

  • Money

  • Tool bar toggle

  • Inventory database reference

  • Items

    • Array of 'ShopItem'​

 

 

 

For every 'Inventory' there is, there's an 'InventoryUI' that handles the front end. This is attached to a UI component. This gets a reference to :

  • Its backend 'Inventory' script

  • A tool bar

  • Its money text UI that gets updated

  • Temp UI

    • Temporary item that is created when moving items around inventories​

For each game there is 1 Inventory database that is assigned to an empty gameobject and it stores an array of different 'ShopItem's that are attached to the same gameobject

ShopItem stores:

  • Item name

  • Descroption

  • Icon

  • Cost

  • Colour

  • Slot  number in inventory

  • Whether or not its a tool

'ShopItemUI' is attached to a UI image prefab and stores the same variables as 'ShopItem' plus:

  • original parent

    • stored for when its moved around in inventories​

  • 'InventoryUI' that it belongs to

 

 

Lastly there's 'Slot', which is attached to an image prefab and stores

  • If its a tool slot

  • Item (ShopItem) its storing

  • Its 'InventoryUI' 

Initialization

When a game is started, each 'InventoryUI' grabs all the components from its backend counter part 'Inventory'. It then goes through an loop and assigned empty Slot prefabs in a r'owGameObject,' when it gets to the end of a row  (inventorySize.x) creates a new 'colummGameObject' until it reaches the end of 'inventorySize'.  If the slot is in the 'ToolUIContainter' it marks that slot as 'isToolSlot'

After the loop, it instantiates the 'toolTip' prefab.

Adding items

When the 'Add Item' button is pressed it calls 'AddItem' on 'InventoryUI'. For debugging purposes it add a random item from 'InventoryDatabase' and goes through its 'InventorySlots' checking if its item is null, and if the slot type is for tools or not. If the item can go in the slot, break out of the look and create a temp 'ShopItemUI' from 'ShopItemPrefab' at the slots transform.

Set the 'InventoryUI's 'InventorySlots' at the slot position to the tempItem, then call 'UpdateInventory' with the inventorySlot for 'Inventory'. This will go through the entire inventory and match each item to the slots in 'InventoryUI'

Swapping items

When the player hover's their mouse over a 'ShopItemUI' 'OnMouseEnter' is called. For 'ShopItemUI', this sets its 'originalParent' to its current parent . Foreach 'InventoryUI'  in calls 'Select' passing in the 'ShopItemUI' to let them all know what is being selected.

The tooltip is enabled, and its text is updated to the 'ShopItemUI's display values.

When the player drags the mouse while over the 'ShopItemUI' it will set the icon's position to the mouse's position and set its parent to a gameobject with a position of (0, 0, 0) , so that the mouse position is accurate. Sets the tool tip is active, and updates the values.

When the mouse is let go, it sets the items parent back to 'originalParent' and calls 'SwapItems' in its 'inventoryUI'. Then turns off the tooltip.

In 'SwapItem', it finds the slot index of both items in their slots. If both inventories have enough money to switch, the item can go into the slot. If the new slot isn't empty, get the slot's 'inventoryUI's item and set it to the new one. It then does the same for this inventories' item.  If the other item slot is empty, set the temp item as a new 'shopItemPrefab',assign it's item, and assign the slot to that item. Set the old slot's item to null because it will be empty.

Disadvantages of This System

I wrote this system a while ago and am yet to update it. Going through it again I've noticed that the tool bar, and hotbar are apart of 'Inventory size' making it annoying the configure the inventory size without making each slot bigger and making the UI way to big. If I was to update it/ remake it, I would make them independent of each other's size.

When I create the inventory, I use a single int value 'inventorySize', and when it gets to the end of the 'inventory.inventorySize.x'  it create a new column. the naming conventions are really confusing, and instead I could do a nested for loop for the 'inventory.inventorySize.x' and y, and when it gets to the end of the x, create a new column.

bottom of page