ScriptableObject は、クラスのインスタンスとは独立した大量のデータを保存するために使用できるデータコンテナです。ScriptableObject の主な使用例の 1 つは、値に対し複数のコピーが作成されることを回避することによってプロジェクトのメモリ消費を削減することです。これは、プロジェクトにプレハブがあり、設定された MonoBehaviour スクリプトに不変のデータを保存している場合に有効です。
そのプレハブをインスタンス化するたびにそのデータのコピーを取得します。この方法を使用して複製されたデータを格納する代わりに、ScriptableObject を使用してデータを格納すると、すべてのプレハブから参照によってアクセスすることができます。つまり、メモリ内にデータのコピーが 1 つのみ存在します。
MonoBehaviour と同様に、ScriptableObject は基本的な UnityEngine.Object から派生しますが、MonoBehaviour と異なり、ScriptableObject をゲームオブジェクトにアタッチすることはできません。代わりに、プロジェクトのアセットとして保存する必要があります。
Unity エディターを使用する場合、ScriptableObject はエディターの名前空間やスクリプトを使用するため、編集中やランタイムにデータを ScriptableObject に保存することができます。ただし、デプロイされたビルドでは、ScriptableObject を使用してデータを保存することはできませんが、開発時に設定した ScriptableObject のアセットから保存されたデータを使用できます。
エディターツールから ScriptableObject にアセットとして保存したデータは、ディスクに書き込まれます。そのため、セッション間で永続的です。
このページでは、ScriptableObject クラスの概要と、このクラスを使用してスクリプトを作成する際の一般的な使用方法について説明します。ScriptableObject クラスのすべてのメンバーの詳細なリファレンスについては、ScriptableObject スクリプトリファレンスを参照してください。
ScriptableObject の主な使用例は以下のとおりです。
ScriptableObject は ScriptableObject クラスから継承する必要があります。
新しい ScriptableObject スクリプトを作成するには、以下のいずれかを実行します。
CreateAssetMenu 属性を使用して、クラスを使用するカスタムアセットを作成できます。 例:
using UnityEngine;
[CreateAssetMenu(fileName = "Data", menuName = "ScriptableObjects/SpawnManagerScriptableObject", order = 1)]
public class SpawnManagerScriptableObject : ScriptableObject
{
public string prefabName;
public int numberOfPrefabsToCreate;
public Vector3[] spawnPoints;
}
Assets フォルダー内の前述のスクリプトを使用して、Assets > Create > ScriptableObjects > SpawnManagerScriptableObject の順に移動し、ScriptableObject のインスタンスを作成できます。新しい ScriptableObject インスタンスに分かりやすい名前を付け、値を変更します。これらの値を使用するには、ScriptableObject を参照する新しいスクリプト (この場合は SpawnManagerScriptableObject) を作成する必要があります。
例:
using UnityEngine;
public class Spawner : MonoBehaviour
{
// The GameObject to instantiate.
public GameObject entityToSpawn;
// An instance of the ScriptableObject defined above.
public SpawnManagerScriptableObject spawnManagerValues;
// This will be appended to the name of the created entities and increment when each is created.
int instanceNumber = 1;
void Start()
{
SpawnEntities();
}
void SpawnEntities()
{
int currentSpawnPointIndex = 0;
for (int i = 0; i < spawnManagerValues.numberOfPrefabsToCreate; i++)
{
// Creates an instance of the prefab at the current spawn point.
GameObject currentEntity = Instantiate(entityToSpawn, spawnManagerValues.spawnPoints[currentSpawnPointIndex], Quaternion.identity);
// Sets the name of the instantiated entity to be the string defined in the ScriptableObject and then appends it with a unique number.
currentEntity.name = spawnManagerValues.prefabName + instanceNumber;
// Moves to the next spawn point index. If it goes out of range, it wraps back to the start.
currentSpawnPointIndex = (currentSpawnPointIndex + 1) % spawnManagerValues.spawnPoints.Length;
instanceNumber++;
}
}
}
注意 スクリプトファイルは、クラスと同じ名前でなければなりません。
前述のスクリプトをシーンのゲームオブジェクトにアタッチします。次に、Inspector で、Spawn Manager Values フィールドを設定した新しい SpawnManagerScriptableObject に設定します。
Entity To Spawn フィールドを Assets フォルダー内の任意のプレハブに設定し、エディターで Play をクリックします。Spawner で参照したプレハブは、SpawnManagerScriptableObject インスタンスで設定した値を使用してインスタンス化します。
Inspector で ScriptableObject の参照を扱う場合は、参照フィールドをダブルクリックして ScriptableObject の Inspector を開きます。カスタムのエディターを作成して、表すデータを管理しやすいように Inspector を好みの外見に定義することもできます。