ScriptableObject はスクリプトインスタンスから独立した大量の共有データを格納できるクラスです。SerializableObject と混同しがちですが、こちらはエディタークラスであり、目的も違うものです。例えば integer が 1 万行ある配列のスクリプトのプレハブを作成したとします。配列は 4 MB のメモリを占有し、プレハブがオーナーとなります。プレハブをインスタンス化するごとに、その配列が複製されます。ゲームオブジェクトを 10 個作成した場合、10 インスタンス合計で 40 MB となります。
すべてのプリミティブ型、文字列、配列、リスト、Unity に特有の Vector 3 などの型や、カスタムクラスを Unity は serialize 属性により宣言されたオブジェクトに属する複製としてシリアライズします。例えば SerializableObject を作成して integer が 1 万個だけ格納した場合、配列はそのインスタンスに格納されます。インスタンスは個別のデータのオーナーとして扱われます。ScriptableObject のフィールドや任意の UnityEngine.Object フィールドである MonoBehaviour、Mesh、GameObject 等は値でなく参照が格納されます。ScriptableObject は配列を格納します。ScriptableObject への参照があるプレハブのインスタンスが 10 個あり、4 MB のデータを保有する場合、合計はあくまで 4 MB となり前例のように 40 MB とはなりません。
ScriptableObject を使用する場面は、値の複製をさけることでメモリ消費を節約するときですが、プラグ可能なデータセットを定義することにも使用できます。例をあげると RPG における NPC キャラクターのショップです。例えばカスタムの ShopContents ScriptableObject で複数のアセットを作成して、それぞれで購入可能なアイテムを定義できます。ゲームに 3 つのゾーンがあると仮定すると、それぞれのゾーンで異なるレベルのアイテムを提供できます。ショップのスクリプトの中で ShopContents オブジェクトを参照して利用可能なアイテムを定義できます。サンプルについてはスクリプトリファレンスを参照してください。
ScriptableObject 派生のクラスに付けることのできる CreateAssetMenu によって簡単にカスタムアセットを作成することができます。
Tips: インスペクター上で ScriptableObject からの参照を扱う場合、参照フィールドをダブルクリックして ScriptableObject を開きます。またカスタムのエディターを作成して、その型が表すデータが管理しやすいようにインスペクターの外見を定義できます。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.