Create an Unity inventory: Part 1: Basic data model

Create an Unity inventory: Part 1: Basic data model

I write this article to teach you how to do an inventory system in Unity, as is shown in the following picture:

Image for post

First we create a new project in Unity:

Image for post

We will use the Unity canvas so it would be interesting to see the Scene and the Game tabs at the same time:

Image for post

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:

Image for post

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:

Image for post

Now we need to import the images, and we will put them inside the Items folder:

Image for post

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:

Image for post

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:

Image for post

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:

Image for post

Then we hit the Apply button con the top right corner, and we will do the same process for the panel_blue:

Image for post

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:

Image for post

By default the script will look like this:

Image for post

And we are going to delete the built-in methods and the MonoBehaviour keyword:

Image for post

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:

Image for post

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:

Image for post

In our case we will have stats, so let?s say we have Power, then we would like to get its value:

Image for post

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:

Image for post

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:

Image for post

So we pass in all the Item?s properties to the constructor and now we need to assign them:

Image for post

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:

Image for post

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:

Image for post

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:

Image for post

And finally we want to take the stats:

Image for post

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:

Image for post

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():

Image for post

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:

Image for post

As follows:

Image for post

So back in our ItemDatabase we start filling our first Item:

Image for post

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:

Image for post

And we can create as many items we want to, separated by commas:

Image for post

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():

Image for post

So we will find the item by its id, and for that we loop over the items list using a method called Find:

Image for post

And we also would like to access those items using their title, as follows:

Image for post

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.

1

No Responses

Write a response