Skip to content

Instantly share code, notes, and snippets.

@MeowKim
Last active September 11, 2023 19:32
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save MeowKim/79796c1468872de6bf4b50f0bfb0ed41 to your computer and use it in GitHub Desktop.
Save MeowKim/79796c1468872de6bf4b50f0bfb0ed41 to your computer and use it in GitHub Desktop.
Guide for Unity Addressable Asset System.

πŸ“ About

Guide for Unity Addressable Asset System.
Check out this sample code repo.

This guide supports languages below.

Created: 2020-07-03
Last updated: 2020-08-26 minor typo correction

πŸ’Ώ Setup

Create new Unity project

1 create-new-project

To be simple, we would not create any models.
Create a project with Universal Render Pipeline template to use sample models.

Install Adressables

2-1 select-package-manager

2-2 install-addressables

Select Windows > Package Manager.
Search Addressables & install it.

πŸ“¦ Addressables

Create Addressables Settings

3-1 select-addressables-groups

3-2 create-addressable-settings

Select Windows > Asset Management > Addressables > Groups.
Click Create Addressables Settings.

Create Groups

4 create-groups

We would categorize assets into Materials, Models, Prefabs, Shaders, Textures like Assets\ExampleAssets folder.
To do this, select Create > Group > Packed Assets on Addressable Groups window and rename it.

Make Assets As Addressable

5-1 move-assets-to-addressable-group

5-2 simplify-addressable-names

From Assets\ExampleAssets folder, move assets into proper group on Addressable Groups window.
To shorten the name choose assets, right-click and select Simplify Addressable Names.

Add Labels

6-1 create-labels

6-2 add-labels

Select Tools > Labels.
Create labels according to groups we produced at previous step.
Add proper label to each Addressables.

Remove Existing Objects

7 remove-existing-objects

Remove Workshop Set & Props under Example Assets GameObject from the scene.

πŸ“œ Scripts

Add following 2 classes.

AddressablesLoader.cs

using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.AddressableAssets;

public static class AddressablesLoader
{
	public static async Task InitAssets<T>(string label, List<T> createdObjs, Transform parent)
		where T : Object
	{
		var locations = await Addressables.LoadResourceLocationsAsync(label).Task;

		foreach (var location in locations)
		{
			createdObjs.Add(await Addressables.InstantiateAsync(location, parent).Task as T);
		}
	}
}

AddressablesController.cs

Make sure that this monobehaviour script should be attached to any of GameObjects.
We already have Example Assets GameObject, so let's use this.

using System.Collections.Generic;
using UnityEngine;

public class AddressablesController : MonoBehaviour
{
	[SerializeField]
	private string _label;
	private Transform _parent;
	private List<GameObject> _createdObjs { get; } = new List<GameObject>();

	private void Start()
	{
		_parent = GameObject.Find("Example Assets").transform;
		Instantiate();
	}

	private async void Instantiate()
	{
		await AddressablesLoader.InitAssets(_label, _createdObjs, _parent);
	}
}

Specify label to be loaded

8 specify-label-to-be-loaded

Fill the Label field as Prefabs on the inspector.

Check out

9 check-out

Click Play button and check out how is it going.

🚧 Build

Play Mode Script

10 play-mode-script

There are 3 modes to use addrssables on play mode.
To check out works on production environment, change Play Mode Script to Use Existing Build (requires built groups).

Build addressables

11-1 build-addressables

11-2 files-built

Now we need to build our addressables.
To do this, select Build > New Build > Default Build Script.
You could find files built in Assets\AddressableAssetsData\Windows folder.

Check out

12 requires-build

Click Play button and check out how is it going.

Without build, you'll get the error message.

☁️ Remote Build

We could store & load built files from the server.
Here I would go with AWS S3.

In this guide, I'll not explain about setup for cloud service.
Check out this reference if you need.

Create Profile

13-1 manage-profiles

13-2 create-profile

13-3 set-active

Select Profile:Default > Manage Profiles.
In Addressable Profiles window, select Create > Profile.
Rename created profile as you want.
On created profile, replace RemoteLoadPath value from http://localhost to your bucket base url.
Right click on created profile and select Set Active.

Enable Remote Catalog

14-1 inspect-system-settings

14-2 enable-remote-catalog

Select Tools > Inspect system Settings.
On the inspector, check the box for Build Remote Catalog.
Set Build Path to RemoteBuildPath.
Also set Load Path to RemoteLoadPath.

Set Remote Group

15-1 inspect-group-settings

15-2 set-group-paths

Right click on Prefabs group and select Inspect Group Settings.
On the inspector, set Build Path to RemoteBuildPath.
Also set Load Path to RemoteLoadPath.

Build for remote

16 files-built-for-remote

As we did before, select Build > New Build > Default Build Script.
You could find files built for remote in ServerData\StandaloneWindows folder.
The files should be .json, .hash and .bundle.

Upload

17-1 errors-without-uploading

17-2 upload

17-3 grant-public-read

Running at this time would cause errors because Unity tries to get remote groups from the server.
So let's upload our files.
On AWS Console S3 Service, Go into your bucket root. Upload StandaloneWindows folder to your bucket.
Click Next button to go to Set permissons Section and Set Manage public permissions to Grant public read access to object(s).
Click Upload button to finish.

πŸ”§ Updating Assets

Make change

18 make-change

Make any changes on your asset.
Here I changed the paint supply's material to something else.

Update Previous Build

19-1 update-previous-build

19-2 select-bin-file

To update assets, select Build > Update a Previous Build.
Select addressables_content_state.bin file in Assets\AddressableAssetsData\Windows\ folder.

Upload files changed

20 upload-files-changed

Upload files changed (.json, .hash, .bundle) into your bucket's StandaloneWindows folder.
Don't forget to grant public read permissions as we did before.

πŸŽ‰ Tada!

21 tada

Now you can store & load assets (which's load path is configured to remote) from the server.
Also You don't need to create a new build for updating assets anymore. πŸ˜„

πŸ‘€ References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment