Inventory System | Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
inventories are crucial in almost every game genre being used for a variety of purposes like item storage or even equipment I'll be showing a simple and easy approach similar to Minecraft's inventory with a real-time view of your player equipment slots and an easy item system let's jump straight into it the first thing I show you is the setup of a project I placed a player which doesn't have any controllers the only thing it does have is a capture module and a camera the camera is used to create the real-time view of our player which is very simple to set up I've just created a new layer name player applied it to the captual module and set the culling mask of our camera to the layer this just means it will only show our player layer from now on going down to the output section now I've set up its output texture to a newly created render texture which can be done easily created in the project view like so now onto the actual interface and how this render texture is applied on a new canvas I put together a quick interface which holds inventory slots prefabs within them under the headings Hotbar inventory and equipment slots inventory slots will hold a script later on but just for now make sure it has a grid layout grip with an appropriate sizing this ensures when an item is parented to a slot it sticks to the center of a slot the only difference between the slots is the equipment slots I've used the same prefab again but edited slightly good idea here is to create a prefab variant of these equipment slots or do what I've just done these slots will hold a placeholder image representing its equipment slot within layout element ignoring the layout enabled and disabling the raycast target option on the image component which just ensures it doesn't get in the way of our clicks I've created now a simple button which will spawn random items for us and also an empty transform named draggables for lack of a better name it just holds the item we're currently carrying and lastly I've just set up a simple player view with a black background and the render image within it make sure the image is of type raw image so we can import the player render texture within it the scripting part of this system is quite simple but it's split in a few different parts each are dependent on each other so I show you one at a time the first being a script name item this is just a scriptable object which we can create by selecting the menu options I've defined here the only data this item holds is a Sprite item tag and Equipment prefab I've used an enum to Define different slot types it's just a definite mission of which slots it may sit in whether it be head feet legs or chest for now I've just created a few test items which hold a few different Sprites and Equipment types now onto a script name inventory item I've Incorporated two new namespaces named unity engine.ui and Dot Event Systems this will allow us to use the interface eye pointer click Handler which basically calls a method when we click this object I've included references to the image of this component and canvas grip which serves as a way to block raycast on the object while we are carrying it a reference to my item type and current active inventory slot I've simply just set the image and canvas script components in the awake now on to initialize this will be called when we spawn a new inventory item in our inventory which we will just ask for an input of item type and the inventory slot it'll move into assign our active slot to the news inventory slot and set its active item to this object I'll then set the Sprite of this object to the current item Sprite and lastly to finish it off we'll finally call our pointer click method which must be named exactly like so and include a pointer event data this will just check if we've clicked it with our left Mouse button and set this item to be carried which will make sense in a moment in the other script is our inventory slot this will serve the purpose of dropping items into when carrying and calling certain functionality such as equipping armor like before we need to include the unityengine.event systems namespace and the eye pointer click Handler interface if any references we will need is one to the current inventory item we're holding and the slots tag which we just default at none we'll follow the same structure for our on-poin to click method checking for the left Mouse button to be clicked then we'll run a few checks before we set the slots item firstly checking if we're currently carrying an item then if we're dropping inside of a slot that's an equipment slot check if the item tag and the slot tag are equal then call our set item method taking in the carried item then we're setting the item as our current item as this can only be called when carrying an item in this method we just stop carrying an item empty its previous slot and set the slot as its current parent also make sure to block Ray costs again which just means it can be interacted with once again then a final check if it's been dropped into equipment slot calling the equipped equipment method with that done the final script will hold together and tie most of his functions I've named it inventory but it's probably more suited for an inventory manager but it doesn't really matter the only namespace we'll need is a unityengine.ui and I've included a few references the first being a Singleton pattern to call this script function elsewhere then a reference to our current carried item an array of our inventory slots the draggable transform which again is just an empty transform which holds dragged items another array of all the items within our game and a nice give item button reference I've just added the spawn inventory item twists on click action this script will just pick a random item from our item list if an item isn't included then find an empty slot to spawn the inventory item prefab within it this method can be record when you pick up loot from the ground or whatever it may be within the update method we'll just make sure to make the carried item follow our cursor position if it exists onto the set carried item this is just being called when we click on an inventory item which will firstly just check if we're currently holding an item and switch positions with it as well as checking if the item is within an equipment slot unequipping it appropriately then moving the new carried item to our empty draggables transform making sure to set block raycasts to false to ensure it is interactable finally the last method is to equip our equipment ultimately I've left out the actual equipment spawning but I've just left an example of how you might achieve this I've done this because ideally you're going to want to use a proper player model not a beam so if you wanted all instance shared it on the player you want it to spawn on the player's bones such as head legs or whatever it may be so I've just logged the actions as shown I've also set the reference of inventory item to null to set a way to unequip the item I ideally if you are going to use visible objects you'd have references which can be destroyed and set as when instantiated perhaps in a later video I might revisit this and add more functionality to the system but for now it serves as purpose it can always be added on and improved by yourself before this done this scripts will now be set up in the following way inventory will be placed on the inventory object in our canvas and all references applied the inventory item prefab is just a simple prefab with an image on it and inventory item script placed the inventory slots are the same as before with the inventory slot script attached make sure to assign the slot tag for the equipment slots and the rest are none and that's all for the tutorial if you have any suggestions please leave them in the comments below and as always if you'd like to download the project the GitHub link is in the description also all art using this project has been linked in the description below too if you enjoyed the video or found it helpful Please Subscribe and like the video you can always find me on my Discord server and other social media to support my content
Info
Channel: Pogle
Views: 11,550
Rating: undefined out of 5
Keywords: Unity, how to make game, make a game, unity how to, unity tutorial, unity c#, first person, third person, unity inventory, inventory system, how to make inventory unity, backpack, items, drag and drop, make rpg, rpg game, make rpg game, inventories, slot, weapon and armour, unity breath of the wild, unity minecraft dungeons, unity minecraft, unity rust, drag and drop unity, idrophandler, idraghandler, ipointerclick, iondraghandler
Id: -IPjFSWeyrg
Channel Id: undefined
Length: 7min 19sec (439 seconds)
Published: Tue Jul 18 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.