When you start developing a game, handling all the assets used in your project, such as models, textures, objects, and music, can be fairly simple. Drag whatever you want to use into the interface, then link it to your object.

However, as your project grows, it can become much more challenging. When the game scenes get bigger, loading all the asset data needed for a specific level into memory is impossible using Unity's default approach. So, in this case, we use Unity Addressable to manage and load assets in Unity games.

About Unity Addressable

Unity Addressable is a powerful tool for effectively handling game assets. It allows game developers to load assets dynamically and asynchronously, reducing memory usage and load time. The Unity Addressable also enables developers to share updates and DLC content without rebuilding the entire game.

In this blog post, learn how to load assets from local and remote sources using Unity Addressables.

Why Use Asset Management?

We need to use an asset management system in Unity for two general reasons.

  1. To differentiate assets from the objects that use them, making it simpler to alter which asset is a specific object or project version.
  2. To organize asset content independently from the game's primary content. Either to distribute it differently (such as remotely) or to include/exclude assets in a specific version of the game.

Both of these issues are addressed by Unity's addressable object structure.

Work Mechanism of Asset Management

An addressable asset is detected using an Asset Reference, which is kept in an Asset Group rather than directly retrieving the asset file in a local folder.

An object can import an asset using its reference, but where the asset originates from relies on the settings of its group. Some groups, for instance, may be downloaded, while others are accessible locally. Groups can alter assets before passing them to an object. And complete groups can be included or excluded from specific game versions, simplifying localization and platform-specific modifications.

Prerequisites

You must have Unity 2019.4 LTS or later installed on your Unity Game Engine. It's also recommended to have familiarity with Unity's scripting language, C#.

Using Unity Addressable

Project Setup

First, we need to set up our project to use Addressable. In the Unity Editor, go to the Window menu and select Asset Management > Addressable. This will open the Addressable window. Click on the Groups tab and create a new group by clicking on the New Group button. You can name this group anything you like. For this example, we will call it MyAssetsGroup.

Adding Assets

Next, we need to add some assets to our group. In the Addressable window, click on the MyAssetsGroup group and then click on the Add Assets button. This will open a file dialogue where you can select the assets you want to add. For this example, we will add a texture and a prefab.

Loading Assets from Local Sources

Now that we have set up our group and added some assets, let's load them into our game. In this example, we will load the texture and prefab we added earlier.

To load the texture, we can use the following code.

public AssetReference textureReference;

private void Start()
{
    AsyncOperationHandle<Texture2D> textureHandle = Addressables.LoadAssetAsync<Texture2D>(textureReference);
    textureHandle.Completed += OnTextureLoaded;
}

private void OnTextureLoaded(AsyncOperationHandle<Texture2D> handle)
{
    Texture2D texture = handle.Result;
    // Do something with the texture...
}
Loading texture locally

In this code, we create an AssetReference variable that points to our texture asset. In the Start() method, we call Addressables.LoadAssetAsync() to load the texture asynchronously. We also attach a callback function to the completed event of the operation handle, which will be called when the texture is loaded.

When the texture is loaded, the OnTextureLoaded() function is called with the operation handle as a parameter. We can then get the loaded texture from the result property of the handle.

To load the prefab, we can use a similar code.

public AssetReference prefabReference;

private void Start()
{
    AsyncOperationHandle<GameObject> prefabHandle = Addressables.InstantiateAsync(prefabReference);
    prefabHandle.Completed += OnPrefabInstantiated;
}

private void OnPrefabInstantiated(AsyncOperationHandle<GameObject> handle)
{
    GameObject prefabInstance = handle.Result;
    // Do something with the instantiated prefab...
}
Loading prefab locally

In this code, we use Addressables.InstantiateAsync() to instantiate the prefab asynchronously. We attach a callback function to the completed event of the operation handle, which will be called when the prefab is instantiated.

When the prefab is instantiated, the OnPrefabInstantiated() function is called with the operation handle as a parameter. We can then get the instantiated prefab from the result property of the handle.

Loading Assets from Remote Sources

We now know how to load assets from local sources. Let's see how we can load assets from remote sources using Unity Addressable. In this example, we will load a texture from a remote URL.

To load the remote texture, we can use the following code.

public string remoteTextureURL;

private void Start()
{
    AssetReferenceTexture remoteTextureReference = new AssetReferenceTexture(remoteTextureURL);
    AsyncOperationHandle<Texture2D> textureHandle = Addressables.LoadAssetAsync<Texture2D>(remoteTextureReference);
    textureHandle.Completed += OnRemoteTextureLoaded;
}

private void OnRemoteTextureLoaded(AsyncOperationHandle<Texture2D> handle)
{
    Texture2D texture = handle.Result;
    // Do something with the texture...
}
Loading asset remotely

In this code, we create an AssetReferenceTexture variable that points to the remote texture asset. We pass the remote texture URL as a parameter to the constructor of the AssetReferenceTexture.

We then call Addressables.LoadAssetAsync() to load the remote texture asynchronously. We also attach a callback function to the completed event of the operation handle, which will be called when the texture is loaded.

When the texture is loaded, the OnRemoteTextureLoaded() function is called with the operation handle as a parameter. We can then get the loaded texture from the result property of the handle.

Conclusion

We can now efficiently manage our game assets by loading them from local and remote sources using Unity Addressable. It also improves game performance, reduces memory usage, and provides a better gameplay experience to the players.

Thanks for reading! Don't forget to subscribe and leave a comment below.