Saving and storing data during an Editor session.The main use cases for ScriptableObjects are: For an exhaustive reference of every member of the ScriptableObject class, see the ScriptableObject script reference. This page provides an overview of the ScriptableObject class and its common uses when scripting with it. In a deployed build, however, you can’t use ScriptableObjects to save data, but you can use the saved data from the ScriptableObject Assets that you set up during development.ĭata that you save from Editor Tools to ScriptableObjects as an asset is written to disk and is therefore persistent between sessions. When you use the Editor, you can save data to ScriptableObjects while editing and at run time because ScriptableObjects use the Editor namespace and Editor scripting. Instead, you need to save them as Assets in your Project. A GameObject’s functionality is defined by the Components attached to it. Just like MonoBehaviours, ScriptableObjects derive from the base Unity object but, unlike MonoBehaviours, you can not attach a ScriptableObject to a GameObject The fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. This means that there is one copy of the data in memory. Instead of using the method, and storing duplicated data, you can use a ScriptableObject to store the data and then access it by reference from all of the Prefabs. More info See in Glossary.Įvery time you instantiate that Prefab, it will get its own copy of that data. More info See in Glossary that stores unchanging data in attached MonoBehaviour scripts A piece of code that allows you to create your own Components, trigger game events, modify Component properties over time and respond to user input in any way you like. The prefab acts as a template from which you can create new object instances in the scene. This is useful if your Project has a Prefab An asset type that allows you to store a GameObject complete with components and properties. One of the main use cases for ScriptableObjects is to reduce your Project’s memory usage by avoiding copies of values. You can also create a custom Editor to define the look of the inspector for your type to help manage the data that it represents.A ScriptableObject is a data container that you can use to save large amounts of data, independent of class instances. Tip: When working with ScriptableObject references in the inspector, you can double click the reference field to open the inspector for your ScriptableObject. Once you have defined a ScriptableObject-derived class, you can use the CreateAssetMenu attribute to make it easy to create custom assets using your class. Please see the scripting reference for examples. Your shop script would reference a ShopContents object that defines what items are available. In a scenario where the game has three zones, each zone could offer different tier items. You could create multiple assets of your custom ShopContents ScriptableObject, each defining a set of items that are available for purchase. An example of this would be to imagine a NPC shop in a RPG game. The intended use case for using ScriptableObject is to reduce memory usage by avoiding copies of values, but you could also use it to define pluggable data sets. 10 instances of a prefab that has a reference to a ScriptableObject, that holds 4MB data, would total to roughly 4MB and not 40MB as discussed in the other example. The ScriptableObject in turn stores the array. If you have a script with a reference to the ScriptableObject with the million integers, Unity will only store a reference to the ScriptableObject in the script data. ScriptableObject fields, or any UnityEngine.Object fields, such as MonoBehaviour, Mesh, GameObject and so on, are stored by reference as opposed to by value. The instances are thought to own their individual data. This means that if you created a ScriptableObject and stored the million integers in an array it declares then the array will be stored with that instance. Unity serializes all primitive types, strings, arrays, lists, types specific to Unity such as Vector3 and your custom classes with the Serializable attribute as copies belonging to the object they were declared in. If you created 10 game objects, then you would end up with 40MB of array data for the 10 instances. Each time you instantiate that prefab, you will get a copy of that array. The array occupies 4MB of memory and is owned by the prefab. Consider for example that you have made a prefab with a script which has an array of a million integers. Do not confuse this class with the similarly named SerializableObject, which is an editor class and fills a different purpose. ScriptableObject is a class that allows you to store large quantities of shared data independent from script instances.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |