Version: 2023.1
Language : English
Scripting with Assets
Streaming Assets

Loading Resources at Runtime

In some situations, it is useful to make an asset available to a project without loading it in as part of a sceneA Scene contains the environments and menus of your game. Think of each unique Scene file as a unique level. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. More info
See in Glossary
. For example, there may be a character or other object that can appear in any scene of the game but which will only be used infrequently (this might be a “secret” feature, an error message or a highscore alert, say). Furthermore, you may even want to load assets from a separate file or URL to reduce initial download time or allow for interchangeable game content.

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

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 windowA window that shows the contents of your Assets folder (Project tab) More info
See in Glossary
, 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.

Resource Unloading

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().

Limitations and Alternatives

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:

  • Placing a lot of content in this folder will slow down application startup and the length of builds.
  • The Resource folder is not appropriate for delivering custom content for specific platforms.
  • Making changes to Assets in the Resource folder requires a player rebuild and redeployment, whereas AssetBundles are better suited for incremental content updates.

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.

Scripting with Assets
Streaming Assets
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961