Getting started with Addressable Assets
Installing the Addressable Assets package
Requires Unity 2018.2 or later.
To install this package, follow the instructions in the Package Manager documentation.
Marking Assets as addressable
There two ways to mark an item as an Addressable Asset. Once the Addressable Assets package is installed, you can mark an Asset as addressable in the Inspector window or drag it into the Addressables window.
In the Inspector window for the item, click the Address checkbox and enter a name to identify the Asset.
Open the Addressables window by clicking Window > Addressable Assets.
Drag the item from the Project window’s Asset Folder into the Addressables window’s Asset tab.
When you first start using Addressable Assets, the system saves some edit-time and run-time data Assets for your Project in Assets/AddressableAssetsData which should be added to your version control check in..
Loading or instantiating by address
You can load or instantiate an Asset at run-time. Loading an Asset loads all dependencies into memory (including Asset bundle data if relevant). This allows you to use the Asset when you need to. Instantiating loads the Asset, and then immediately adds it to the scene.
The default address for an Asset is the path of the Asset. You can change the address to any unique name.
To access an Asset in script using a string address:
Using the AssetReference Class
The AssetReference class provides a mechanism to access Assets without the need to know string (or other) addresses.
To access an Addressable Asset using the *AssetReference *class:
- Select an Asset.
- In the Inspector, click the Add Component button and then select the component type.
Add a public AssetReference object in the component. For example:
public AssetReference explosion;
In the Inspector, set which Asset the object is linked to by either dragging an Asset from the Project window onto the entry or clicking the entry to choose from previously defined addressable Assets (shown below).
Begin typing the AssetReference name for easy filtering.
Loading an Addressable Asset by object reference
To load an
AssetReference, call one of the methods defined on it. For example:
Downloading in Advance
Addressables.PreloadDependencies() method loads the dependencies for the address or label that you pass in. Typically, this is the asset bundle.
IAsyncOperation object returned by this call includes a
PercentComplete attribute that you can use to monitor progress of downloads. You can use the percent complete to display a progress bar and have the app wait until the content has loaded.
While it can be advantageous to download assets for your app in advance, there are instances where you might choose not do so. For example:
If your app has a large amount of online content, and you generally expect users to only ever interact with a portion of it.
You have an app that must be connected online to function. For example, a multiplayer game. If all the content for the app is in small bundles, you might choose to wait and download content as-needed.
You can also choose to partially use the preload functionality. Rather than using the percent complete to wait until the content is loaded, you can start the download then continue on. You would need to create a loading/waiting screen later on when you actually load the asset. If the preload has finished, the load is quick, otherwise the app would need to wait until the content is loaded.