
Create and save ScriptableObject because it doesn't exist yet If (m_VehicleType & m_VehicleType.m_MaxSpeed ("Assets/VehicleTypeWagon.asset") Use parameter from the ScriptableObject to control the behaviour of the Vehicle M_CurrentSpeed += m_Acceleration * ltaTime M_Acceleration = Random.Range(0.05f, m_VehicleType.m_MaxAcceration) Public void Initialize(VehicleTypeInfo vehicleType) Reference to the ScriptableObject asset In PlayMode it accelerates up to the maximum speed permitted by its type Snippet of a MonoBehaviour that would control motion of a specific vehicle.

Public class VehicleInstance : MonoBehaviour The first script of the example implements a class derived from ScriptableObject. It isĪlso good for performance, especially in cases where the size of the shared data is substantial. This design makes it convenient to tweak vehicle behaviour in a central location. Type, rather than keeping its own redundant copy of each parameter. Each instance of the vehicle in the game would have a reference to the asset corresponding to its The following example demonstrates a typical use of a ScriptableObject: different types of vehicle parameters are represented in the fields of a VehicleTypeInfo class, derived from ScriptableObject.Įach type of vehicle would have its own asset file, with the parameter values set appropriately for the type. However, this class doesn't support the null-conditional operator ( ?.) and the null-coalescing operator ( ?).

For example, it returns true for a "obj = null" check. A ScriptableObject in this state acts as if it is null. See Script Serialization for details.Ĭlasses that include big arrays, or other potentially large data, should be declared with the PreferBinarySerialization attribute, because YAML is not an efficient representation for that sort of data.Īfter a ScriptableObject is destroyed, the C# object remains in memory until garbage is collected. The C# fields of a ScriptableObject are serialized exactly like fields on a MonoBehaviour. When a ScriptableObject is referenced from a field on a MonoBehaviour, the ScriptableObject is automatically loaded, so a script can simply use the value of the field to reach it. If a ScriptableObject has not been saved to an asset, and it is referenced from an object in a scene, Unity serializes it directly into the scene file.įor ScriptableObjects that have only a single persistent instance within a project, use the ScriptableSingleton base class.Īccess previously saved objects using AssetDatabase, for example AssetDatabase.LoadAssetAtPath. You can also generate ScriptableObjects as an output from a ScriptedImporter. You can save ScriptableObjects to asset files either from the Editor UI (see CreateAssetMenuAttribute), or by calling AssetDatabase.CreateAsset from a script. Instantiate ScriptableObject objects with CreateInstance. It’s a major opportunity for restructuring your projects in a macro sense.Use ScriptableObjects to centralise data in a way that can be conveniently accessed from scenes and assets within a project. At its highest form it’s an incredibly lightweight and diverse design tool with which to simplify and communicate between different parts of your Unity project. It can be thought of as an interactive spreadsheet with which to organize, adjust, and categorize different aspects of a database.

It’s a lovely standard by which to organize any information database. To sell it quickly, the Scriptable Object is an intelligent data container in Unity. One such tool in the quest for modularization is the Scriptable Object.

Simply put, a good complex system should be made up of small, adjustable pieces that can interact with while remaining independent from one another. It is a mechanism for complexity redistribution.”¹ Modularity is “the degree to which a system is made up of relatively independent but interacting elements, with each module typically carrying an isolated set of functionality. Complexity cannot be avoided, but it can and should be handcuffed to its sober sponsor, Modularity. You will find that the deeper you delve, the higher the likelihood that one small tweak can corrupt your entire structure. Reduce memory usage, organize, and make your project easily designable with Scriptable Objects!Īs grows the size of any system, so grows the complexity, and so the accompanying inconvenience of additions or adjustments made to it.
