ScriptableObjects

Scriptable Objects - Unity Manual, Scripting API

Scriptable Objects are amazing data containers. They don't need to be attached to a GameObject in a scene. They can be saved as assets in our project. Most often, they are used as assets which are only meant to store data, but can also be used to help serialize objects and can be instantiated in our scenes. Adam Buckner - Official Unity Tutorial

ScriptableObject is a class that allows you to store persistent data independent from script instances.

A class you can derive from if you want to create objects that don't need to be attached to game objects. This is most useful for assets which are only meant to store data.

Unity Manual Example: ScriptableObject based Spawner.

The example in the Unity Manual creates a very simple and powerful spawner using the scriptableObject to store all necessary data for spawning: spawn points and the type of prefab and the number of prefabs to spawn.

Video About Using Scriptable Objects in Unity: Richard Fine - Video

Scriptable Objects are essentially 'MonoBehaviors' but are not structured to be used as a gameObject's component. : Scriptable Objects don't require being attached to a GameObject, doesn't need to be (can't be) associated with a scene, transform, etc. SO's do not get standard Unity 'callback methods' like Start( ) , Update( ) etc, which means they don't have a 'frame-based' lifecycle. They can be 'serialized' (saved by the system) and viewed in the inspector. Richard Fine - Video 9:45

Unity Scriptable Object Patterns: (Richard Fine video 21:30)

  • DataObjects and Tables - Example - When killing a goblin, what are the items that the object can drop? Data: DropObject, ChanceOfDropping

  • Extendable Enums - Empty ScriptableObjects bound to Asset Files - Example: DamageTypes. You can compare, test equality on an emtpy object, ie: use as Dictionary Keys. Benefit is that it's easy to add new values without changing 'code'.

    • Dual Deserialization - Supports JSON Utility for saving, loading data:

    • Example: Level Layout - Editor with Level Data such as Platform, Player, Coin, Enemy position data.

    • Reload-Proof Singletons

    • (Video location: 31:00)

    Delegate Objects: Scriptable Objects can contain methods, ( not just data ) - Strategy Pattern

Resources

Unity Tutorial - Scriptable-Objects

Architect your Code in Unity

ScriptableObjects- Richard Fine

Last updated