Helper class for Unity's Addressables Asset System.
Refer this link for installation guide: https://docs.unity3d.com/Manual/upm-ui-giturl.html
- Load addressable asset using address.
private IEnumerator LoadAsset() {
GameObject gamePrefab;
yield return AddressablesHelper.LoadAssetByAddress<GameObject>("GamePrefabs/Game1.prefab", gameObj => gamePrefab = gameObj);
}
- Load multiple addressable assets using addresses.
private IEnumerator LoadAssets() {
var assetAddresses = new[] {"GamePrefabs/Game1.prefab", "GamePrefabs/Game2.prefab", "GamePrefabs/Game3.prefab"};
List<GameObject> gamePrefabs;
yield return AddressablesHelper.LoadAssetsByAddress<GameObject>(assetAddresses, gameObjCollections => gamePrefabs = gameObjCollections.ToList());
}
- Load multiple addressable assets using asset label.
private IEnumerator LoadAssets() {
var assetLabels = "GamePrefabs";
List<GameObject> gamePrefabs;
yield return AddressablesHelper.LoadAssetsByLabel<GameObject>(assetLabels, gameObjCollections => gamePrefabs = gameObjCollections.ToList());
}
- Load multiple addressable assets using multiple labels.
private IEnumerator LoadAssets() {
var assetLabels = new[] {"GamePrefabs", "Prefabs"};
List<GameObject> gamePrefabs;
yield return AddressablesHelper.LoadAssetsByLabels<GameObject>(assetLabels, gameObjCollections => gamePrefabs = gameObjCollections.ToList());
}
To use the following functions you must initialize the addressables helper class with asset label & asset type as shown below. This is an async task to initialize the class on app/game start.
private void InitAddressablesHelper() {
var labelsAndTypeDict = new Dictionary<string, Type> { // {AssetLabel , TypeOfAsset}
{"GamePrefabs", typeof(GameObject)},
{"GameButtons", typeof(GameObject)}, // Multiple labels for same type of asset.
{"Sprites", typeof(Sprite)},
{"SpriteAtlas", typeof(SpriteAtlas)},
{"Materials", typeof(Material)},
{"Json", typeof(TextAsset)}
};
AddressablesHelper.Init(labelsAndTypeDict);
}
Before using the following functions for first time, please ensure that the addressables helper is initialized using,
yield return AddressablesHelper.WaitForInit;
private IEnumerator LoadAssets() {
GameObject gamePrefab;
yield return AddressablesHelper.Instance.LoadAsset<GameObject>("Game1", gameObj => gamePrefab = gameObj);
}
Note: No need to pass the full asset address, matches the given string to existing asset addresses.
private IEnumerator LoadAssets() {
var gameObjectNames = new[] {"Game1", "Game2"};
List<GameObject> gamePrefabs;
yield return AddressablesHelper.Instance.LoadAssets<GameObject>(gameObjectNames, gameObjCollections => gamePrefabs = gameObjCollections.ToList());
}
Note: No need to pass the full asset addresses, matches the given string to existing asset addresses.
All assets loaded using this class are unloaded on when a scene unloads, to prevent this pass doNotDestroyOnLoad parameter as true. This is false by default.
private IEnumerator LoadAssets() {
var gameObjectNames = new[] {"Game1", "Game2"};
List<GameObject> gamePrefabs;
yield return AddressablesHelper.Instance.LoadAssets<GameObject>(gameObjectNames, gameObjCollections => gamePrefabs = gameObjCollections.ToList(), true); // doNotDestroyOnLoad = true
}
private void Start() {
InitAddressablesHelper();
StartCoroutine(LoadAssets());
}
private void InitAddressablesHelper() {
var labelsAndTypeDict = new Dictionary<string, Type> {
{"GamePrefabs", typeof(GameObject)},
{"Sprites", typeof(Sprite)},
{"SpriteAtlas", typeof(SpriteAtlas)},
{"Json", typeof(TextAsset)},
{"Materials", typeof(Material)}
};
AddressablesHelper.Init(labelsAndTypeDict);
}
private IEnumerator LoadAssets() {
yield return AddressablesHelper.WaitForInit; // Waits until addressables helper is initialized.
var gameObjectNames = new[] {"Game1", "Game2"};
List<GameObject> gamePrefabs;
yield return AddressablesHelper.Instance.LoadAssets<GameObject>(gameObjectNames, gameObjCollections => gamePrefabs = gameObjCollections.ToList());
}