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.
Unity supports Resource Folders in the project to allow content to be supplied in the main game file yet not be loaded until requested. You can also create Asset Bundles. These are files completely separate from the main game file which contain assets to be accessed by the game on demand from a file or URL.
An Asset Bundle is an external collection of assets. You can have many Asset Bundles and therefore many different external collections of assets. These files exist outside of the built Unity player, usually sitting on a web server for end-users to access dynamically.
To build an Asset Bundle, you call BuildPipeline.BuildAssetBundles() from inside an Editor script. In the arguments, you specify an array of ObjectsThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
See in Glossary to be included in the built file, along with some other options. This will build a file that you can later load dynamically in the runtime by using AssetBundle.LoadAsset().
Resource Folders are collections of assets that are included in the built Unity player, but are not necessarily linked to any GameObject in the InspectorA Unity window that displays information about the currently selected GameObject, asset or project settings, allowing you to inspect and edit the values. More info
See in Glossary.
To put anything into a Resource Folder, you simply create a new folder inside the Project View, and name the folder “Resources”. You can have multiple Resource Folders organized differently in your Project. Whenever you want to load an asset from one of these folders, you call Resources.Load().
All assets found in the Resources folders and their dependenciesIn the context of the Package Manager, a dependency is a specific package version (expressed in the form
package_name@package_version) that a project or another package requires in order to work. Projects and packages use the dependencies attribute in their manifests to define the set of packages they require. For projects, these are considered direct dependencies; for packages, these are indirect, or transitive, dependencies. More info
See in Glossary are stored in a file called resources.assets. If an asset is already used by another level it is stored in the .sharedAssets file for that level. The Edit -> PlayerSettings First Streamed Level setting determines the level at which the resources.assets will be collected and included in the build.
If a level prior to “First streamed Level” is including an asset in a Resource folder, the asset will be stored in assets for that level. If it is included afterwards, the level will reference the asset from the “resources.assets” file.
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)
You can unload resources of an AssetBundle by calling AssetBundle.Unload() or AssetBundle.UnloadAsync(bool). If you pass true for the unloadAllLoadedObjects parameter, both the objects held internally by the AssetBundle and the ones loaded from the AssetBundle using AssetBundle.LoadAsset() will be destroyed and memory used by the bundle will be released.
Sometimes you may prefer to load an AssetBundle, instantiate the objects desired and release the memory used up by the bundle while keeping the objects around. The benefit is that you free up memory for other tasks, for instance loading another AssetBundle. In this scenario you would pass false as the parameter. After the bundle is destroyed you will not be able to load objects from it any more.