Published on

What is a ScriptableObjects in Unity? This is an article to read when you're wondering.

Authors
  • Evelyn

Please share this article if you like!

Today I'll explain ScriptableObject for beginners!

I've been wondering a lot about what a ScriptableObject is, and the answer is simple.

It's an object that stores data.

To put it more simply, it's a mechanism for Unity to serialize and deserialize data and store parameters.

There are a lot of people who say, "It's useful to know how to use ScriptableObjects!"

There are many people who say, "It's useful to be able to use ScriptableObjects", but I think it's a difficult field for beginners to get into.

It can be used to hold master data as shared data, or to hold parameters for configuration, etc. It has a wide range of uses, so it might be a good idea to get to know it a little bit at a time.

Table of Contents

The main use cases

The main use cases for ScriptableObjects are as follows

  • Saving and storing data during an editor session.
  • Storing data as assets in a project and using them at runtime.

To use ScriptableObjects, create a script in your application's Assets folder and place it in the folder and inherit from the ScriptableObject class. using the CreateAssetMenu attribute, you can easily create custom assets using the class.

For Example

To give you an idea of how to use it, here is an example.

Manage language data for other languages

First of all, I considered the case of supporting other languages.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName = "LocalizeData", menuName = "ScriptableObject/LocalizeData", order = 1)]
public class LocalizeData : ScriptableObject
{
    [TextArea]
    public string englishText = "";
    [TextArea]
    public string japaneseText = "";

    public string GetWord()
    {
        switch (Application.systemLanguage)
        {
            case SystemLanguage.English:
                return englishText;
            case SystemLanguage.Japanese:
                return japaneseText;
            default:
                return englishText;
        }
    }
}

Let me explain one by one.

The following statement will remove the scriptable object data from the project in the Unity editor

[CreateAssetMenu(fileName = "LocalizeData", menuName = "ScriptableObject/LocalizeData", order = 1)]

Furthermore, to use it as a ScriptableObject, you need to inherit from the ScriptableObject class.

※ Please be careful not to inherit from MonoBehaviour.

public class LocalizeData : ScriptableObject

The created scriptable object can be used in the game by editing the public variables from the inspector.

Manage enemy character information

Next, I considered the case of managing enemy character data.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(menuName = "ScriptableObject/Create Enemy")]
public class Enemy : ScriptableObject {
	public string name;
	public int hiPoint;
	public int attack;
	public int defence;
	public int exp;
	public int reward;
}

In this way, you can manage data even in games with various types of enemies, as shown in the following image.

Summary of this article

This is a brief introduction to ScriptableObjects and examples of their use.

How did you like it?

I hope you will try to use them in your game development to improve the efficiency of your implementation.

If you like the content of this article, please share it on social media.

Good luck with your game development!!!

Please share this article if you like!