What are Scriptable Objects in Unity and How to use them | Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so first off what exactly are scriptable objects scriptable objects are essentially just data containers that follow a template an indication that using scriptable objects might be a good idea is if you have many objects in your projects which all have the same underlying structure for example in an inventory system every single inventory item needs a name description category and an icon [Music] this video is brought to you by Sky games check the link in the description for more info now to begin with in unity I've set up a simple UI that we will use to display information about our inventory items and then we can get started with the scriptable object so in my scripts folder I'll add another folder for my scriptable objects and another one in there for my inventory items then in here I'm going to make a new script and I'll call it in-game item and open it up in unity now in order to tell Unity that this is a scriptable object we need to inherit from scriptable object then the next thing we need is a way to create instances of the scriptable object and to do this we use the create asset menu attribute and we'll set the menu name parameter as in-game item and you can also declare a menu structure by adding forward slashes this create asset menu attribute allows us to create instances of our scriptable object in our assets folder by right clicking and following the menu structure we defined hopefully this will seem less confusing as as you watch the video but what is happening here is the c-sharp script that we created is acting as the template for the scriptable object then by creating an instance of the scriptable object we can begin populating its data so now to actually give our scriptable object data that we can fill out we need to give it some variables so thinking about in-game items each item would have a name description a Sprite Which acts as an icon as well as items may have stats or settings so I'll create an enum for item category and one to specify where this item will be attached to the player's body and how many hands it might require for the player to carry this item now in unity I'll rename our test scriptable object to sniper and now you can see all the data I can populate this with so I'll choose a name then in order to select an icon Sprite I'll have to go to different directory so when you're referencing files in another folder you can right click and open properties to have a pop-out window that stays while you're browsing for sprite then once you've filled out all the data for one in-game item you can go and create as many of those as you want so I'll create one more for a helmet now we need a way to take that data from our in-game items and display it through our UI so I'm going to make a new folder with a script called inventory item viewer in there we will create two methods that will be called by our next and previous buttons then we'll store a reference to the index of the current item we are displaying as well as an array of in-game items which will be the items we want to display now when we press the next button we'll increase our index and make sure it Loops by checking out if our index is greater than or equal to the length of the array and set it equal to zero after that we'll call display current item which is a method we'll create in a moment then when we press the previous button we want to decrease the current index and check if it is less than zero and then set it to the array's length minus 1. now when we create a display current item method we'll need some references to the UI and I'll just add some headers to Newton Upper bit then we'll grab references to the title text description text and the image which will display the items icon and now all we need to do in our display function is set our title text equal to our items at current index dot item name description.txt is equal to our items at current index.description and I'm just going to format this to display the items category on a new line after the description as well then lastly we need to set the icon dot Sprite is equal to items at current index.icon now we can save the script and add it to your inventory item viewer gamma object make sure that the UI references are correct and don't forget to assign the inventory item viewer dot on next button pressed and on previous button pressed to your actual UI buttons and now finally you can simply drag and drop your in-game items into the array or select them in the inspector oh yes and there's one more thing we need to do in our inventory viewer script in the void start we'll want to make sure that the current index is zero then if items is not equal to null and items the length is greater than zero we will call display current item and that's it if you save and run the game you should see the data from your scriptable objects displayed on the screen and we can scroll through our in-game items with the next and previous buttons and if I go and create some more in-game items and add them to the list you can see they're automatically are displayed through our UI now sometimes you simply want to access all instances of your scriptable object without having to drag and drop every single one into a list so I'll show you a handy little trick we can use to do this firstly you'll need to create a resources folder that will contain all your in-game items and note that the folder has to be named exactly the same as I have named it otherwise this won't work then inside the resources folder I'll create another one and call it in-game items and move all my in-game items into that folder now in our in-game item class We'll add a public static class which I'll call database and in the static database class I'll have a static list of in-game items called in-game items and will default that to null then we'll add a public static list of in-game items called get in-game items where we'll return in-game items if it isn't now and if it is now we'll set in-game items equal to resources.load all and will pass in in-game item as the type then for the parameter I'll just copy the name of the folder which contains all the in-game items in the resources folder and what this line of code does is it retrieves all in-game items that are stored in a folder called in-game items within any resources folder in your whole project then we'll just convert the result to a list and return it and the last database method I'll show you is a public static in-game item called get item by name and takes a string parameter and in here we'll just return get in-game items dot find where item dot item name is equal to the item name parameter for those of you unfamiliar with this notation this line of code is equivalent of doing a for each Loop and returning the item where its item name equals the item name that we received as a parameter and now in our inventory item viewer in void start we can access our in-game item database by saying items is equal to in-game item dot database dot get in-game items and as simple as that we can now add new scriptable object instances and they will all be automatically added to our list when we hit play in the editor I hope you learned something useful in this video and if you did a like or subscribe would be much appreciated and don't forget to go check out our website at skygames.com
Info
Channel: Skye Games
Views: 11,138
Rating: undefined out of 5
Keywords: Unity Tutorial, Scriptable Objects Unity, scriptable objects unity inventory, scriptable objects tuturial, scriptable objects unity tutorial, unity scriptable objects, unity scriptable objects tutorial
Id: czCYSmQHiDg
Channel Id: undefined
Length: 7min 32sec (452 seconds)
Published: Tue Aug 15 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.