Basics of Scriptable Objects in Unity

Today we will tackle something easier as the subject of Scriptable Objects in Unity is fairly straightforward. What is so cool about them is that you can throw so much different thing in them!

What Scriptable Object is?

According to the definition provided by Unity, Scriptable Object is a data container that let you save a lot of data and reduce memory usage. You can create them from code or by making new instances in the project view.

Creating Scriptable Object

Thanks to that you can easily create a lot of different representation of data, for example, inventory items, skills, game modes and more.

Using Scriptable Objects as items

The sky is the limit as they say ?

Example

So how you can create your first Scriptable Object in Unity?

If you can create a MonoBehaviour script, you are almost done. To make the script into Scriptable Object, you only need to change base class.

using UnityEngine;

/// <summary>
/// Class containing info about items.
/// </summary>
[CreateAssetMenu(fileName = "Item Info", menuName = "Example/New Item Info")]
public class ItemInfo : ScriptableObject
{
    [SerializeField]
    // Item id.
    private string itemId;
    public string ItemId => itemId;

    [SerializeField]
    // Item durability.
    private float itemDurability;
    public float ItemDurability => itemDurability;

    [SerializeField]
    // Item preview.
    private Sprite itemPreview;
    public Sprite ItemPreview => itemPreview;

}

See? ?

With done just that, now we can create some instances in the project of it!

Creating new items

As we add more variable to the script we can edit more fields, exactly as in regular component attached to the Game Object.

Item displayed in Inspector

What is also cool about having variables in Scriptable Object rather than in components attached to Game Objects is that we can tweak game parameters in Play Mode as all the changes are going to be saved! ?

Another thing is that you can assign references to the Scriptable Objects in the same way as you are doing it with other references! Just drag and drop! ?

Unfortunately, we can’t use Scriptable Objects in the same way as other Data models. If you remember my post about Data Serialization, we could serialize Data model into text and then back to the object. With Scriptable Objects, we can’t really do that in such an easy way.

Of course, they were designed with something different in mind so I can’t really say more bad things about them! ?

A little fun fact

To also give you an idea of how versatile Scriptable Objects are, let me tell you a few examples where I used them.

One time I was working on the game where we had a few different game modes and obstacle spawn system based on patterns. Both of those things were using Scriptable Objects but in different ways.

Obstacle spawner used them for storing information about patterns and where to place obstacles. Besides Spawner, Scriptable Objects also had references to each other as patterns could be chained together. ?

In the case of game modes, we used Scriptable Objects as Game Modes itself, so they were controlling gameplay, rules, and spawners.

using UnityEngine;

/// <summary>
/// Game Mode base class implemented as Scriptable Object
/// </summary>
[CreateAssetMenu(fileName = "Game Mode", menuName = "Example/New Game Mode")]
public class GameMode : ScriptableObject
{
    /// <summary>
    /// Method called to initialize loading game mode.
    /// </summary>
    public void LoadGameMode()
    {
        // Code responsible for loading content for game mode.
    }

    /// <summary>
    /// Method called each frame to have game mode loop.
    /// </summary>
    public void UpdateGameMode()
    {
        // Code responsible for game mode loop.
    }

    /// <summary>
    /// Method called to initialize unloading game mode.
    /// </summary>
    public void UnloadGameMode()
    {
        // Code responsible for unloading content for game mode.
    }
}

That was short!

There is not a lot to combine into a repository, so I’m going to skip it this time.

Nonetheless, I hope you enjoyed it and see you next time! ?

4 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x