Scriptable Objects in Unity

Today, I have met with a great class: Scriptable Objects. I am pretty surprised to find out how easy it is to create and use scriptable objects. So, here we go:

I have been recently working on learning application development with Unity, which actually has been great in terms of improving my UI skills. Current application is a basic zoo app and here’s a shot from the application: The map.

By clicking on the interest points, a card window will pop-up and the user will be able to gather some basic information.

Rather than creating each information card one by one, Scriptable Objects will be used. Basicly, Scriptable objects act as data containers. Here’s the definition from the Unity Manual:

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.

For more detailed information, click the link below for Unity Manual:

The idea is that I will be only creating a card, whose content will be manipulated by using Scriptable Objects.

Here’s a sample card:

This card will act as a template.

Once clicked on an interest point on the map, The Card Panel will be activated. A card, consists of the following game objects:

By attaching a script to the Card Panel, we can easily manipulate the child game objects within the Card Panel. The fields that we want to change are simply provided below, the rest (the background, close button etc and logo) will not be changed.

The cards will be sharing these information with different information — which will be manipulated with scripting.

Now, Scriptable Objects. Create a new script, which will be our Scriptable Objects template. Rather than MonoBehaviour, the template will inherit from ScriptableObject class. If you try to attach this script to a game object, you will realize that Unity will give out an error stating that you can’t.

CardModel Script

This script simply defines the fields that we are going to change according to the user selection — our template. To make use of this script, we need to add CreateAssetMenu as an attribute, which will allow us to create new scripts using this template by adding the corresponding class to the Asset/Create as a menu item:

Now, we can easily create new Cards. As you can see below, fileName (optional) is the name that appears once we create a new object and menu name is the name that appears on the menu.

It is really surprising to find out how easy it is to create multiple Cards within only seconds for each. Once we create model for each of the clickable fields on the map, the only thing left is to find out a way to call them via scripting.

On the CardView script (the script attached to Card Panel), by creating an array of the CardModel (the scriptable object), drag and drop each of the card models you have created. By their rankings within the array, we can easily call them.

Cardview Script
Card View Script Inspector

As the map is already designed in a way that each of the interest points, the only thing left to do is adding the on click events to each button, by assigning the corresponding array integer values.

And this is how easy it is. Rather than creating multiple redundant duplicates, we have created an efficient application by using scriptable objects. Each of the scriptable object models we have created only consumes a space of 1 KB.