Unity3D - Scriptable Object Inventory System | Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Dude, awesome tutorial. Very professional editting and teaching. watched 5 minutes and had stop and like. I hope you will continue great work. Also nice choice of subject, as a 2 years unity developer, i rarely use scriptable objects.

👍︎︎ 1 👤︎︎ u/art-vandelayy 📅︎︎ Sep 07 2019 🗫︎ replies
Captions
hello and welcome back to another episode of coding with unity today we'll be making a scriptable object based inventory system similar to the one you see here our items will be made with scriptable objects and once our items are created and added to our player will also store those items on another scriptable object but we're not just going to stop here we're gonna keep going with this system until we have a banking system an equipment system and a fully working inventory system that's more than just displaying pictures with a number on it so let's go ahead and get started with making this so go ahead and open up your unity hub and let's make a new project I'm gonna call my project YouTube inventory but feel free to call yours whatever you want maybe my RPG or whatever you want let's go ahead and create our project it can be 2d or 3d this system will work in either game environment but by the end of my series our system that I'm creating is going to be working specifically with a 3d system so that we can have a 3d model that has items equipped to it that bins with your characters animations so let's go ahead and create this YouTube inventory project once your project has created let's go ahead and create a new folder for holding some scripts since our inventory is going to be using scriptable objects for creating the items and the inventory let's go ahead and create a folder called scriptable objects then inside of this folder let's make another folder called items and inside of our items folder let's create another folder and call it scripts we're just trying to keep everything inside of our editor organized so when our project gets more complicated it's not hard to find what you're looking for to edit it later down the road so inside our scripts folder let's create a script called item object then we'll go ahead and open this item object script up and inside of here let's get rid of this start and update functions and replace monobehaviour with scriptable object because this is going to be the base class for creating our items we're gonna make this an abstract class because this won't be the class that we will actually be using to create the objects but it's going to be a base class that we can extend the other items from so we can have more classes like food object equipment object or any other item type that you can think of the reason we put object on the end of the class name is just to keep it organized and more easily to identify that this class is a scriptable object and not a monobehaviour or a base C sharp class now inside of here let's make a game object variable called a prefab that's going to hold the display for the item once we add it to the inventory then we're gonna make another variable that's going to hold the type of the item but before we can do that we need to make an applicant that has the different type of items inside of it so we'll save public enum item type and inside of here we'll save food equipment and default then back inside our item object class we'll save public item type type so we can store what type our item is then let's make a string to hold the description of the item and to make this more easily displayed in our editor because if we create this now it's going to be a single line string for the description let's make it say text area and give it a size of 15 and 20 this will make it easier to be able to read the description inside the unity editor now that we have our public abstract item object class created let's create another class for the type of item that we want to make the first item can be a default object so let's create another c-sharp script we'll call it default object and this is going to extend our item object class we can remove our start and update and then above where it says public class we'll type some brackets and type create a set menu make a parenthesis and we'll name our file by saying file name equals we'll say new default object and then we'll give it a menu name of inventory system slash items slash default so that we can right-click go to inventory system items and default to create a new copy of our default item now the only thing that we're gonna type in here is a public void awake so that every time that we create a new default object we can automatically set some variables and the only variable that we want to set is type equals item type dot default that way we don't have to manually set our item type every time we create a scriptable object let's go back into the unity editor to see how this works so let's go out of our scripts folder and into the items folder right click create inventory system items default you can see we have a new default object our type is default we have no prefab and our description text box area is big enough to be able to read a reasonably long description so let's name our default object will name it bones now let's give our bones a description we'll say the leftovers of those you've slain excellent we've created our first item you could easily just right-click create inventory system items default and make as many default items as you want but we'll just leave ours at bones for now so let's go back into our scripts folder and we'll create another script and call it food object or it could be like potions object or whatever you want some type of object for healing your character so let's create that and open it up get rid of our start and update replace monobehaviour with item object now we're gonna make an awake function again for setting the type we'll say type equals item type dot food then let's also create a variable on here for holding the amount of help that we want to restore when we eat the food so we'll say public int restore health value and now we need to make another create a set menu line above our public class food object for actually being able to create the food object so let's go back into our default object copy this and paste it inside of here now we can change new default object to say new food object and instead of item / default let's say item / food go back into unity and we can see if we can create our new food object so let's right click create inventory system items you'll see it says default and food let's click on food and let's name our food object we'll call it a monkfish you can see it automatically set type to be food we can set a prefab for the object and let's go ahead and give it a scription we can also go ahead and set a restore health value let's restore our health by 16 so excellent we have a default bones object and a food monkfish object and our monkfish has a restore health value that is separate from our default object that does not have that value we can go ahead and create as many different types of food as we want give it its own unique name its own unique description and a unique value for how much health it should restore but we'll leave ours at monkfish for now and move on to making another type of object the last object we'll make is an equipment object so let's go to create C sharp script and call it equipment object then we'll open that script up replace monobehaviour with item object get rid of the start and update functions we can copy our create asset menu and paste it into our equipment object change food to say equipment and food here to also say equipment now inside of our equipment object class we can say public float attack bonus and public float defense bonus you can add any other variables that you think your game might require but we'll leave it here for now then let's read our awake function for setting the type automatically so we don't have to do it manually we'll say type equals item type die equipment we can go back into unity let it compile and create some equipment items so gonna create inventory system items you'll see we now have an equipment option let's click that it automatically set the type to equipment let's name it sword give it a description let's give it an attack bonus of 10 and since it's a sword let's not give it a defense bonus we'd also go ahead and create a few more equipment items let's create a sword 1 a sword 2 and a shield let's change the description for our shield remove its attack bonus and give it a defense bonus then we can go ahead and go to our sword 1 change its attack value go to our sword 2 and change its attack value now we have three different types of swords a shield a monk fish and some bones excellent we have quite a few different types of items but how do we store them into an inventory system 4 we can display later within our game well that's what we're going to do next let's go back to our scriptable objects folder and create another folder and call it inventory open up the inventory folder and inside of here create another folder and call it scripts and in the scripts folder let's create a new C sharp script and call it inventory object we can go ahead and open our inventory object script up after it compiles and the first thing we'll do is replace monobehaviour with scriptable object because our inventory is also going to be a scriptable object remove the start and update functions then let's make a public list for storing all of the items that we want to add to our inventory system so let's say public lists the thing that our list is gonna hold is going to be item objects and we'll call it container because it's a container that holds our items then we'll say new list item object excellent but not really one problem with this is we're just storing the type of item but we actually need to store the type of item and the amount of items that is held in that slot of our inventory so let's create a class called inventory slots and instead of holding items our inventories container will hold inventory slots and those inventory slots will contain the item in the amount of items you have in that slide so we can just do this inside of the same class instead of creating a new class so we'll say public class inventory slot an open and closed bracket and then above your public class you're gonna also want to say system serializable so that when we add this class to an object within unity it will actually serialize and show up in the editor so inside of our inventory slot class let's create a public item object and call it item this is the item that's stored in that inventory slot now let's make an int for holding the amount of that item that's stored in that slot excellent now let's make a constructor for setting some values when the inventory slot is created so we'll say public inventory slot and it's gonna ask for an item object we'll call it underscore item and it'll ask for an amount so we'll say int underscore amount now inside of this constructor will just say item equals underscore item and then amount equals underscore amount now let's create a function for adding the amount to our inventory slot so we'll say public void add amount int value then we can just say amount plus or equal to value now let's go back up to our inventory object class change where it says item object to say inventory slot and now let's make a function for adding items to our inventory so we'll say public void add item and our voids going to ask for the item that we want to add so we'll say item object underscore item and then the amount of that item that we want to add so we'll say int amount now we need to first find out if we have that item within our inventory or not we may switch this to using a dictionary later but for now we'll just use a list so the first thing we need to do is check if we have that item within our inventory so we'll say Bull has item equals false because we're gonna first assume that we do not have that item now we need to loop through all the items in our inventory and see if the item that we're trying to add is already in the inventory so we'll say int I equals 0 I less than container count I plus plus now we'll say if container I dot item is equal equal to underscore item has item is equal to true and we can go ahead and break because we don't need to continue going through our for loop now since we do have that item let's add two the amount of that item that we have by sayin container I dot add a mount underscore amount using the function that we created earlier so if we go through this for loop and has item is never set to true will say if explanation mark has item meaning has item is equal to false container dot add new inventory slot and we'll pass in the item in the amount excellent we have our inventory object completely set up the only thing we have left to do is the create asset menu part for creating new inventories for our game so we'll say create asset menu file name equal new inventory and then menu name is equal to inventory system inventory excellent we can go back into unity and create a new inventory inside of our inventory folder so go into your inventory folder go to create inventory system now you'll see underneath your item selection you'll have an inventory selection create a new inventory and we'll call it player inventory the reason we're storing the inventory and a scriptable object and not directly onto a player within unity is because we can create multiple inventories and say our second inventory is still the players inventory but we want him to have this inventory during the tutorial well we can have a specific scriptable object with the items on it that we want to be accessible during the tutorial but we don't actually want to give to the player when the game starts so we can just switch between the player tutorial inventory and the player inventory this could also be used in lots of other places within your game you could even use these inventories for shops or enemies or whatever think you need that needs to hold items within your game so now that we have our inventory and our items set up let's create a character that actually holds an inventory on him so let's create an empty we'll call him player and underneath this player let's give him a graphic for now he'll just be a cube now on the player object let's add a new script to it and call the script player now we can open the player script up and inside of here let's say public inventory object inventory excellent now our player has an inventory on him all we need to do is add and remove items to it and create a way to display that inventory within the game so the way that we're gonna add items to it for this demonstration is going to be similar to what you saw in the beginning of the video we're gonna create a bunch of objects within the world then you're each gonna hold their own specific item that'll be added to the inventory when you interact with them so let's create a new object will call this item and then underneath the item create a graphic we'll use a spear move it off your player and now on the parent object let's create a new class and call it item now this class is to hold the item that this object is representing so let's open it up and inside of here we can say public item object item now that we have our item class setup with a public item object item for storing the item that we want our game object to be representing let's create a way to interact with this object the way we're going to do that is by going to our player and removing the Box Collider from the child object we're gonna add it to the parent object and let's go ahead and add a rigidbody to it also and let's turn gravity off so our player doesn't start falling right when we click on play then on our item go to your sphere remove the sphere Collider and add it to the parent item now let's go back into our IDE go to your player class and we can get rid of the start and update functions but let's add a new function and call it ontriggerenter because we need a function that's going to fire every time we interact with an item within our world so we'll say public void ontriggerenter then we'll save our item is equal to other docket component item now we'll say if item meaning it was able to find an item let's say inventory died add item item dot item and one then we need to destroy the game object that we just interacted with we'll do that by saying destroy other dot game object so that we only add one of the object that's within the world so let's go back into our unity editor go to your item and one thing we need to change is make it a trigger enter since we're using on trigger enter let's click on our player inventory so we can see if it's getting items added to it or not now before we click on play we need to set up the item on our item script will use bones for this one we also need to go to our player and set up the inventory so set player inventory to your players inventory variable then let's click on play move our square into the sphere see if the sphere gets destroyed and it does let's go into our project click on the player inventory and you'll see that we have a bones added to our inventory go ahead and click out of clay and we'll do locate the spawns items two more times and see if we're able to add three bones to our inventory click play click on your player go over the items we deleted all three of them and your player inventory size you have four bones added the reason is is because when we leave play our inventory still stays the same and three plus the one that we got from the previous play equals four the way we're gonna fix this is by going back into our IDE and inside of our play your class we're gonna make another function and say private void on application quit and when we do quit the application will say inventory da container clear which will clear all the items within our inventory so let's just manually set the size back to zero this time click play go to your player move your player over the three items you'll see you have three bones in your inventory and when you leave play your container size is set back to zero excellent everything's working so far the last thing we need to do is find out a way to actually display our inventory so let's quickly do that right click go to you I make a canvas and underneath this canvas let's make a panel change your unity editor to 2d so it's easier to work on the UIs then you can double click on the panel to automatically get it to the size you want and make sure you're on the rect tool so it's easier to edit your UIs let's change our panel size we'll make an inventory that's about this size and we can rename our panel to say inventory screen now we need to make the prefabs that we're going to add to the items that are going to be displayed on our inventory when our inventory is updated with the new items so let's create a new folder and call it prefabs let's also create another folder and call it graphics and inside of this folder you can drag the graphics that you want to use for your inventory but for my project I'm just going to be using a square so inside your prefabs folder we need to create some prefabs so go to your inventory screen create a UI let's resize our image to make it look more like the size that are single inventory items should be I think I'll use a size about like that let's move it to the position that we want our inventory to be at I use negative 100 and then 183 now underneath our image let's create a tex-mex to display the amount of that item that you own inside of your inventory we can change our text size to the size of our square and then use auto size for the font reduce your minimum size option to one I suppose or whatever size you want to use then change new text to say zero then let's go up to our image put the square or whatever image you're using for the item display that you're creating into your source image spot then I'm going to change my color to red so the zero is easier to see and since we're using tex-mex pro we can add an outline to our text so let's add a black outline to our text to make it easier to read I'm gonna Center my text out better and the way we're gonna do that is not by adjusting adjusting the position but we're actually gonna go to here and just Center it all right so I have my item set up and all it is is an image with a source image in a color and then a text message and center it out now that I have my item set up I'm going to go ahead and drag and drop it into my prefabs folder and then we can delete this out of our inventory screen I'm going to make a couple different variants of this using the prefab variant system so that we have different variants of this item for the different items we have in our game so we have one two three four five six items so I'll make six five variants and one original now that we have our six items set up I'll go ahead and change the colors on each of my items so now we have our six different items set up for our six different items we can go into our items folder and set up the prefabs for each of our items and now that we have all our prefabs set up on each of our items we can go to our inventory screen panel and create a script for displaying and updating our inventory so let's create a script called display inventory and now on this script let's make some variables the first variable is going to hold the inventory that we want to display so we'll say public inventory object inventory another useful thing of using a scriptable object architecture is we don't have to link to our player to find as inventory we can just link directly to his inventory inside of our editor now let's make a public int that's gonna hold the X space between our items so we'll say X space between item then we'll make another one for the number of columns that we want our inventory to have and one more variable for the Y space between items Notts create a dictionary for the items displayed will say dictionary the key of our dictionary is going to be the inventory slot and the value is going to be a game object we'll call this items displayed new dictionary then onstart will say create display and an update will say update display now we need to make these two functions first let's make the create display function so we'll say public void create display and inside of here we need to loop through all the items in our inventory so we'll say for int I equals 0 I is less than inventory diet container dot count I plus plus then we'll save our obj is equal to instantiate inventory dot container i dot item dot prefab then we'll put vector 3.0 quaternion dot identify and transform this is set our position rotation and our parent now we'll say obj dot get component rect transform that local position is equal to now we need to get the actual position that we want to place the item at once it's in stanch eiated the reason we didn't do it here is because this sets the world position and we need to set the local position so let's actually create a function for getting this position since we're gonna have to write it twice and it'll make things cleaner so we'll say public void give position and this position is gonna need a value for which position in the inventory is the item so we'll just say int I since the value that we'll be using will be equal to the I inside of our for loop so we'll say and this isn't going to be a void it's actually going to be returning a vector3 so we'll say public vector3 get position and now inside of here we'll say return new vector3 and now let's write a quick line of code that will place our objects within a grid so we'll say x space between items times parentheses I modulo number of columns comma parentheses negative Y space between items times I divided by number of columns then a comma 0 F then we can close it off with another parenthesis now right here we'll say give position I now we need to get the object in the child which is the text message I and set that text to the amount of that item we have so we'll say object docket component and children and we need to add a using for the text message is using TM Pro now we can go back down and say object I get component and children the component that we want is the text message EUI then we'll say dot text is equal to inventory dot container I got amount then we'll put a two string and 0 this will format it with commas so that it looks better with it and when you get to above 1000 of that item will create the update display in a minute let's go into our game we'll add some items to our inventory to start with and see if this actually creates and adds items to our game so let's make one we'll give it an item of monkfish amount of 10 let's make another one but this one's not going to be a monkfish it'll be a sword click on play and let's see what happens we forgot to set our inventory inside our display inventory so set that let's also go ahead and set up some X space between the items which I'll use 35 6 and 35 let's click on play and see if this is working you'll see that we got our image variant and our image variant too and they both display 10 if we go to our player inventory we have 10 monkfish and 10 swords this seems to be working but let's make it to where it starts placing them here and works down to here instead of guarding here the way we'll do that is by going back into our code we can create two more variables we'll say public int X start and public int Y start then going down to our get position at the very beginning of our new vector3 let's say X start + and then put parentheses around this and then after the comma we'll say Y start + this excellent let's go back into unity click on our inventory screen I'm going to try we can actually just go into our prefabs add our image and we can see we need negative 100 183 let's see if those values will work so we'll say negative 100 183 our inventory is clearing itself because of that line of code that we added so let's read our items to our inventory now we have our six items set up let's click on play excellent we have our six items displayed in our inventory you can see our number of columns is set to six so if we leave the game reset up our inventory again maybe not the best line of code to have added while testing this I'm going to leave the item amount to zero since we know that's working let's go to our inventory screen and change the number of columns to 4 to make sure the column part is working excellent you can see after we have 4 it goes down to the next column you can feel free to change these values to make the items display better in your inventory but obviously that how it is in my project for now let's go to our player leave 2d find our player and see if picking up these items adds more items to our inventory which it did but since we don't have the update display our inventory didn't update so let's go back into our ID and add the last function we need which is the update display so we'll save public void update display and inside of here we'll have a for loop over all of our items and then we'll say if items displayed diet contains key inventory dot container I meaning is that item already in our inventory if it is already in our inventory will say items displayed inventory dot container I died get component in children hex mash Pro you GUI dot text is equal to inventory container amount then we'll add the dot to string and 0 now if that item isn't already inside of our inventory we'll say else and we need to create the object which is exactly what we did here so we can just copy and paste this into here and then one last thing we need to do is actually add the item that's getting created to our items display dictionary so we need to say items displayed dot add inventory dot container I comma obj which is the object we just created and we need to also do this in the create display spot excellent now that we have the update and the create display made let's go back into our project our inventory has nothing in it we have three items in the world which are all bones so let's create a couple more items so it's not just bones all right now I have a bunch of items set up we can try picking these up and see if our inventory updates and displays correctly so let's click on our player and drag them over all of the objects excellent everything seems to be working so this is a pretty basic way of displaying the items so in the next video we're gonna create a way to actually interact with the items in your inventory and start to actually be able to use it for a fully developed game and not just a basic intro to an inventory system so that's it for this video if you enjoyed the video give it a thumbs up and if you have any feedback suggestions or just in general comments please feel free to leave the comments in the comment section below but until next time have a wonderful day and stay coding
Info
Channel: Coding With Unity
Views: 162,267
Rating: undefined out of 5
Keywords: unity3d, unity2d, unity3d inventory, unity2d inventoy, unity, inventory, make an inventory with unity3d, make an inventory with unity, unity3d tutorial, coding with unity, unity inventory system, unity inventory tutorial, inventory tutorial
Id: _IqTeruf3-s
Channel Id: undefined
Length: 31min 31sec (1891 seconds)
Published: Fri Sep 06 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.