This is a description of Scriptable objects from Unity Manual — ScriptableObject
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
Every time you instantiate that Prefab, it will get its own copy of that data. Instead of using the method, and storing duplicated data, you can use a ScriptableObject to store the data and then access it by reference from all of the Prefabs. This means that there is one copy of the data in memory.
ScriptableObjects cannot be attached to a GameObject
When you use the Editor, you can save data to ScriptableObjects while editing and at run time because ScriptableObjects use the Editor namespace and Editor scripting. In a deployed build, however, you can’t use ScriptableObjects to save data, but you can use the saved data from the ScriptableObject Assets that you set up during development.
Data that you save from Editor Tools to ScriptableObjects as an asset is written to disk and is therefore persistent between sessions.
Scriptable objects are very useful as templates, for example
Here we have some cards with text (info) and pics.
With scriptable objects, we can create one template and create as many cards as we want changing only the info (text) and the image (pic)
For instance, we can create a new panel with the objects for each card
- Create a script called
- Drag and drop the script into the Cards_panel
- Inside the script, declare the following
- Assign each one in the hierarchy
The objective will be to update this info through code at runtime
- Create a script CardModel. This will be the model (template) for all the cards
- Instead of
MonoBehaviour, it will inherit from
The first script will show the info, this script will store the data to be shown
CardModel can’t be attached to any game object, so we need to create a new scriptable object with a Unity attribute
In the project window, create a new scriptable object, call it Africa and fill it in with the info
So, after creating all the scriptable objetcs, and in this map:
We can create a
DisplayCard method and a
CardModel array in
And assign the values in the inspector
Later in the map panel, assign the
DisplayCard method to
OnClick event, for instance, AfricaLabel (button), and also to enable the Cards_panel gameobject
And you can do the same for the remaining cards
Also, Add an OnClick event to the close button in order to close the Cards_panel