Assets, Objects and serialization Assets, Objects与序列化
本文档主要是对Unity官方手册的个人理解与总结(其实以翻译记录为主:>)
仅作为个人学习使用,不得作为商业用途,欢迎转载,并请注明出处。
文章中涉及到的操作都是基于Unity2018.3版本
参考链接:https://unity3d.com/cn/learn/tutorials/topics/best-practices/assets-objects-and-serialization?playlist=30089
This chapter covers the deep internals of Unity’s serialization system and how Unity maintains robust references between different Objects, both in the Unity Editor and at runtime. It also discusses the technical distinctions between Objects and Assets. The topics covered here are fundamental to understanding how to efficiently load and unload Assets in Unity. Proper Asset management is crucial to keeping loading times short and memory usage low.
本章将介绍Unity序列化系统的深层内部原理,以及Unity如何在Unity编辑器和运行时维护不同对象之间的健壮引用。还讨论了对象和资产之间的技术区别。这里涉及的主题对于理解如何有效地加载和卸载Unity中的资产是基础的。适当的资产管理对于保持加载时间短和内存使用率低是至关重要的。
1.1. Inside Assets and Objects 资产与对象的内在
To understand how to properly manage data in Unity, it is important to understand how Unity identifies and serializes data. The first key point is the distinction between Assets and UnityEngine.Objects.
要理解如何正确地管理Unity中的数据,理解Unity如何标识和序列化数据是很重要的。第一个关键点是资产和UnityEngine.Objects之间的区别。
An Asset is a file on disk, stored in the Assets folder of a Unity project. Textures, 3D models, or audio clips are common types of Assets. Some Assets contain data in formats native to Unity, such as materials. Other Assets need to be processed into native formats, such as FBX files.
资产是磁盘上的一个文件,存储在Unity项目的Assets文件夹中。纹理、3D模型或音频剪辑是常见的资产类型。有些资产以Unity原生的格式包含数据,比如materials。需要将其他资产处理为原生格式,比如FBX文件。
A UnityEngine.Object, or Object with a capitalized ‘O’, is a set of serialized data collectively describing a specific instance of a resource. This can be any type of resource which the Unity Engine uses, such as a mesh, sprite, AudioClip or AnimationClip. All Objects are subclasses of the UnityEngine.Object base class.
UnityEngine.Object或大写“O”的对象是一组序列化数据,它们共同描述资源的一个特定实例。这可以是Unity引擎使用的任何类型的资源,例如网格、精灵、AudioClip或AnimationClip。所有对象都是 UnityEngine.Object基类的子类。
While most Object types are built-in, there are two special types.虽然大多数对象类型都是内置的,但是有两种特殊类型。
- A ScriptableObject provides a convenient system for developers to define their own data types. These types can be natively serialized and deserialized by Unity, and manipulated in the Unity Editor’s Inspector window.
ScriptableObject为开发人员定义自己的数据类型提供了一个方便的系统。这些类型可以由Unity本地序列化和反序列化,并在Unity编辑器的检查器窗口中进行操作。 - A MonoBehaviour provides a wrapper that links to a MonoScript. A MonoScript is an internal data type that Unity uses to hold a reference to a specific scripting class within a specific assembly and namespace. The MonoScript does not contain any actual executable code.
MonoBehaviour提供了一个链接到MonoScript的接口。MonoScript是一种内部数据类型,Unity使用它在特定的程序集和命名空间中保存对特定脚本类的引用。MonoScript不包含任何实际的可执行代码。
There is a one-to-many relationship between Assets and Objects; that is, any given Asset file contains one or more Objects.
资产与对象之间存在一对多的关系;也就是说,任何给定的资产文件都包含一个或多个对象。
1.2. Inter-Object references 对象间引用
All UnityEngine.Objects can have references to other UnityEngine.Objects. These other Objects may reside within the same Asset file, or may be imported from other Asset files. For example, a material Object usually has one or more references to texture Objects. These texture Objects are generally imported from one or more texture Asset files (such as PNGs or JPGs).
所有UnityEngine.Objects可以引用其他UnityEngine.Objects。这些其他对象可以驻留在相同的资产文件中,也可以从其他资产文件导入。例如,一个材质对象通常有一个或多个纹理对象的引用。这些纹理对象通常从一个或多个纹理资产文件(如png或JPGs)导入。
When serialized, these references consist of two separate pieces of data: a File GUID and a Local ID. The File GUID identifies the Asset file where the target resource is stored. A locally unique(1) Local ID identifies each Object within an Asset file because an Asset file may contain multiple Objects.
序列化后,这些引用由两个单独的数据组成:文件GUID和本地ID。文件GUID标识存储目标资源的资产文件。本地惟一的(对于同一个资产文件本地ID唯一)Local ID标识资产文件中的每个对象,因为一个资产文件可能包含多个对象。
File GUIDs are stored in .meta files. These .meta files are generated when Unity first imports an Asset, and are stored in the same directory as the Asset.
文件GUIDs 存储在.meta文件中。这些.meta文件是在Unity首次导入资产时生成的,并与资产存储在同一个目录中。
The above identification and referencing system can be seen in a text editor: create a fresh Unity project and change its Editor Settings to expose Visible Meta Files and to serialize Assets as text. Create a material and import a texture into the project. Assign the material to a cube in the scene and save the scene.
上面的标识和引用系统可以在文本编辑器中看到:创建一个新的Unity项目并更改其编辑器设置,以公开可见的Meta 文件并将资产序列化作为文本。创建一个材质并将纹理导入到项目中。将材质分配到场景中的一个立方体中并保存场景。
Using a t

本文详细介绍了Unity中Assets、Objects和序列化的概念,包括它们之间的关系、引用系统、文件GUID和本地ID的作用。资产是磁盘上的文件,而Objects是Unity中描述资源实例的序列化数据。Unity使用File GUID和Local ID来唯一标识和引用对象,确保资源在移动或导入时的引用稳定性。此外,文章还探讨了对象加载、卸载的场景,以及加载大型对象层次结构时的性能考虑。
最低0.47元/天 解锁文章
7705

被折叠的 条评论
为什么被折叠?



