Unity INVENTORY SYSTEM Tutorial #1 - item drag & drop

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
creating a good inventory system for your game can be a bit tricky especially if you don't want to over complicate things and make it easily expandable I have came up with this decent inventory system we'll be using scriptable objects to make the items so that you can easily add as many items as you want we'll be able to pick up the items drop them some of the items will also be stackable obviously we will be able to move them across the inventory grid and also put them to the Hotbar so that we'll be able to choose which weapon or item we want to be holding in our hand we will start with a empty scene I have also downloaded three packages one is the Fantastic URL starter pack for the UI then the survival melee weapons pack which has 4 weapons you will be able to find all of these packages in the description and for the player controller we are using the realistic FBS Controller free in this scene I will create a 3D object playing HD player controller and because we don't need to use some stuff I will just delete the items that he is holding in his hands to do that we'll need to unpack the prefect next we will Begin by creating the simply y so right click in the hierarchy go under UI and we can begin by creating some background so we can use panel for that switch mode to 2D and just set the source image to some of the textures so this will be our background I will also create two empty objects one will be for the inventory and the other one will be for the Pod bar so we have these two backgrounds also don't forget to set the canvas scale mode in the canvas to scale with screen size so that it looks same on all devices create two new game objects one will be for the items and one for the slots and we'll copy them to hot part 2. so the structure of the inventory should look like this and now under the slots object we'll create these slots so UI image drag some texture to the image I like to use this green one one quick tip if you have many of these thoughts and you want to move them down for example by 120 to the position y you can just type minus 120 and it will calculate you the new position so now we have the basic inventory and because we have it all under the inventory parent we can just turn it on or off as we want also don't forget to add these slots for your bar here I will set the scale of all of them 2.9 because later we'll be making the slot that we have selected bigger this looks good enough now we will get decoding so I have created folder for these scripts and we'll create a script inventory item another one for inventory slot inventory manager item so which stands for the script and object we will start by creating descriptive object so open the item script we want it to derive from descriptive objects instead of mono Behavior create unset menu so that we can instantiate the items in our assets foreign and we'll add some properties public string for name public Sprite for the icon public game object for the prefab of the item and also public integer for the max size of the stack now we should be able to right click create script of objects and create instance for the item and for each item we should be able to set the name icon prefab and the max size of the stack here I have the icons that I made by myself and we just need to set the texture type to Sprite 2D and UI if you want to set the icon property of the Hammer by dragging the Sprite you want to lock the inspector then you can open the icons and just drag it like that [Music] now we'll do some coding in the inventory item script so that we can correctly show the item in the inventory based on the scriptable object that we assigned to it we don't need to add using unit engine.ui because we'll be changing the Sprite of the item now make a public variable for the item strip double object and a serialized field variable which will be for the image that you want to set the reason that we are using serialize field is that we'll be able to set the image in the inspector but other scripts will not be able to change it and in update we can just say icon image dot Sprite is equal and get the Sprite from the item splitter object just like that right now we don't actually have a preset for the item so we can just create empty object and under it we will add the icon so just add UI and image and we would also probably want to have some background so just copy the image and on the background we can decrease the opacity and also make sure that the background is actually in the background so drag it over the icon like this to the item parent we obviously want to add the inventory item script and set the icon image I will also make a brief up from the item because later we will need to instantiate it and we can put as many items as we want under the item's parent and set the item scriptable objects yeah you can see that it is correctly displaying the item based on the scriptable object that we set here now let's jump to the inventory slot where we will just add a public variable for the item that it is currently holding hence to make it easier for us later I will add a public void which we'll call set Health item to the parameters we will have just a game object for the item and what this will do is that at the same time we will be able to easily set the health item and also set its position to this slot because when we have some item and it is being held by the slot we obviously want to set its position to this slot this will make it that later in the inventory manager we don't need to be setting the health item and also setting its position we can just call this void now we can get the main part which is the inventory manager so I will put it through the canvas and get into the script here I will add both using Unity engine.ui because we will be changing some stuff with UI and using unit engine.event systems this will allow us to get to know if we are actually clicking on some of the items next to the mono Behavior we will Implement two interfaces one of them is I pointer down Handler and the other one is eye pointer app Handler if you are not familiar with interfaces you can also check one of my videos so now we need to implement the on pointer down void don't forget to make it public [Music] and we also need to implement the void on pointer app make sure that you have the pointer event data in the parameters this is just some data about the pointer such as the object that we have clicked on if we have clicked with the left or right button and so on so now from this on pointer down void we should be able to know which item we clicked on so we can try debugger not and we can access the game object from the event data that pointer currentlycast Dot Game object now it is telling us that we are clicking only slot now on the icon and so on but we don't want the pointer to register the icon we only want it to register this slot because how do we know if there is an item we could do the raycast on the icon itself but it is better to ask the item slot if it is holding some item and because we don't once devoid on pointer down to register the icon we'll need to go to the prefab of the item which we have created and select the background and the icon and stick off the raycast target just like that now it is registering only the slot even when you are clicking on the item but it is also registering when we right click which is not what we want so in the onboard enter downward we need to ask if we are clicking with the like button then we will get the object that we have clicked on and try to get the inventory slot component [Music] so we are checking if we have clicked with the left button then we are getting the clicked object getting the slot component and if there is some slot strip which means that this slot we can do some other stuff I will create a game object variable which will be for the direct object the object that you are currently dragging so when we click on this slot and there is some item in it we can set eject object to the health item of the slot and set the health item on the slot to now and to make the movement of the item work in the update we can just ask if the direct object is not equal to now and then we can just move the eject object to the position of the mouse in this scene I will just add the inventory slot script to all of the source and if some of these slots is currently holding some item don't forget to set it otherwise you will not be able to pick it up so now when I'm clicking to any of these slots it is not doing anything but when I click 2D full slot you can see that I have successfully picked up the hammer but I can't do anything with it just yet so we will have to edit the on pointer up void we want to place the item to the slot only if there is some game object in the event data and the direct object is not equal to now and at the same time we want to make sure that we have clicked with the left button so there is a lot of conditions and then we will just check if there is this slot component on the object and if it is not holding any item [Music] and when this is true we can just set the direct object to null but before that we want to set the health item of the slot to the direct object and for this we can use our void which we have created on the slot so that we don't have to be setting the position of the item now we should be able to pick the item up place it somewhere else yeah this is working just fine but when I take the hammer and try to place it to the club you can see that it is broken because we have no condition for that so else if there is this slot but it is currently holding some item we want to put the direct object backed to its last slot so else if there is some slot but it is currently holding some item we want to send the direct object back to the last slot for this we'll need to define a new game object variable last item slot we will set this variable when we are picking up some item so right here so then when there is already some item we'll again set the direct object to now but before that we will set the health item of the last slot to the direct object so I'm still able to pick up the items but when I try to put the hammer over the band you can see that it gets back to its last item slot but there is other way how you can do it which is by switching the items which I think is better for this we need to set the health item of the last item slot to the new item which you will get from the slot and we need to set the health item of the new slot to the direct object so it is simple as that and now you can see that we are able to switch the items and this would obviously work even if you would have the whole inventory full you could still move the items around switch them and so on there are still definitely some bugs and features that you would want to implement so in next video we'll implement the whole bar make it so that we can throw the items away pick them up and also make these stackable items I hope that this video was helpful if you have any questions drop them down to the comments don't forget to like subscribe and I will see you in next videos bye
Info
Channel: Freedom Coding
Views: 2,338
Rating: undefined out of 5
Keywords: Unity, Unity2D, UnityEngine, GameDevelopement, IndieGameDev, PCGames, Coding, C#, Easy, Tutorial, Education, Unity3D, Programming, Bolt, UnityTurorials
Id: 2MPn3ir0wJ8
Channel Id: undefined
Length: 15min 1sec (901 seconds)
Published: Thu Jul 27 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.