Scriptable Objects in Unity
A ScriptableObject is a data container that you can use to save large amounts of data, independent of class instances. One of the main use cases for ScriptableObjects is to reduce your Project’s memory usage by avoiding copies of values. This is useful if your Project has a prefab
that stores unchanging data in attached MonoBehaviour scripts.
There are a bunch of case scenarios where you can use Scriptable Objects, but today we are goint to use an specific one.
Creating Characters With Scriptable Objects
First, let’s create our ScriptableCharacter class.
We have to make the class inherit from ScriptableObject.
Then let’s add the attributes to the class so we can create ScriptableObjects through the unity project view.
Defining the class fields
Once we have our class defined to create an Item, let’s go back to Unity.
Creating a ScriptableObject
This will create us the file you can see behind.
In the inspector we can define our Character Properties through the inspector.
Let’s define the CharacterUI class
In this piece of code we are telling Unity to set-up the UI once the game starts. But before that we need to initialize all those fields through the inspector.
Normally, what people do is set-up the stats in the inspector view. But in this case we only have to reference the scriptable object we just created.
Once we compile the project, this is the result.