在某些情况下,为项目提供资源而不将资源作为场景的一部分进行加载是很有用的做法。例如,可能存在可出现在游戏的任何场景中但不经常使用的角色或其他对象(比如说,可能是“秘密”功能、错误消息或高分警报)。此外,甚至可能希望从单独的文件或 URL 加载资源,从而减少初始下载时间或支持可互换的游戏内容。
You can use Resource Folders in your project to include content in your player build, which will make it available to load when needed, independently of the scenes that you build.
Resource Folders contain collections of assets that are included in the built Unity player even when they are not directly referenced from any Scene included the Build.
To put assets into a Resource Folder, create a new folder in the Project window, and name the folder “Resources”. You can have multiple Resource Folders located at different subfolders within your Assets folder, and packages may also contain Resources folders. You can then place assets into that folder in the same way as any other folder in the Project window. Whenever you want to load an asset from one of these folders, call Resources.Load().
Note: All assets found in the Resources folders and their dependencies are stored in a file in the build output called resources.assets. If a scene in the build references an asset then that asset is serialized into a sharedAssets*.assets file instead.
Only assets that are in the Resources folder can be accessed through Resources.Load(). However many more assets might end up in the resources.assets file since they are dependencies. For example a Material in the Resources folder might reference a Texture outside of the Resources folder. In that case the Texture is also included in the resources.assets file, but it is not available to load directly.
If you want to destroy scene objects that were loaded using Resources.Load() prior to loading another scene, call Object.Destroy() on them. To release assets and reclaim memory, use Resources.UnloadUnusedAssets().
The Resources system is convenient to use, especially for rapid prototyping and small projects. But it does not scale well and overall use of this feature is discouraged. For this reason AssetBundles and the Addressables package are the recommended alternative.
Some downsides of using Resources:
The resources folder can be appropriate for small Assets that are required throughout the project’s lifetime, that do not require updates, and do not vary across platforms or devices. Resource assets might be part of the minimal bootstrapping for a game, with the main content downloaded on-demand with AssetBundles. But local AssetBundles located in the StreamingAssets folder could also serve that bootstrapping need.