Scriptable Object

时间:2023-12-11 15:32:32

Scriptable Object

  ScriptableObject 是一个可继承的Class,适用于存储大数据的情形。

   Consider for example that you have made a prefab with a script which has an array of a million integers. The array occupies 4MB of memory and is owned by the prefab. Each time you instantiate that prefab, you will get a copy of that array. If you created 10 game objects, then you would end up with 40MB of array data for the 10 instances.

  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.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. The instances are thought to own their individual data.

  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. The ScriptableObject in turn stores the array. 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.

CreateAssetMenuAttribute

  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.

  在 ScriptableObject 类定义的上一行加上 [CreateAssetMenu] ,即可通过 Asset/Create 菜单来创建此 ScriptableObject 类的asset。

  

参考:

1、http://docs.unity3d.com/Manual/class-ScriptableObject.html

2、http://docs.unity3d.com/ScriptReference/CreateAssetMenuAttribute.html