The Resources folder Resources文件夹 最佳实践系列4

本文探讨Unity的Resources系统,不建议在全面生产中使用,因其可能导致内存管理困难、启动时间增加和资源管理复杂。Resources适合快速原型和少量非内存敏感、不频繁更新的内容,例如初始化设置和第三方配置数据。Resources文件夹在构建时会被序列化成一个文件,其加载时间随对象数量增加而增长,影响应用启动速度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The Resources folder Resources文件夹

本文档主要是对Unity官方手册的个人理解与总结(其实以翻译记录为主:>)
仅作为个人学习使用,不得作为商业用途,欢迎转载,并请注明出处。
文章中涉及到的操作都是基于Unity2018.3版本
参考链接:https://unity3d.com/cn/learn/tutorials/topics/best-practices/resources-folder?playlist=30089

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的文件夹中,并在运行时使用Resources API从这些资产中加载或卸载对象。

2.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 文件夹数量的增加,管理这些文件夹中的资产变得非常困难
  • The Resources system degrades a project’s ability to deliver custom content to specific platforms and eliminates the possibility of incremental content upgrades
    资源系统降低了项目向特定平台交付自定义内容的能力,并消除了增量内容升级的可能性
    • AssetBundle Variants are Unity’s primary tool for adjusting content on a per-device basis
      AssetBundle变体是Unity根据每个设备调整内容的主要工具

2.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:
有两个特定的用例,资源系统可以在不妨碍良好开发实践的情况下提供帮助:

  1. 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文件夹的易用性使其成为快速原型化的优秀系统。然而,当项目进入全面生产时,应该避免使用Resources文件夹。
  2. The Resources folder may be useful in some trivial cases, if the content is:
    Resources文件夹在一些 不重要的情况下可能很有用。如果内容是:
    • 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)的脚本化对象。

2.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 和索引信息,类似于一个AssetBundle。如AssetBundle文档所述,该索引包含一个序列化查找树,用于将给定对象的名称解析为相应的文件GUID和本地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文件夹中包含的大多数对象实际上很少需要加载到应用程序的第一个场景中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值