I write this article to teach you how to do an inventory system in Unity, as is shown in the following picture:
First we create a new project in Unity:
We will use the Unity canvas so it would be interesting to see the Scene and the Game tabs at the same time:
We will create two main folders: Scripts and Resources. It is important to name the second one as Resources because of it will have a special relation with our scripts and Unity itself. Inside Resources we create: Sprites>Items:
We will be using some free images for the items. I have put the Unity project and the images inside this zip: https://drive.google.com/open?id=1-o_8kWwfnXYPddgNPFmC8fCLx88h5myD
Our images will look like:
Now we need to import the images, and we will put them inside the Items folder:
It is important to be sure about those images? texture type property, because of it should be ?Sprite (2D and UI)?, because we will use them con a Canvas:
We do not want to have the buttons? images stretched, we want to have them tiled so they repeat their patterns and do not lose quality zooming in and modifying pixels. So we select the sprite called: buttonLong_blue and go to the Sprite Editor:
And we see a zoomed in version of the image with 4 green anchors con the sides. We will create a border to indicate that we want the top and bottom parts to be tiled horizontally, and the left and right parts to be tiled vertically. We write 7 for L, R, B and 7 for T:
Then we hit the Apply button con the top right corner, and we will do the same process for the panel_blue:
Then we are going to create the class Item to write what we consider to be a Inventory?s item object. Let?s create the C# script:
By default the script will look like this:
And we are going to delete the built-in methods and the MonoBehaviour keyword:
We want to have a way to identificate the Item so we will have an id. Then we would like to have a title and a text description. And it is needed to have an icon, so it will be our Sprite:
Another interesting data to have is a way to indicate a stat and get its value. For example let?s say we type ?sword?, it would be interesting to get the swordItem associated with it:
In our case we will have stats, so let?s say we have Power, then we would like to get its value:
We want to use a structure called Dictionary where we can type a string and get a value, so we store key, value pairs. The Dictionary will have the name stats and will keep string, int pairs as follows:
In fact we desire to have the ability to create our own new Items, so that we will use a constructor. Constructors are methods with the same name as the class they belong to:
So we pass in all the Item?s properties to the constructor and now we need to assign them:
To assign the icon we use the Resources folder we previously created, and we have a class built in Unity for that, where we write the folder to access the icons as follows:
And we see the line in red because of we are currently telling: Please grab a GameObject which is located in ?Sprites/Items/? but we do know that the icon is a Sprite so we write this:
So because of we are grabbing the icons which correspond to the item?s title, we would like to rename the sprites we imported to unity, for example sword_diamond to Diamond Sword:
And finally we want to take the stats:
In addition later we will use a constructor to copy one Item and to create another Item, so we write the following constructor where we grab an Item and copy all of it:
And the second thing we will do in this article is to create our database. It will be a C# script called ItemDatabase. Inside we wish to have a List to store all our items and keep track of them. In addition we will create a method to BuildDatabase():
Here we fill our items list. Please note that I have left in our Item script ?Sprite icon? which we are not using at all and needs to be deleted:
As follows:
So back in our ItemDatabase we start filling our first Item:
And now we have to create our dictionary to store the sword?s stats. First we create an empty dictionary and the inside it we create each row with curly braces and inside them a string for the stat?s name and an int for the stat?s value:
And we can create as many items we want to, separated by commas:
And the last thing we are going to write in this article is a way to find items. Before that we need to create the database con game starts, so when the game starts the method Awake() is called, and then we call to BuildDatabase():
So we will find the item by its id, and for that we loop over the items list using a method called Find:
And we also would like to access those items using their title, as follows:
So we have defined our Item and filled up our ItemDatabase. In the following article we will create our Inventory and we will be able to update and remove items con it to let the player keep all his equipment.
Thanks for reading.