在某些情况下,为项目提供资源而不将资源作为场景的一部分进行加载是很有用的做法。例如,可能存在可出现在游戏的任何场景中但不经常使用的角色或其他对象(比如说,可能是“秘密”功能、错误消息或高分警报)。此外,甚至可能希望从单独的文件或 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.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.