I’m trying to download assets from my AWS S3 bucket. So I’m using this script:
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.AddressableAssets.ResourceLocators;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.ResourceLocations;
public class AssetLoader : MonoBehaviour
{
public string catalogPath;
public string myLabel;
public List<IResourceLocation> locations;
public void initAddressables()
{
uiManager.loading.SetActive(true);
AsyncOperationHandle<IResourceLocator> handle = Addressables.InitializeAsync();
handle.Completed += initDone;
}
private void initDone(AsyncOperationHandle<IResourceLocator> obj)
{
if (obj.Status == AsyncOperationStatus.Succeeded)
{
LoadCatalog();
}
}
void LoadCatalog()
{
AsyncOperationHandle<IResourceLocator> handle;
handle = new AsyncOperationHandle<IResourceLocator>();
handle = Addressables.LoadContentCatalogAsync(catalogPath);
handle.Completed += LoadCatalogsCompleted;
}
void LoadCatalogsCompleted(AsyncOperationHandle<IResourceLocator> obj)
{
if (obj.Status == AsyncOperationStatus.Succeeded)
{
LoadResourceLocation();
}
else
{
Debug.LogError("LoadCatalogsCompleted is failed");
}
}
void LoadResourceLocation()
{
AsyncOperationHandle<IList<IResourceLocation>> handle = Addressables.LoadResourceLocationsAsync(myLabel);
handle.Completed += LocationsLoaded;
}
void LocationsLoaded(AsyncOperationHandle<IList<IResourceLocation>> obj)
{
if (obj.Status == AsyncOperationStatus.Succeeded)
{
locations = new List<IResourceLocation>(obj.Result);
LoadDependency();
}
else
{
Debug.LogError("locationsLoaded is failed");
}
}
void LoadDependency()
{
AsyncOperationHandle handle = Addressables.DownloadDependenciesAsync(myLabel);
handle.Completed += DependencyLoaded;
}
void DependencyLoaded(AsyncOperationHandle obj)
{
if (obj.Status == AsyncOperationStatus.Succeeded)
{
LoadAssets();
}
else
{
Debug.LogError("dependencyLoaded is Failed");
}
}
private void LoadAssets()
{
AsyncOperationHandle<IList<GameObject>> handle = Addressables.LoadAssetsAsync<GameObject>(locations, OnAssetsCategoryLoaded);
handle.Completed += OnAssetsLoaded;
}
private void OnAssetsCategoryLoaded(GameObject obj)
{
SpawnItem(obj.name);
}
private void OnAssetsLoaded(AsyncOperationHandle<IList<GameObject>> obj)
{
}
void SpawnItem(string addressableKey)
{
if (!instantiated)
{
AsyncOperationHandle<GameObject> asyncLoad = Addressables.InstantiateAsync(myLabel, Vector3.zero, Quaternion.identity);
StartCoroutine(progressAsync(asyncLoad));
asyncLoad.Completed += AssetSpawned;
instantiated = true;
}
}
private System.Collections.IEnumerator progressAsync(AsyncOperationHandle<GameObject> asyncOperation)
{
float percentLoaded = asyncOperation.PercentComplete;
while (!asyncOperation.IsDone)
{
Debug.Log("Progress = " + percentLoaded + "%");
yield return 0;
}
}
void AssetSpawned(AsyncOperationHandle<GameObject> obj)
{
Debug.Log("Asset Spawned");
}
The public method “initAddressables()” is called when I press a button.
Everything works fine at the first time. But when I try to load the same asset another time (without closing the application) it returns me this error:
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: startIndex
And it happens when the application runs this line:
handle = Addressables.LoadContentCatalogAsync(catalogPath);
Is there a way to “reset” the addressables? Or something like that?