PROPER Way to Inventory in Unity with Scriptable Objects! #1 - Getting Started

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back everyone to this new inventory 2.0 sort of mini series it's just gonna be a two episode series um this is the first episode here where we set up the basic user interface and the uh functionality of the inventory um keep in mind this video is split into two parts the first part is setting up the basic items and the way the inventory works the basic functionality the second part of the um episode is adding this great user interface into it so um this great grid sort of standard using facebook basically every inventory system out there so that that's the second part and the reason i've done that is so if you just want to do the basic functionality just listen to the first half of the video if you want to add this using face as well onto it as a separate layer then you can follow it in with the rest of the episode um just saves people who just want the basic functionality and then you can add your own um interface onto it however you want as well so it's a very flexible very portable type of inventory system so that's it i'm gonna shut up now and carry on with the rest of the video enjoy guys okay so to get started i've loaded up a brand new unity 2021 project um it doesn't matter which preset you use we're going to be able to drag and drop this basically into any project you want the first thing i've done is just download the voxel pack it's still in the zip form if you want to download it you can get it from kenny assets uh you all know um that i love the any assets page they've got some really good quality assets there so highly recommend them uh make sure you don't donate or become a patreon as well to support them a link will be in the description for this asset pack as well so once you have that you're going to drag and drop the zip folder into unity and then you're going to right click and say show in explorer and we're going to extract this really quick so i'm just going to select it right click and say extract all hit enter that'll start extracting um there we go boom done now we would just have to click back into unity unity is gonna unpack everything and set up all the images and everything we need we can then get rid of the zip folder or what i suggest is for you to archive this so you don't have to download it next time um that said i'm just gonna delete it voxel pack we now have i don't need any of this there we go and i don't need any of this keep the license that's important uh png is the only thing i want to keep all these folders can go and that's it so that said characters can actually go as well particles can go and other can go i don't need them only thing i really need is the items for the purpose of this tutorial um tiles i will keep as actually i don't need the tiles that's fine so all i need is the items okay so now to set up the items you're gonna go ctrl a and we're just going to select all of them and mess around with some of their import settings just to set them up how we need them to be first thing we're going to do is make sure texture type is sprite or 2d ui pixels per unit should be 128 for this asset pack pixels per unit should be whatever size your sprites are so for example if they're 128 by 128 which these are um that you want pixels per unit to be 128 alpha should be transparent uh wrap mode can stay as clamp filter mode if you're using pixel art do point no filter otherwise you can do building or trilner i stick with building it for now compression can stay normal quality unless you're using pixlr then do not no compression okay with that all done press apply and that will now make them all look properly and normal so our next step is going to be setting up the main inventory script before we do that i'm going to create a item class so all an inventory script really is is a uh it's a list of item classes or items rather uh where each item class has its own data etc etc name you know image whatever you want to add to it um so our items are going to be scriptable objects so we can add as many as we want uh using the same class and i'm going to show you guys a few cool little methods and we're going to be messing around some abstract classes interfaces and whatnot so it is going to get a little bit confusing um but bear with me i'm going to explain everything as best as i can so first thing we're going to do is just create a new c-sharp script and i'm going to call this item class like so and then some of your items are going to be more specific than others for example you may have some consumable items some miscellaneous items um some tools etc whatever you may have so i'm going to create another c-sharp and call this full class now tool class is gonna inherit everything from an item class okay so it's gonna be a subclass of item class um and you'll see how that sort of works together later i don't know why that non-reference came up um another thing we're going to do is another type of item so for example if i want a miscellaneous item so i will call this miscellaneous class or misc class and then as well as that i want to create a consumable class if it lets me type unity is just trying to catch up consumable class okay so now these three are subclasses the miscellaneous tool class and consumable class they're all subclasses of item class um and they're all going to be scriptable objects so we're going to set that up in just a second we just need another script which we're going to use later and this is going to be our inventory uh manager and our inventory manager is going to manage our inventory so let's click double click on the item class to load that up in visual studio or whatever code editor you're using um once that's loaded we can get started so like i said everything is going to be a subclass of item plus which means our item class is simply an abstract class it's not a modern behavior it is literally just an abstract class cool if you don't know how abstract classes work basically they're really cool it lets you define everything without using it so you'll see how that sort of works later i'm going to open up all our scripts for now so if i load up the tool class next i don't know why they're not all loading together usually they do but obviously today just didn't want to work so our item class is going to have each item how can i explain this basically every single data which is shared across every item which is common so for example a string for the name every item has a name and every item has a sprite or an image right so that goes inside the item class so here i can have public string item name and then i can have a public sprite or item icon so that's all comment across the item classes so now in our inventory when we have a list of item classes they all have this i spotlight along here now the difference is if we want to ever get the item class and we need a function to do so so i can have a public item class and it's going to be called get item and this is simply not we so this is a uh sorry this is abstract so this this is where um abstract classes start to get a little bit confusing where we're defining a function here this is a function however we're not implementing the function's uh actual role so if the function is going to do something but we're not sure what it's going to do just yet so we're just going to define it we will use it later the way we're going to use it actually inside the subclasses and before we do that i just want to create another one and this is going to call be called tool class and this is going to be saying get tool and then obviously i'm going to do one for the misc class and it's going to say get miss and there we want one for consumable consumable class and this is going to say get consumable so these functions are all here we're going to define them um literally now so our tool class like i said it's going to inherit from item class but we don't need systems generic we don't need these functions and it's not going to be monobehaviour it's going to be your item class and this is going to give you some error saying it doesn't implement the inherited abstract member so basically it's saying we have these functions that we've defined here but we haven't actually given them a use here and that's saying that's a problem because we need to use them so i'm going to copy all these and paste them here and get item now remember tool class is of type item class so if i say return this it's actually gonna let me do that because or maybe not right it's not yeah these aren't abstract anymore my bad there you go um right yeah override there we go okay yes these have to be override um functions because they're overriding the definition um in the superclass or rather in this one so my bad just had a brain fart there okay so the reason return and this works even though this is of type tool class it's also of type item class because it inherits everything from item class and the reason that's important for our inventory is because then we can have a list of items but each item could be a tool it could be miscellaneous it could be consumable and the re the way we're going to determine which one it is is by determining um well you'll see now if i say return this for the tool class as well because again it's a tool class and an item class miscellaneous we can't say return this because this is not a miscellaneous class it's an item class or a tool class so there is no miscellaneous class which means we're just going to return null which means on our item class if we ever say item class dot get miscellaneous and it returns null that just means to our code that it's not a miscellaneous item now i can do the same for this one as well return now and that will be fine um now remember our tool class can have its own definitions so to make our inspector look a bit nicer i'm going to create a header here and call this item item yeah i will do that and then here i'm going to have another header and call this which means all our tool variables are going to go here so i might create a drop down for tool type so that's a public enum tool type and tool type could either be a weapon a pickaxe let's say hammer or axe so those are our tool types and then we can have a public tool type here and say tool type just like that and that means every tool has a tool type but every tool has a tool type item name and item icon um and that's that's basically it so i'm going to copy these override functions from our tool class and paste them into the miscellaneous class that'll give us an error at first but that's fine get rid of this system generic and then i can say item class because this is of type item class this here is there when we say return this for the get tool this now should say return null and then get miscellaneous we'll say return this just like that and then for example we'll have another header here and this can be miscellaneous i don't know how to spell that to be 100 honest and i'm not going to try because that's just embarrassing myself miscellaneous will have i don't know i guess it could have whatever data you would want to add to it for this purpose i'm probably not going to add anything because it's just miscellaneous uh consumable class is going to be different i'm going to paste these functions back into consumable class delete line 2 systems generic blah blah blah and this is of course item class as well now get miscellaneous is going to say no and then get consumable is going to return this um and then we're going to add a header here to say consumable and then i'll have a public floats i guess and this will just say um i don't know health added or something so this is the amount of health that is added to the play itself um we're not going to set this up this is just some placeholder data which you can mess around with if you wanted but the point is that under here you'd have data specific to your consumable class under here you would have data specific to miscellaneous class so you know whatever you want to add and they hear data specific to consumable class and then here you'd say data specific to full class and then here you'd say data shared across every item so you might even have an item id or something here you know so that's all fine now one thing is that our item class at the moment should be of type scriptable object so that we can actually duplicate this and create instances of it so what that means is now that since our itemclass is a typescriptable object and our tool class is of type item class it means tool class is also of type scriptable object a bit of class inception going on but believe me all you have to worry about is the way that basically everything inherits from its root class and the root class would be item class in terms of our scope but in the larger script of root class is actually scriptable object now why that's important is that because now we can add create asset menu to the tool class specifically and i can say file name equals new class for example and the menu name i can define a file path here by saying item slash tool um and i'll have to add another bracket there cool so i'm going to copy this and put it onto miscellaneous class as well and here i can say item miscellaneous and on the consumable class i can do the same and say item slash consumable and that's our class is coded so now if you head back into unity we can create a few test classes um so let's delete that that's fine clear our console now in here i'm going to create a new folder just to keep things a little bit clean call this items let's open up items and in here i'm going to create a new and you'll see we have this new selection here where we can go to item and here we can choose a either a consumable tool or miscellaneous i'm going to choose tool for now and you'll see interestingly it has item name and item icon however we didn't actually define that here we just have health where is it because we just have to type but because it inherits it we can see birthday and that's what makes this really really cool is because we can inherit many things so now i can give this an item name of let's say pickaxe actually let's just do sword or the cooler item icon you can choose here any sword you want to choose like so and the tool type is of course weapon right uh we'll name this sword as well that's not hasteful sword there we go and now if i want to create a let's say item miscellaneous class you'll see that this only has item name and item icon so i'll call this miscellaneous um i don't know let's call this call i guess always miscellaneous item i'll do the same down here and then item icon can be a cold sprite down here and then i'll create another create item and this will be a consumable and i'll just call this this will be an apple apple you see now we have health added so i can say it adds five points of health for example it's not going to for our use case but you know let's just say in theory it does and that's basically we have three items set up now now you're going to be like oh why do we go through all that effort of inheriting blah blah blah i'm going to show you right now so if i create an empty item i'm just going to call this um inventory sorry an empty game object in um in our scene called inventory reset the position what not and i'm going to drag and drop our inventory script onto it at the moment it doesn't do anything that's fine in our inventory manager now i'm going to show you guys why inheriting is so important for an inventory so let's say i wanted to hold different types of items in the same list right i want to hold tools and miscellaneous if i had a public list of type full class uh and just call this items right equals new list of type tool class now that's going to show up in our inventory that's going to be fine okay that's going to be perfectly fine down here that's going to show up a new list of items and we can add whatever items we want to it all right so i have three items i want to add let's lock the inspector here for now now i'm going to add a sword successful coal doesn't let me add coal why because call isn't a tool class now it'd be the same if i wanted to add miscellaneous class of type miscellaneous class it wouldn't let me add um sword to it now so it's going to disappear because it's going to say incompatible type or something now i can't add sword but i can add coal i still can't add apple there so the thing is obviously since these are different types of classes but they inherit item class if i have a list of item classes and item class there now i'll be able to add all three which are different types of classes they have different data they have you know completely different essentially but since they inherit item class i can add sword coal and apple into the same you know the same list and you'll see here it says tool class miscellaneous class consumable class and that is basically the functionality of having items and an inventory script that's pretty much it so now that's the that's basically our inventory set up we have a list of items perfect now we just want a remove function and a add function so i'm gonna have a public ads public void add and this will take in an item class item and all this will do is say items dot add item now remember since our item is a tool class miscellaneous class or consumable class i can literally say adds to anything so in the for example start function let's say public void start um here i will say add and then it will take in a new item so i can pass in an item here but i will do that let's say i create a public item class item to add and then here i'll just add an item to that okay so and this is just this is just for to show you guys how it will basically work so our item class this one this is the item we want to add this would be something like you picked up an item from the ground right so it could be what however you want to script this um i'm just putting in the start function for now but this would essentially be for example when you collide with the item drop then you would add the item to add and our add function is going to take the item which again could be miscellaneous tool consumer whatever it's going to add that to our list right now a remove function is the more complicated one um remove why does it do that there we go remove and this will take in an item class item as well now it's going to look through our list and remove the item we want to remove so let's have a public item class item to remove and this will be you know again scripted however you want to script it um so that your for example you want to drop an item onto the floor you choose that item from your inventory now your item class is your selected item and then you would say items to remove equals selected item and then remove item to remove so okay um and then and i'm gonna test this in the start function after we do the add bit so it's gonna say items dot remove and this takes in an item class item and i can just pass an item and that's basically it no need to overcomplicate it no need for anything else that's pretty much it so now we have two slots here and uh one thing i'm going to do is say the item i want to add is an apple right so i'm going to get rid of apple from my list there is no more apple in my list anymore and then i want to remove sword so what would should appear when i press start is that when i press play the apple should appear at the bottom of the list let's see what happens boom apple has appeared at the bottom of the list as a consumable class perfect now let's say i want to remove the item to remove so we said we'd want to remove the sword so in theory what should happen now is when i press play sword is deleted from that list and apple appears at the end of the list and let's see if that happens perfect no more sword and we have now an additional apple um it happens quite quickly obviously because it's our code works but if i press stop you'll see that sword comes back and apple is removed here we go perfect and that's literally it guys like that's the basics functionality of our inventory system already set up so like i said now i'm gonna go and set up the gui so thanks for watching this one guys i will catch you in the next video take care and peace [Music]
Info
Channel: ErenCode
Views: 24,081
Rating: undefined out of 5
Keywords: how to create an inventory system in unity, inventory system in unity, unity inventory system, unity inventory system tutorial, unity tutorial, make an inventory system in unity, how to create an inventory system, how to code an inventory system, how to use abstract classes, how to use scriptable objects in unity, creating an RPG inventory system, RPG inventory system in unity, full inventory system tutorial, scriptable objects unity, easy unity tutorial, unity scriptable object
Id: e9ATQaC-WMY
Channel Id: undefined
Length: 23min 3sec (1383 seconds)
Published: Fri Oct 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.