Assets, Resources and AssetBundles(3)翻译
3.The Resources folder
This chapter discusses the Resources system. This is the system that allows developers to store Assets within one or more folders named Resources and to load or unload Objects from those Assets at runtime using the Resources API.
本章讨论了资源系统(Resources system)。该系统允许开发人员在一个或多个名为Resources的文件夹中存储Assets ,并在运行时使用Resources API从这些Assets中加载或卸载对象。
3.1. Best Practices for the Resources System
Don't use it.
This strong recommendation is made for several reasons:
提出这一强烈建议有以下几个原因:
- Use of the Resources folder makes fine-grained memory management more difficult
- 使用Resources文件夹会增加细粒度内存管理的难度
- Improper use of Resources folders will increase application startup time and the length of builds
- Resources文件夹的不当使用将增加应用程序的启动时间和构建的长度
- As the number of Resources folders increases, management of the Assets within those folders becomes very difficult
- 随着Resources文件夹数量的增加,管理这些文件夹中的Assets变得非常困难
- The Resources system degrades a project's ability to deliver custom content to specific platforms and eliminates the possibility of incremental content upgrades
- 资源系统(Resources system)降低了项目向特定平台交付定制内容的能力,并消除了增量内容升级的可能性
- AssetBundle Variants are Unity's primary tool for adjusting content on a per-device basis
- AssetBundle Variants (变体)是Unity在每个设备基础上调整内容的主要工具
3.2. Proper uses of the Resources system
There are two specific use cases where the Resources system can be helpful without impeding good development practices:
有两个特定的用例,资源系统( Resources system)可以在不妨碍良好开发实践的情况下提供帮助:
- The ease of the Resources folder makes it an excellent system to rapidly prototype. However, when a project moves into full production, the use of the Resources folder should be eliminated.
- 资源文件夹的易用性使其成为快速原型的优秀系统。但是,当项目进入完全生产状态时,应该取消对Resources文件夹的使用。
- The Resources folder may be useful in some trivial cases, if the content is:
- 资源文件夹可能在一些琐碎的情况下有用,如果内容是:
- Generally required throughout a project's lifetime
- 通常在项目的整个生命周期中都需要
- Not memory-intensive
- 不是内存密集型
- Not prone to patching, or does not vary across platforms or devices
- 不容易打补丁,或者不因平台或设备而异
- Used for minimal bootstrapping
- 用于最小自展
Examples of this second case include MonoBehaviour singletons used to host prefabs, or ScriptableObjects containing third-party configuration data, such as a Facebook App ID.
第二种情况的例子包括用于托管预装程序的MonoBehaviour单例,或者包含第三方配置数据(如Facebook应用程序ID)的ScriptableObjects,比如Facebook应用程序ID。
3.3. Serialization of Resources
The Assets and Objects in all folders named "Resources" are combined into a single serialized file when a project is built. This file also contains metadata and indexing information, similar to an AssetBundle. As described in the AssetBundle documentation, this index includes a serialized lookup tree that is used to resolve a given Object's name into its appropriate File GUID and Local ID. It is also used to locate the Object at a specific byte offset in the serialized file's body.
在构建项目时,所有名为“Resources”的文件夹中的资产和对象被合并到单个序列化文件中。这个文件还包含元数据(metadata )和索引信息(indexing information),类似于一个AssetBundle。如AssetBundle文档中所述,该索引包括一个序列化的查找树,用于将给定Oeject的名称解析为合适的File GUID和Local ID。它还用于在序列化文件体的特定字节偏移处定位对象。
On most platforms, the lookup data structure is a balanced search tree, which has a construction time that grows at an O(n log(n)) rate. This growth also causes the index's loading time to grow more-than-linearly as the number of Objects in Resources folders increases.
在大多数平台上,查找数据结构是一个平衡搜索树,它的构造时间以O(n log(n))的速度增长。这种增长还导致索引的加载时间随着Resources 文件夹中对象数量的增加而超过线性地增长。
This operation is unskippable and occurs at application startup time while the initial non-interactive splash screen is displayed. Initializing a Resources system containing 10,000 assets has been observed to consume multiple seconds on low-end mobile devices, even though most of the Objects contained in Resources folders are rarely actually needed to load into an application's first scene.
此操作是不可跳过的,并且在应用程序启动时显示初始的非交互闪屏时发生。在低端移动设备上,初始化包含10,000个资产的资源系统(Resources system)会花费数秒,尽管Resources 文件夹中包含的大多数对象实际上很少需要加载到应用程序的第一个场景中。