Reputation: 365
I am trying to download a prefab using the Unity Addressable system. The address is located on a remote server and I have my addressable system set to pull from that remote server. The code below loads that asset from the server and is supposed to report its download progress. However, this doesn't seem to work. The UpdateProgressBar method only gets called once and then never again. As far as I know, Coroutines are supposed to run as long as they have something to do, so I assumed using the while loop would cause the coroutine to continue to call the UpdateProgressBar method.
I looked around on the internet and it does seem like people have had similar issues with getting download progress from AsyncOperationHandles, but most of those issues were a few years old, so I'd assume they'd be fixed by now.
Anyways, is there anything I'm missing/doing wrong? I'm pretty new to Unity Addressables, so any tips or constructive criticism would be most welcome.
public IEnumerator DownloadAsset(string assetKey)
{
loadingScreen.SetActive(true);
AsyncOperationHandle<GameObject> handle = Addressables.LoadAssetAsync<GameObject>(assetKey);
handle.Completed += (AyncOperationHandle) =>
{
DownloadComplete(handle);
loadingScreen.SetActive(false);
};
yield return handle;
DownloadStatus downloadStatus = handle.GetDownloadStatus();
while (!handle.IsDone && downloadStatus.Percent < 1)
{
UpdateProgressBar(downloadStatus.Percent);
yield return null;
}
}
private void DownloadComplete(AsyncOperationHandle goHandle)
{
Debug.Log("Asset Downloaded!");
GameObject obj = goHandle.Result as GameObject;
Instantiate(obj);
Addressables.Release(goHandle);
}
public void UpdateProgressBar(float progress)
{
progressBar.normalizedValue = progress;
Debug.Log(string.Format("Downloaded {0:P1}", progress));
if (progress >= 1.0f) loadingScreen.SetActive(false);
}
The DowloadAsset function is being called from another script when a button is clicked:
[SerializeField] private string assetKey;
void Start()
{
Button button = GetComponent<Button>();
button.onClick.AddListener(() => StartCoroutine(gameManager.DownloadAsset(assetKey)));
}
Upvotes: 1
Views: 4682
Reputation: 1
private IEnumerator ShowSizeText(string labelName)
{
AsyncOperationHandle<long> asynOperCheck = Addressables.GetDownloadSizeAsync(labelName);
yield return asynOperCheck;
if (asynOperCheck.Status == AsyncOperationStatus.Succeeded)
{
long DownloadSize = asynOperCheck.Result;
long size = (long)Math.Round(DownloadSize / 1000000f, 2);
long totalBundleSize = (long)size;
}
else if (asynOperCheck.Status == AsyncOperationStatus.Failed)
{
Debug.Log("Network Error");
}
}
Upvotes: 0
Reputation: 1
Try this
public IEnumerator DownloadAsset(string assetKey)
{
loadingScreen.SetActive(true);
var downloadAsync = Addressables.DownloadDependenciesAsync(assetKey);
downloadAsync.Completed += (AyncOperationHandle) =>
{
DownloadComplete(handle);
loadingScreen.SetActive(false);
};
yield return downloadAsync;
while (!downloadAsync.IsDone && downloadAsync.IsValid())
{
DownloadStatus downloadStatus = downloadAsync.GetDownloadStatus();
UpdateProgressBar(downloadStatus.Percent);
yield return null;
}
}
Upvotes: 0