Unity Scriptable Object – Data Container

Unity scriptable object is used as a data containers.It stores a large quantity of shared data without any script instance.It is like one of our asset in unity.Yes, it can be saved as an asset in our project.

Unity scriptable object can be used to store data, but it also helps to serialize objects in our scene.Here we saw that how scriptable object can help us to store data.

It can also help us to reduce memory usage and get better performance, it is like a pluggable data set.An example of this would be to imagine a Gun Inventory in Shooting Multiplayer Game.You could create multiple assets of each gun with an intentional gun properties.Each gun has a different but static property on each level, so it could be helpful to store gun properties for an independent level.

Do we implement a script that creates all types of the scriptable object types? , yes we can do it using generic methods in c#.lets see how we can do this

This method easily creates a new asset file of the scriptable object-derived class.This new asset will be created at path assetPathAndName, which is variable with a storage path.

let’s implement this using such a good example.Imagine we have to store an item with its name, icon, and gameobject.so let’s create a serializable class InventoryItem with item properties.

This serializable class contains an item’s name, icon, and gameobject which is stored in an asset.

Unity scriptable object

What?Do you have more than one Item in the application?.Don’t worry we can also create a list of an item using a scriptable object.

List of items looking like this,

List Image

It’s going well, isn’t it?.Now let’s implement a function to create an asset from this two scriptable objects.

MenuItem attribute of Unity Editor can help you to a better creation of this asset.This will create an asset with a single click from unity editor.

Asset List

The Static functions in inventory class are necessary to show a menu in unity editor.Create this asset just by clicking in the editor.Which is good rather than creating it from a script.

Saving and loading scriptable object:

a saving scriptable object in a file is easy and maintainable.

and loading is much easier than this.


Get some other helpful asset:

1 thought on “Unity Scriptable Object – Data Container”

Leave a Reply

Your email address will not be published. Required fields are marked *