Make your Games Designer Friendly! (Scriptable Objects)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to look at scriptable objects in unity these are objects you can create in your project files that can be used to store any kind of data and make your game more designer friendly extremely useful for storing level data enemy data weapon data and just about anything we're going to learn how they work and then later we're going to apply them to systems we made previously like the crafting system let's begin [Music] hello and welcome i'm your code monkey and this channel is all about helping you learn how to make your own games with in-depth tutorials made by a professional indie game developer so if you find the video helpful consider subscribing okay so scriptable objects are an excellent feature that makes your game much more designer friendly essentially they are simply containers of data you define the various fields you want your object to have then you create the instances of that object in your project files and set those fields to whatever you want the key point is you define and store all that data in discrete objects in your project files rather than having all of that written directly in the code if you want to make your game more designer friendly then scriptable objects are the way to go okay so here we're going to learn the basics on how to define and create scriptable objects and then later on we're going to take what we learned here and apply it to the crafting system that we made previously so the items will be defined as scriptable objects as well as the crafting recipes this video is made possible thanks to these awesome supporters go to patreon.com unitycodemonkey to get some perks and help keep the videos free for everyone alright so in here let's make a very simple scriptable object now down here in our project files let's create a new c-sharp script call this our test scriptable object and let's open it okay now here we have our default unity code now the first thing we do is let's get rid of all these methods and in here instead of extending mono behavior we're going to extend scriptable object and just like this we already have a class that we can use as a scriptable object now like i said they are containers of data and how you define whatever that you want to store is by defining some fields so in here let's define a public string call it my string okay now we have a scriptable object with a simple string field now let's see how we can create an instance of our scriptable object now to do that we go up here to our class definition and we're going to add the attribute create asset menu then we have three optional parameters so first of all let's define the file name so this is going to be the default file name whenever we create an object so in this case let's just call it the same name so test scriptable object then we have the menu name so this is where it will show up on the create asset menu so in this case let's create a scriptable objects folder so you create a folder by simply putting a forward slash at the end and then let's call its test scriptable object and lastly if you want you can add the order attribute or it doesn't matter so let's just ignore that one alright so that's it over here we have the most basic scriptable object definition possible we just extend scriptable object we define our fields and we add the create asset menu attribute and now if we go back into the editor now in here on our assets folder i can right click go into create and yep over here we see scriptable objects our very nice folder and inside we can create a test scriptable object so just click on it and yep it starts off with our default name and for now yep just leave it named like that all right so here we have our scriptable object and we can see all the fields that we defined so in this case you see simple string so in here let's just write some text all right there it is and now this scriptable object has been saved with this data okay now let's see how we can use this all right so in here let's create a new c sharp script call this just testing let's create an object to run it and drag the script okay now here let's start off by adding a field for our scriptable object type so we're going to make it a serialized field so we can set it in the editor let's make it a private of type test scriptable object and then on the private void start let's simply do a debug log on the test scriptable object and we access my string all right so there it is very simple now back in the editor yep over here we have a field of type test scriptable object so we can drag our specific scriptable object yep and let's try and yep over here we can see our very nice message so we access the scriptable object and grab the data from it so now to see how this is useful we can duplicate this script more object so we make a second scriptable object and over here we give it a different string all right and now go back into our testing script and then just use this scriptable object instead and if we run if there you go now we have the different message so we have two different scriptable objects each with their own instance of data and then we have our script which works with that scriptable object type all right awesome so here we can see the most basic usage of scriptable objects we define our object with whatever fields we want so here for example we have a simple string but we could add for example let's say a public end for an end field then we can also for example add a sprite array so we can add whatever films we want to store whatever that we require and if we look in the editor and select our scriptable object yep there you go here we have all of our fields and we can set them to anything so for example on this one i can set two elements on this array and then on this second one i can select five elements so yep there you go each of them has the same format but different data so we can create as many instances of this type of object as we want in order to store whatever that we need so just like this you can already imagine the possibilities so for example if you have an item you would have a string for the item name then you would have a sprite film for the visual and so on another example is if you had a grid system like the one we made previously you could use a scriptable object to define the size of the grid and all of its attributes so you can see how they are extremely versatile now one key thing about scriptable objects is the difference between how data persists in the editor or in a build while you're running in the editor any changes you make to descriptable object won't be saved on the actual asset so if you're running the game and you have a scriptable object for the player max health and you change that the new value won't be saved to the asset and won't be used the next time that you hit play however when playing on a build the changes are not persistent so if you modify the scriptable object while playing the build and close and reopen it again it will go back to the original value so whatever value you set on the scriptable object when you made the build will always be the starting value when you actually open the game and since you can edit them and save the changes while working in the editor another usage for scriptable object is for building editor tools now another important thing about scriptable objects is related to the type of object they are scriptable objects work as references so over here we have our testing script and we have a field for our scriptable object so when we set it over here in the editor what it gets is a reference to descriptable object instead of a copy of that data so this has several implications now it means that these scriptable objects can help you reduce the memory footprint of your game so for example if you have a prefab for each enemy and they contain lots of data in their weapon if you instantiate that prefab it will constantly duplicate the data for that weapon however if that weapon data is stored as a scriptable object then in terms of memory when you instantiate an enemy it will simply have a reference to the scriptable object so it ends up with no duplicate in memory when you have a game with lots of enemies and memory issues this can turn into huge savings alright so again here we just saw the basics so this is just one example scriptable objects can be used for just about anything in fact you've almost certainly used scriptable object without even knowing it like i said they can be used for building editor tools and many unity features and editor tools use scriptable objects to handle their data so for example one of the things is the universal render pipeline asset so this is one example of a scriptable object and everything here is made with a custom editor another example is over here a post-processing profile which once again all this data is stored using a scriptable object so you can see how even without knowing about scriptural objects you've already used them so if you're making your own custom tools you can use cryptomol objects combined with custom editors to manage whatever data you need to save alright so that's the basics now in the next video we're going to apply what we learned here and modify our previously made inventory and crafting systems to work with scriptable objects so we're going to create some really nice designer friendly objects to create each item and each recipe so go ahead and hit the bell icon to be notified when that video comes out which should be soon if you found this video helpful consider liking and subscribing this video is made possible thanks to these awesome supporters go to patreon.com unitycodemonkey to get some perks and help keep the videos free for everyone as always you can download the project files and utilities from unitycodemonkey.com subscribe to the channel for more unt tutorials post any question comments and i'll see you next time
Info
Channel: Code Monkey
Views: 36,391
Rating: undefined out of 5
Keywords: unity scriptable objects, unity scriptable objects inventory, unity scriptable objects architecture, unity create asset menu, code monkey, unity create assets, unity tutorial, unity game tutorial, unity tutorial for beginners, unity 3d, unity, game design, game development, game dev, game development unity, unity 2d, unity 3d tutorial, programming, coding, c#, code, software development, learn to code, learn programming, unity tutorials, unity game designer
Id: 7jxS8HIny3Q
Channel Id: undefined
Length: 9min 22sec (562 seconds)
Published: Thu Jul 30 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.