我正在尝试使用系统下载预制件。地址位于远程服务器上,我的可寻址系统设置为从该远程服务器提取。下面的代码从服务器加载该资产,并报告其下载进度。然而,这似乎不起作用。UpdateProgressBar方法只被调用一次,然后永远不会被调用。据我所知,只要有一些事情要做,就应该运行Coroutines,所以我假设使用while循环会导致coroutine继续调用UpdateProgressBar方法。
我在互联网上环顾四周,似乎人们在从AsyncOperationHandles获得下载进度时遇到了类似的问题,但大多数问题都是几年前出现的,所以我认为现在已经解决了。
总之,我有什么遗漏/做错了什么吗?我对Unity非常陌生,所以任何建议或建设性的批评都将是最受欢迎的。
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);
}单击按钮时,将从另一个脚本调用DowloadAsset函数:
[SerializeField] private string assetKey;
void Start()
{
Button button = GetComponent<Button>();
button.onClick.AddListener(() => StartCoroutine(gameManager.DownloadAsset(assetKey)));
}发布于 2022-08-15 14:20:02
尝尝这个
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;
}
}发布于 2022-10-25 08:38:03
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");
}
}https://stackoverflow.com/questions/70885414
复制相似问题