The Resources
class allows you to find and access objects that are in a Resources
folder in your project.
Important: The Resources system is a performance-intensive way to organize assets in your project and isn’t recommended. Use AssetBundles and the Addressables package instead.
You can use the Resources system 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. When you add content to a folder named Resources
in your project, Unity makes it available to load when needed, independent of the scenes that you build. However, having the content constantly available at runtime has significant performance impact on your project.
It’s best practice not to use the Resources
system for the following reasons:
Resources
folder slows down application startup and the length of builds.Resources
system makes it harder to deliver custom content to specific platforms and prevents incremental content upgrades.Resources
folder requires a player rebuild and redeployment, whereas AssetBundles are better suited for incremental content updates.AssetBundles and the Addressables package are the recommended alternative.
Unity combines all assets and objects in the Resources
folder into a single serialized file when you build a project. This file contains metadata and indexing information, which includes a serialized lookup tree that Unity uses to resolve a given object’s name into its appropriate File GUID and Local ID. It’s also used to locate the object at a specific byte offset in the serialized file’s body.
On most platforms, the lookup data structure is a balanced search tree, which has a construction time that causes the index’s loading time to grow more-than-linearly as the number of objects in Resources
folders increases.
This operation is unskippable and happens at application startup time while the initial non-interactive splash screen is displayed. For example, initializing a Resources
system containing 10,000 assets takes several seconds on low-end mobile devices, even though most of the objects contained in Resources
folders are rarely needed to load into an application’s first scene.
The Resources
system can be helpful in the following situations:
Resources
folder when a project moves into full production.Examples of the latter include MonoBehaviour singletons used to host prefabsAn asset type that allows you to store a GameObject complete with components and properties. The prefab acts as a template from which you can create new object instances in the scene. More info
See in Glossary, or ScriptableObject instances containing third-party configuration data, such as a Facebook App ID.
To use the Resources system:
Resources
in your project, and add assets to it. Unity then makes these assets available even if they’re not directly referenced in a scene. Note: You can have multiple Resources
folders located at different subfolders within your Assets
folder, and packages can also contain Resources
foldersResources.Load
in your code. Only assets in the Resources
folder can be accessed in this way.Unity stores all assets in the Resources
folders and their dependencies in a file in the build output called resources.assets
. If a scene in the build references an asset Unity serializes that asset into a sharedAssets*.assets
file instead.
Additional assets might end up in the resources.assets
file if they’re 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 isn’t available to load directly.
If you want to destroy scene objects that were loaded using Resources.Load
before loading another scene, call Object.Destroy
on them. To release assets and reclaim memory, use Resources.UnloadUnusedAssets
.