Creating An Inventory System in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
whether you're making a role-playing game management game strategy game first person shooter puzzle game or any other genre under the sun it's pretty likely that at some point in time you're going to want to design some kind of system for your player to hold items and have a place for them to sort through or interact with them in some way the specifics of what this system will look like will definitely vary from genre to genre but the one thing that's sure is that you'll need to build the logic for collecting items displaying them managing them and probably removing them so allow me to ready my rucksack and pull out the materials i've gathered over the years to help you put something like this together hi there i'm matt and welcome to game dev guide and in this episode we're going to take a look at how we can build an inventory system in unity we're going to look at how we can build an index of inventory items for our game through scriptable objects we're then going to build out a system for those objects to be added to stacked and removed from and we're also going to look at how we can build out a ui to visually interact with our inventory system as an additional bonus we're also going to build a script that will automatically generate an icon for each of our items in the ui before we get started though i'd like to take a minute to thank the sponsor of this video core core is a free pc gaming platform where anyone can play create and share single player or multiplayer pc games it's powered by unreal engine meaning that you can create games with high quality aaa graphics and is available to download for free on the epic store if you're a beginner looking to get started in game development it's perfect as there's no coding required and there are thousands of free music sound and art assets to choose from so it's possible to create a game by yourself faster than ever before core helps you build games from scratch and includes built-in multiplayer functionality many of you who are watching are more advanced users so while it's not required if you did want to get more hands-on you can as core allows you to kit-bash your own 3d models and write your own game logic via the lua scripting language core offers a 50 revenue share with its game creators through the core perks program and they've asked me to mention that a number of creators have been able to pay their bills buy their dream cars and quit their day jobs thanks to this program they've also asked me to tell you that they're currently hosting the game creator challenge with 13 000 in total prizes they're collaborating with various youtubers from the gaming community to lead a group of creators to complete in a mega game jam you can join a youtuber you want to support and be a part of their team and if you submit a game that you've made with core you could win up to a thousand dollars in prizes the deadline is december 15th and you can use the link in the description below to learn more about the event core is free to download free to play and free to create so use the link in the description below to sponsoring the downl now let's get started building our inventory system so i've got a little adventure game here that i've been working on and i'd like to add some gameplay relating to inventory items for instance our player character would like to send a letter to their pen pal however the postman is here and they haven't written the letter yet so they're going to need to wander around the apartment and gather the things they need to write their letter we're then going to have them combine the items together into a final letter that they can give to the postman this means then that the first thing we're going to want to do is create the concept of an item for our game let's create a new script and name it inventory item data at the top we'll change it from a monobehaviour to a scriptable object and create a create asset menu attribute to allow us to add new assets to the project easily let's first add an id string for our item so that we can reference it easily then let's add a field for the display name of our item and a sprite for our items icon which we're going to generate in a moment with the script set up we'll go ahead and create the assets in our project and hook up the relevant data now let's look at how we can generate icons for our items i've got a separate scene here which i'll be using as an editor only tool whenever i want to create icons for my items we basically need to write a script in here that will save the camera's output to an image in our project so first i'll drag one of my prefabs out into the scene and create a camera and then size up my shot [Music] next we'll create a script that can take a screenshot from the camera and save it to a folder in our project i want the icon to have a transparent background so i need to make sure i set the camera to depth only [Music] okay so now our camera can save its output to a file we're ready to process the rest if all of the items were the same size i'd probably spawn them into the scene automatically as part of a batch process however in this instance as all of the items are different sizes and there's just a few of them i'm going to manually place and frame each one of them to make sure that their icons all look good however i am going to have our processing script screenshot them and then assign them to our scriptable objects automatically in our screenshotting tool we'll create two lists one for our scene objects and the other for their data assets then upon calling the screenshot event we can iterate through each of the game objects take the photo and then link that photo to the corresponding object by default our import settings will cause the icons to be imported as textures we won't actually be able to find them as sprites so we need to somehow ensure that any image imported into this folder is created as a sprite thankfully we can use this handy script from the unity manual to define import settings based on the folder the assets are in once this script is added to an editor folder in our project all we need to do is create a preset in here then let's ensure that these are set to the ui type and that they're marked as transparent now all images in this folder will be imported based on this preset this means then that all we need to do is enter play mode and run the screenshot process [Music] and there we have it our data assets now have icons so our items are set up the next thing we need to do is build our actual inventory let's create a new script called inventory system this doesn't need to be a monobehaviour but for the purposes of this demo we're going to have our inventory system as an instanced unity object so that we can view it in the inspector and debug it you could however set this as just a normal class and construct it in some other kind of game manager elsewhere though anyway we're also going to create a new wrapper class called inventory item this will act as the actual instance version of our data and will be useful to determine additional metadata around our item that our system will want to use in here we'll create a reference to our data and create a constructor where we'll pass and set the data we'll also set a stack size variable and methods to add or remove the number of items in the stack which will come in handy later in our inventory system script we'll create a new list of inventory items and create methods to add and remove new items notice how i've also set up a dictionary in here this is so that i can grab the item stack more easily with the reference data when we're trying to add an item if it exists in our inventory already we'll add to the stack if it doesn't we'll create a new instance of the item and add it to our inventory when we're removing we're simply doing the inverse and if our stack size hits zero we'll remove the item from our inventory with the basic framework set up i think we're ready to scatter our items around the apartment i'll grab my objects and put them into place i've scattered an envelope paper and a pen to various interaction points in the apartment i've also decided that the players should need to find a number of stamps in order to be able to post their letter so i've scattered a few different sets of stamps around the place too now i need to add some logic to actually pick these items up and add them into our inventory so firstly i've created a little pickup script on our character which is taking advantage of the ik system we built previously when the pickup method is called i'm simply stepping up the weight on the ik and then stepping it back down doing it with ik means that i'm able to move the hand target around and the character will actually lean towards the object to pick it up let's create a new script that we can use to handle the pickup for each of our items when our player is in the vicinity of the item and uses the interact action they'll call pickup on the item which will then add the item to the inventory and remove the object from our scene i've also added a singleton reference to our inventory manager which right now is an instance so we'll need to add the imagery system script into our game somewhere to instance it again there's no real reason for this to be a monobehaviour but for debug purposes it's useful with that all together we should now be ready to pick up items if we play our game our character should now be able to stand near an object lean towards it and pick it up with our character now able to pick things up we should probably visualize our inventory on the screen somewhere there are a lot of different types of inventory menus and it largely depends on the style of game you're making so i'm not going to spend too long going over building the ui itself if you want some inspiration for different types of menus you can always check out references and see what best suits your game either way the underlying concepts to build a functional menu from our system here are the same so for now we'll just go for a standard inventory bar at the top of our screen here i built out a bar up here with a horizontal layout component and a content size fitter that way the inventory bar resizes as we add and remove items the item slot is a prefab that includes a child image as a placeholder for our item icon a label for the name of the item and a slot to show the current number in our inventory the slot script contains a set method which when called adds a reference to our item into the slot and then sets the icon as well as the stack text if there's more than one of our items in the inventory for the inventory ui itself i've added a manager script which listens for add or remove events from the inventory system and updates the slots each time it changes if we play the game and head to one of our items once we interact with it the item is removed from the scene and appears in our inventory bar awesome so that's the gist really but as regular viewers know there's always more we can do so what if we wanted to check if an item exists in our inventory before we can interact with it for instance the envelope model i'm using is already packed so why don't we force the player to have the paper pen and stamps ready before they can pick up the envelope we'll need to update our interaction script to be able to check for an inventory item in order to work let's create a new struct called item requirement and in here will allow for an inventory item data reference as well as a required value in our pickup script we'll add a list of these structs and we'll also add an option to remove the reference items from our inventory upon picking up the current one we'll then adjust our pickup method to check for multiple reference items before allowing us to pick up the item and if required tell our inventory to remove the reference items too then we'll just set things up in our inspector [Music] and we should be able to gather our items and create our finished letter ready to give to our mailman so that's the basics of building an inventory system as always i'm sure there's a lot more you can do with it but hopefully this has given you an idea of where to start putting something together and that's it for today's video just wanted to say thanks again to core for sponsoring the video be sure to follow the link in the description to get started using it for free and remember to check out the game creator challenge event if you've enjoyed the video be sure to hit the thumbs up button and let me know your thoughts down below if you're new to the channel please consider subscribing as you'll get to know when new videos go live alternatively if you'd like to see more from me first you can click the link on screen now to check out another video as always thank you very much for watching and i will see you again next time
Info
Channel: Game Dev Guide
Views: 150,448
Rating: undefined out of 5
Keywords: unity, learn, games, gamedev, how to, making games in unity, tutorial, unity tutorial, learning unity, unity3d tutorial, programming, making games
Id: SGz3sbZkfkg
Channel Id: undefined
Length: 11min 2sec (662 seconds)
Published: Tue Nov 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.