Version: 2017.1
流媒体资源 (Streaming Assets)
Editor 高级主题

ScriptableObject

ScriptableObject is a class that allows you to store large quantities of shared data independent from script instances. Do not confuse this class with the similarly named SerializableObject, which is an editor class and fills a different purpose. 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 将所有原始类型、字符串、数组、列表、特定于 Unity 的类型(如 Vector3)以及具有可序列化属性的自定义类序列化为_副本_,这些副本属于声明了这些内容的对象。这意味着,如果创建了一个 ScriptableObject 并在其声明的数组中存储百万个整数,那么该数组将与该实例一起存储。这些实例被认为拥有自己的数据。ScriptableObject 字段或者任何 UnityEngine.Object 字段(比如 MonoBehaviour、Mesh、GameObject 等等)按照_引用_来存储,而不是按照值来存储。如果某个脚本引用了包含百万个整数的 ScriptableObject,Unity 将只在脚本数据中存储对 ScriptableObject 的引用。ScriptableObject 继而存储该数组。引用 ScriptableObject 的预制件的 10 个实例(保存 4MB 数据)总共大约为 4MB 而不是另一个示例中所讨论的 40MB。

使用 ScriptableObject 的目标用途是通过避免值的副本来减少内存使用量,但也可以将其用于定义可插入的数据集。这方面的一个例子是,假设 RPG 游戏中有一个 NPC 商店。可以创建自定义 ShopContents ScriptableObject 的多个资源,每个资源定义一组可供购买的物品。在游戏具有三个区域的情况下,每个区域可以提供不同层级物品。商店脚本将引用一个 ShopContents 对象,而该对象定义可供购买的物品。请参阅脚本参考中的示例。

定义了 ScriptableObject 派生类后,可以使用 CreateAssetMenu 属性,从而轻松地使用该类创建自定义资源。

提示:在 Inspector 中使用 ScriptableObject 引用时,可以双击引用字段来打开 ScriptableObject 的 Inspector。还可以创建自定义编辑器来定义该类型的 Inspector 外观,从而帮助管理它所代表的数据。

流媒体资源 (Streaming Assets)
Editor 高级主题
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961