Better Data with Scriptable Objects in Unity! (Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
better data with scriptable objects in unity we primarily work with mono behaviors to author gameplay logic and store data while model behaviors are powerful they are specifically bound to a scene while there are potential ways to ensure that a monobehaviour stays persistent in unity without being bound to a specific scene they require some setup and are strictly bound to play mode as an alternative unity provides project bound assets which will stay persistent within your project called scriptable objects scriptable object versus monobehavior let's first take a look at the structure of a scriptable object and compare it to the structure of a mono behavior in a typical model behavior we have many message callback functions available such as awake unenable start update on disable scriptable objects have fewer message callback functions available you will only get awake on enable on disable and on destroy message callbacks this is important as by design scriptable objects are not bound directly to the player loop and can be treated as plain old objects with methods available to be called while monobehaviours can be attached as a component to game objects scriptable objects are not instead they are created as project asset files typically through a custom editor script or with the create asset menu attribute scriptable objects can be referenced in mono behaviors to access the available data and methods to create a scriptable object instance in your project you can use the create asset menu attribute this will create a menu entry in your assets create submenu when you right click in your project view scriptable objects as a data container in the unity royale project sample we make use of scriptable objects to provide authorable decks that we can provide to both the player and the ai if we look at the deck data script we can see how it simply stores card data which is dynamically loaded on runtime like any other class in c sharp we can provide methods which help process store data such as incrementing the index of the current card when we want to draw a card this is an example of making use of a scriptable object as a data container while it is entirely possible to store this data in a mono behavior let's think of the memory impact of using this approach mono behaviors always live on a game object so by design they're bound to an instance of a game object if we store all the data in a mono behavior attached to a prefab game object and we instantiate the prefab to retrieve the data then each instance will have a full copy of all the data this includes the game object and the transform component which can become very wasteful and impact our memory performance if we are only interested in accessing a specific piece of data if the data does not belong to an instance but is more shared amongst game objects like our card data by design it is easier to store this data as a scriptable object this is because when mono behaviors reference scriptable objects they simply store the reference that points to the scriptable object and not a full copy of the scriptable object scriptable objects as enum states we can also use scriptable objects to author extended enum states all units spawned such as the warrior mage and archer stats are defined by a scriptable object type called placeable data these stats define the rules for searching out the next available target to attack and how the unit can attack for example is our unit interested in attacking only the building type both or is it just a still unit which defends our castle we can define these rules in our scriptable object runtime data editing one of the benefits of using scriptable objects as an extended enum state is that we can provide runtime data editing while in play mode in our inspector when manipulating data on a mono behavior in play mode we often have to remember what changed exit play mode and then make those changes again in the inspector with scriptable objects you can author the data during play mode and have the edited data saved after you exit play mode this is because scriptable objects are not bound to a scene's runtime as they exist on a project basis in the assets folder for example if we wanted to edit our archer's range during play mode we can locate the archer scriptable object tweak our attack range and play our archer card the data defined in the archer scriptable object will be copied over to the archery unit game object which will include our tweaked value if we're happy with the value we defined we can exit play mode and inspect the values again and see that the value we tweaked during play mode still exists in the scriptable object scriptable objects are powerful tools in unity that allow us to create unique systems architecture and custom authoring workflows we highly recommend following the links in the description to learn more about the many use cases of scriptable objects thanks for watching you
Info
Channel: Unity
Views: 156,644
Rating: undefined out of 5
Keywords: Unity3d, Unity, Unity Technologies, Games, Game Development, Game Dev, Game Engine, unity3d, unity technologies, scriptable objects, how to, unity tutorial, better data in unity, data management in unity, data management tutorial, unity scriptable objects, how to scriptable objects, scripting in unity, c#, game development, unity game development, game dev, unity3d tutorial, unity
Id: PVOVIxNxxeQ
Channel Id: undefined
Length: 5min 12sec (312 seconds)
Published: Wed Sep 23 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.