-
Notifications
You must be signed in to change notification settings - Fork 15
Scriptable Objects
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.
ScriptableObjects are a type of class that correctly serializes as references, so that they only get serialized once. This allows complex class interactions to be stored in a way that you would expect. Internally in Unity, ScriptableObjects and MonoBehaviours are the same; in managed userland code you can have a ScriptableObject that is not attached to a GameObject; this is different to how MonoBehaviour works. They are great for general data structure serialization.
- ScriptableObjects will only be serialized once, allowing you to use references properly.
- Use OnEnable to initialize ScriptableObjects.
- Don’t ever call the constructor of a ScriptableObject, use CreatInstance instead
- For nested data structures that are only referenced once don’t use ScriptableObject as they have more overhead.
- If your scriptable object is not rooted in the scene set the hideFlags to HideAndDontSave.
Mark a ScriptableObject-derived type to be automatically listed in the Assets/Create submenu, so that instances of the type can be easily created and stored in the project as ".asset" files.
[CreateAssetMenu(fileName = "somename", menuName = "somename")]
Unity Live Training Video: INTRODUCTION TO SCRIPTABLE OBJECTS