How to Create an INVENTORY SYSTEM in Unity - Ep1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
back when I first started this channel I was posting videos of a survival game that I was developing in unity since then that project has been more or less scrapped but a common question I would get is how does the inventory system actually work today I figured almost two or three years later that I would start a little miniseries on that exact question and hopefully be able to help the people who are currently in the same situation as I was three years ago so I've gone ahead and set up a little Unity scene I've got a first- person player controller that I just imported from the asset store I have four little um item cubes got a red one yellow one green one and a blue one these are literally just standard Unity cubes with material added and a rigid body um I've also added a canvas but currently has nothing on it and that's about it down here in the script section I've added an inventory item and Slot script all of these are empty at the moment so if you're following along just make these three scripts and we should be good to go first thing I want to set up here is where we're actually going to be putting these scripts so so I'm going to come over to my item script here and I'm going to set these to have the item script there I'm also going to come here add a layer and call that layer item and make sure all the items that you're going to have are set to have this item layer on the player script here I'm going to add my inventory script just at the top right there and finally I'm going to come into my canvas and set up um the actual slots that we're going to be using this is a little bit complicated so make sure you follow along and I'll try and explain it as best as I can first of all I'm going to create an empty game object and call this inventory and then you're going to come to this little button here click that hold alt and then click this one here what that's going to do is stretch this um little game object from this tiny Square to that going to open this up again create another empty and call this one background slots I'm going to do the same thing again open this hold alt on my keyboard and click this button again on this I'm going to add something called a grid layout group you can find that obviously by typing grid grid layout group I'm going to make my slots about 50 by 5050 wide set these little spaces to be 10 and 10 and then I'm going to come here and set a fixed column count of about seven right clicking this again I'm going to create a UI image and that is going to represent our slot so if you duplicate this now you'll see they all line up how we how we just described it if we go over seven it'll leap around like this so I'm going to go ahead and duplicate this about 20 times and I have 21 slots I'm going to set um this child alignment to be middle Center and as you can see I now have this nice inventory there I'm also going to come here and create another UI image drag this above our background slots but make sure it's inside the inventory object grow that to full and set it to be black and a bit transparent that's just so we have a clearer view I'm then going to select all of these and choose a inventory slots um image that I have it's literally just a white Bard with a slightly grayed out background and set these to be a little bit transparent just because that's how I like it but you can set that up with whatever style you like and again come in here again and duplicate these background slots and call these the inventory slots open that up up I'm going to delete all but one of these and set it's Sprite to be none and then going to right click here come in here to UI hit text text Mage Pro import TMP [Music] Essentials and you're going to see it's going to create a TM Pro text for me here I'm then going to set this to be zero and come here alt full screen that I've also clicked these two alignment buttons to make it align into the corner I'm going to shrink down this text a little bit too we're going to use this for to display the quantity of the item that we have I'm also going to come here to the left and just oh sorry the right and make that five just so it looks comes a bit further away maybe three there you go um once you're done with that you're going to want to come in here to the image's color and set it transparency all the way down to zero you're also going to want sure that the text is set to nothing I just had it set to zero to demonstrate and then come to this inventory slot um and add the slot script there you go we're then going to want to duplicate this the same amount of times that you duplicated your background slots we had it 20 it's actually 21 but there you go one last thing you want to make sure is done here you're going to set your inventory to be off by default by clicking on that and clicking this little tick and then we're going to create a Crosshair to let you know where you're actually looking um to do this we're just going to come in here create a little square object Sprite and set this to be knob and everyone will have this by default in unity and I'll just set this width and height to be 10 and 10 and then here we have our crossair going to label this Crosshair I'm going to make sure rast Target is off on this Crosshair to make sure that we don't accidentally recast it and I'm going to come in here and create UI text mesh Pro and this is going to be when we're looking at an object it's going to allow us to determine where we're actually looking so to do that I'm just going to come in here like this hold alt click on this one and maybe make it a little bit lower 35 and set it scale or font size to be about 20 I'm also just going to leave this white because that's how I like it so I'm going to make it just exactly - 30 there you go this text just set this to be off by default um the reason for that is when we look at an object we're going to want to know what we're picking up before we've actually picked it up and we're going to use that text to actually display the name of the object so I've just gone ahead and open up each script here you can see I have my inventory item and Slot script and what we're going to do first of all is come to our item script and we're just going to set this up so there's a few main components for the item script um obviously we're going to need a public string we're going to call this name and this is going to be the name of our item so we're just going to set this by default to new item one thing you're going to notice is there going to be a little green squiggly line underneath this because when you're looking at an item name is or for a game object Unity name is usually used to get the name of the transform so what we're going to do between public string is just write new and that's going to get rid of that little squiggly line for us come on here we're going to create a public Sprite we're going to call this icon um actually above this here we're going to write public string description equals new description just in case you guys want that um public spr icon there you go we're also going to create a public int for current quantity and then we're going to make a public int for Max quantity and that's going to allow us to set up some sort of Minecraft like logic where if you have 64 of one object you want it to sort of go to a new slot we're going to set this to Beal to one by default and then the max quantity is going to be equal to let's say 16 by default moving over to our slot script here there's a few things that we're going to want to set up first of all we're going to create a private item and we're going to call this held item and this is going to just we're using this variable essentially to determine which item we're holding in this current slot we're also going to set up some colors this going to be useful for later we have to repeat ourselves we're going to call this one Opa opaque even sorry um new color one one one one then we're going to set up another private color call this one transparent same thing new color one one one and then this final one is going to be equal to zero meaning that it's transparent coming in here we're then going to go private image call this this slot image and one last thing we're going to want to do is create a public or and we're going to call this Orit now next to Mono Behavior here we're going to add a couple of things I pointer enter Handler and then I pointer exit Handler and this going to allow us determine if we're actually hovered over this slot or if we've exited hovering over this slot create a public void we're going to call this initialize slot initialize don't know if I spelled that right there you go initialize slot and then we're going to create a public void set item going to take in an item for this function and finally at the bottom of this here we're going to set a public item get item and this is going to return L item like so now obviously you can see these two have little red Squigly lines on need so to fix that we're going to create a public void on pointer enter and you won't get any prompts to auto fill that for you unfortunately pointer invent data point data and then in this we're just going to write hovered equals true then we're going to come ahead and just copy and paste this and instead of this enter bit here we're going to just write exit and you'll see those squiggly lines just disappear here we're going to write hovered equals false also did it for me there um initialize slot and set item we will set up in a bit but I think it's time we move over to the inventory here to get this set up as well so here we are in the inventory script and we're going to set up a few variables here as well first one's first is a header and we're going to call this UI then we're going to create a public game object inventory and it's going to be a reference to our inventory object and it will allow us to set it on and off so we can hide it and unhide it then we're going to create a public list and we're going to call this slot there we're going to call this inventory slots this is going to be equal to a new list slot just there we're going to initialize it like that like so beneath this we're going to create a public image variable and obviously that would't work sorry so we're going to duplicate this using Unity engine and using Unity engine. UI Public Image we're going to call this Crosshair and then again we're going to come up here and we're going to use using TM Pro if you aren't using TM Pro and you're using Legacy text you don't have to use this um but underneath this we're going to come create public tmore text and this is going to be our item hover TT like so beneath this we're going to create another header and we're going to call this of bra cast and we're going to create a public float bra cast distance we going to equal this to be about five by default and then a public layer mask item layer and that's going to be set to the layer we created earlier coming in here we're going to create a public void star a public void update couple more functions we're going to need so we're going to create a private void item rcast and inside here we're going to have a Boolean called has clicked default this to be boss so the first thing we're going to do in this update function is call this item rast function and we're calling this up item rast function every frame regardless of we clicked or not but we also going to pass in input. get mouse button down zero so if we left click with this has clicked Boolean will be set to True otherwise we'll just set it to false by default every time this is going to allow us to detect an object before we actually click on it we're also going to come here and create a little Loop for each slot you by slots in inventory slots and we're just going to go slots. initialize slot this is a function we haven't created in our actual slot script sorry um this is a function we haven't actually created yet let's come over to our slot oh we have already created it initialize slot there you go just make sure I spelled that right obviously I totally spelled that right nothing to see here um we're going to move move on from that so we're actually going to come back to our slot script and set up this initialized slot the reason for the initialized slot function instead of a maybe a public void start is that the inventory script is going to hold all these slots and it'll be off by default when we first open the project and chances are people aren't going to open the inventory before they've actually tried to pick up an item so what we're going to do instead is call this initialized slot from our inventory start which will always be active allowing us to make sure these slots are actually set up and ready to go before we've actually opened the inventory so inside of this we're going to go this slot image is equal to game object. get component image then we're going to come here and go to this slot image. Sprite is equal to null and this like image. color is equal to transparent and now you're going to see why these little colors that we made are important finally we're going to come here and create a set item null and this is just going to make sure that our item has nothing in it before we've actually started so moving on to our item recous function here we're going to start setting this up first thing we're going to want to do is set item hover text do text equal to zero or equal to null then we're going to set up our raycast here and we're going to call this RayRay is equal to camera. main screenpoint array Crosshair transform. position and this is going to be used for the actual rast where we're rting to so you want to make sure that your Crosshair is set to the very center of your screen then we're going to go rast hit hit that's just setting up our rast hit variable here you're going to do um the actual recast so if physics not raycast Ray out hit and rast distance which is the variable up here which we Cur have set to five and then we're going to set the layer to the item layer variable there we again we set this up earlier if hit. collider doesn't equals null so we have hit an object the reason we aren't checking for a tag here is because we have this item layer and the only objects on this item layer should be your actual items so we won't have to look for it tag to ensure that we're actually looking at an item because we can kind of just assume that if we are getting anything out of this hit it will be an item we're then going to set up this if has clicked and then we're going to do some Logic for an else so this will let us determine if we're trying to pick up the object which will happen here or if we're trying to get the name of the object to allow us to show to the player what they're about to pick up before they picked it up so if we have clicked we're going to do item new item is equal to hit. collider dog get component item just like that then we're going to go if new item so if new item actually exists and we haven't just grabbed nothing of the object we're going to create an add item to inventory function call and we're going to pass in new item obviously this function doesn't exist yet so we should go ahead and create that private void add an item to inventory and we're going to taking an item item open this up here like so oh I shouldn't have r that the end sry there you go we also we're just going to come back to this item rast to finish this up so if we haven't actually clicked we're just going to go item new item anyway um same thing again so we can just copy and paste this code there and then if new item and instead what we're going to do is go item hover text. text is equal to new item. name and that's all we have to do for this rast script so moving on to our add item to inventory function there's um kind of a few things I should explain here we have essentially a three-step plan for how we want to add items to our inventory to do this we're going to have to record how much um quantity is left over throughout the entire process so we're going to just do int left over quantity is equal to item to add do current quantity then you're going to want to create a slot we're going to call this open slot is equal to n for now this just allows us to search for an open slot while we're actually doing the rest of the quantity logic we're going to create a quick little loop we're going to go for in I equals z i smaller than inventory slots do count and then finally I Plus+ open curly brackets there we're going to go for an item item is equal to inventory slots I do get item there there you go then we're going to go if held item doesn't equals null and and item to add. name is equal to H item. name we're then going to do a little bit of logic there so essentially what this means is if the slot that we're currently looking at has an item and the item has the same name as the item to add we can assume that those are the same objects so we can try and and stack those up instead of adding it to a new slot so what we're going to do here is go int fre space in slot is equal to held item. Max quantity minus held item. current quantity essentially this integer is going to be equal to how much space these current items in the slot have for the item that we're trying to add coming down here we're then going to set up another if free space and Slot is bigger or equal to leftover quantity this essentially means that we can fit the entire object item to add here into this slot so what we're going to do is we're going to go held item do current quantity plus equals leftover quantity then we're going to come here and we're going to destroy the item to add. game object the reason we can destroy this without having to worry about it is that we've added the entire item's quantity to an item that's already in our inventory which means that essentially we no longer need to worry about that object because it has an it has a quantity of zero under this second curly bracket underneath this first if we're going to create another else if um this else if is going to say h item equals equals null and what we're doing here is we're going to get this open slot so ENT if held item equals equals to n we know that the slot we're currently looking at has no item so what we can do is record the open slot is going to be equal to inventory slots I this is useful essentially for if we have tried to add the entire quantity of the item to add to other slots but we couldn't do it we're going to want to we're going to want to know which slot is open to add the item to another thing we should do here is actually if exclamation Mark open slot this just means that we won't do this every time we get to this point but only a singular time so we get the first open slot underneath all of this little loop we're going to go if leftover quantity is bigger than zero meaning essentially that we weren't able to add the entire items quantity to other items in the inventory already and and open slot so we actually have a slot to add it to we can instead go to item slot open slot. set item item to add then we're going to go item to add do current quantity is equal to leftover quantity then we're going to go item to add. game object do set active B just like so scroll down a bit sorry else and this is in the situation where we have leftover quantity but no open slots which we're then going to go to item to add dot current quantity is equal to leftover quantity but we can't set it active or destroy this object because it still exists there is still quantity left over and we weren't able to add it to our inventory and that's just about it for the add item to inventory scripts here and our array cast but there's one last thing that we want to set up and this is a little function down at the bottom here and we're going to call this toggle inventory private void toggle inventory and then we're going to create a ball call this enable so what we're going to do here is we're going to go inventory. setactive enable essentially what we're doing here is we're turning on our inventory object on or off according to this enable function or this enal Boolean even and when we have our inventory open we kind of want our cursor to be able to be visible and move around so the actual the player can interact with the items in the inventory so what we're going to do is go cursor do lock state is equal to and then we're going to go enable question mark cursor. loock mode. none sorry didn't didn't do that right cursor lock mode none colon cursor lock mode locked essentially this is a little flip function here so cursor delock state if enable is true then we're going to have no lock on our cursor if it's false we're going to lock the cursor to the center of the screen and we also need to do this for cursor. visibility so cursor. visible is equal to enable just like that we don't need to do anything fancy like this with the question mark and the co on there we're just equaling it directly to enable now one thing you might want to do for you um it's kind of something individual to your own project you're going to want to disable the rotation of the camera finally we're going to take this toggle inventory function we're going to come up to our update again and then we're going to go if input. get key down key code and you can set this whatever you want I'm going to make it maybe e this will be how I enable and disable my inventory we're going to go totle inventory and then I'm going to do this with inventory active in hierarchy um one other thing you might want to come up into your start here and go Hogle inventory then pass in false that'll just set everything to be off by default and here we're just doing it a little flip so if the inventory is already active we're passing in the opposite of that this is what this little exclamation mark means so if the inventory is active we're passing in false and if if it's not active we're passing in true so once we're done with this we're then going to finally go to our slots here and we're going to set up this set item script or set item set item function um this is super quick we're going to go held item is equal to item if item doesn't equals null underneath this we're going to create an else so if the item actually exists we're going to go this slot image do Sprite is equal to held item. icon then we're going to go this slot image. color is equal to opaque and then copy and paste this into our else and here held item. icon we're going to intend set this to null and instead of opaque we're setting this to be transparent and that is everything so finally back in our actual scene we're going to set up some of these variables we should have already done pretty much all of this so we're going to come here set our Crosshair to be done that our item poex to be equals to this text underneath the Crosshair our inventory is going to be our entire inventory object there inventory slots so what you want to do for this is come up to this top right here and click this lock and this means no matter where you click this player will always be locked on your inspector come into your inventory open up your inventory slots select all of those and drag them into your inventory slots section there and you see I have 21 there come here to your Rec assistance I've already defaulted that to five so that's fine and we're going to set the item layer to the item layer you created so our M's just called item like so on each of our items here we're going to come over we have our red item we'll do that and we'll set its Sprite to be a little sprite I created earlier you can see these These are literally just I open up paint 3D and build those up I'm here for our yellow set our Sprite your Sprites can be anything you can download them if you want call this yellow item then we have our green item set that to be green and finally our blue item and we're going to set this to be blue like so and that's just about everything so if we go ahead and play this and maximize and open up our game here so we've open up our scene here you can see if we hover over the items that text that we set up earlier will display the name of the item we're currently looking at there you go Blue item yellow item red item and green item we open our inventory there's nothing there click on the blue item there we go on the green there we go red and yellow now we have all over our items we can't interact with them in any way I'll set this up on episode two but for now this just works you can see the items in the inventory you can pick up items and they will stack correctly so one last thing I thought we should set up in today's episode was actually the um quantity of our items I figure that's an important thing just to display that all that hard work didn't go to waste so we're going to create a new public void going to call this update data open that up this is in our slot. CS script by the way if held item doesn't equals null we're then going to come in here and we're going to go this slot quantity text we haven't actually done this yet so I should do this up here we're going to create using TM Pro come in here we're going to create public tmore text that like so we'll set this up in the initialize slot in a moment as well do text is equal [Music] to H item do current quantity to string and then beneath this we're just going to go else this item quantity text. text is equal to nothing we're going to call this update data function from our set item script like so update data and update data then up here we actually need to get this slot quantity text in our initialized slot function here so what we're going to do for that is this SL in quty text is equal to game object dot oh actually no we're going to do transer form. get child Z that's the first child which should be your text. get component _ text open CL brackets there you go one last place we're going to want to add this update data function to um is our actual inventory here so after we've added an item to the inventory the quantity of that slot will then change so here we're going to want to do um inventory slots I dot update data like so and then at the very end of this we're going to want to do this again so inventory slots I update data and that's just about it to be honest these are the two places you're going to want to add that to so coming back to our scene here hit play and if we have maybe three red items you can see inventory is empty 11 one one one two and three and there you go your inventory is now updating correctly
Info
Channel: HawkesByte
Views: 7,571
Rating: undefined out of 5
Keywords: unity inventory system tutorial, unity3d inventory, how to make inventory unity, inventory, inventory in unity, inventory system, inventory system unity, inventory tutorial, inventory unity, inventory unity 2d, rpg inventory unity, unity, unity 2019 inventory, unity bag system, unity c# inventory, unity inventory, unity inventory c#, unity inventory rpg, unity inventory system, unity inventory tutorial, unity inventory ui, unity item inventory, unity tutorial, unity3d
Id: 5l2D6xtZIgk
Channel Id: undefined
Length: 31min 36sec (1896 seconds)
Published: Mon Dec 04 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.