How To Use Scriptable Objects in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there i'm matt and welcome to game dev guide scriptable objects you may have seen the term thrown around a bit among unity developers or this might be your first time hearing the phrase either way if you're watching this you're probably curious what the hell they are what they do and how you should use them so what are scriptable objects put simply there are data asset a scriptable object is a custom asset that you create in your project folder that holds a bunch of data in the same way you'd assign any other asset such as a prefab sprite or an audio asset to a mana behavior you can also assign a custom scriptable object asset scriptable objects are extremely useful for managing shared data especially constant data that isn't going to change while the game is running imagine you're making a driving game and you want each vehicle in the game to drive at exactly the same speed we can create a script tool object that holds our speed variable and using the create a set menu attribute in our script we can create the asset by right-clicking in our project then we define a speed for our vehicles on our asset and in our vehicle class we read the speed from the scriptural object asset this creates an object property in the inspector of all of our cars so if we assign our object to it and test the game we can see that all of our cars will move at the same speed now if we go into our object asset and adjust the speed while the game's running they'll update scriptable objects allow us to easily tune data and performance values for our game also notice how the scriptable object has maintained the change we made in play mode so let's suppose we now want some of the vehicles to behave differently let's simply duplicate this asset and change the speed we'll assign this new asset to a few of our friends here hit play and we've got some very drear call behaviors scriptable objects can be extremely useful for sharing data like this across entities in your game you can also nest scriptable objects for more advanced shared data management let's suppose you're building a role-playing game you may have a blob enemy and you want 5 different variants of the blob for the 5 different biomes in your game now you want there to be some marginal differences between them such as movesets but the core stats are mostly the same you could have a base scriptable object with the base stats this would then be inherited by another scriptable object with any variant data such as new moves different materials or textures or maybe a different prefab model then that variant data would be assigned to the base script for your enemy if later in design you wanted to change the health for all of the blob types across your game you simply have to change the base scriptable object scriptural objects aren't just for handling data they're also capable of performing functions and support custom editors in the inspector for instance in this project I've created a scriptable object class the sound effects that allows me to manipulate them dynamically the class holds an audio clip and allows me to vary the pitch and volume of my sound I've also created a custom editor script that allows me to preview the sound effect in the editor this is extremely useful as it means that I don't have to assign the asset to anything I'm not forced to play the game to preview the outcome of the sound it works right here in the editor when I do want the sound to play in game I just have to assign the asset to a reference and call the play method on the asset from the script so that's just a brief overview of the power of scriptable objects this is honestly just tip of the iceberg stuff for how flexible they can be so if posted some links below for further reading for anyone that's interested and hopefully you can see a way they'll be helpful for you in your project if you've enjoyed this video be sure to hit the like button and leave a comment let me know if you're new to scriptable objects or if you've been using them for a while let me know what you're using them for and of course if you're interested in more game dev tips tricks and tutorials be sure to subscribe I'll see you next time thanks for watching [Music] you
Info
Channel: Game Dev Guide
Views: 102,339
Rating: undefined out of 5
Keywords: unity, learn, games, gamedev, how to, making games in unity, tutorial, unity tutorial, learning unity, unity3d tutorial, programming, making games
Id: lJxy3oTZeCs
Channel Id: undefined
Length: 4min 27sec (267 seconds)
Published: Mon Apr 22 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.