我正在尝试为多个assetbundle创建一个下载进度条。所有assetbundle的总大小是通过添加webRequest.GetResponseHeader("Content-Length")
来计算的。但www.downloadProgress
仅返回0到1之间的值。
以下是示例代码:
float progress = 0;
for (int i = 0; i < assetToDownload.Count; i++)
{
UnityWebRequest www = UnityWebRequest.GetAssetBundle(assetToDownload[i], 0, 0);
www.Send();
while (!www.isDone)
{
progress += www.downloadProgress * 100;
Debug.Log((progress / totalSize) * 100);
yield return null;
}
}
答案 0 :(得分:1)
不要通过使用不同的请求获取内容大小来使自己如此努力。您只需要使用统一的0-1值并将它们加在一起。当从进度条中查看它时,这不会产生差异,并且在实施中不会产生这样的痛苦。 我希望这会有所帮助。
//To calculate the percantage
float maxProgress = assetToDownload.Count;
for (int i = 0; i < assetToDownload.Count; i++)
{
UnityWebRequest www = UnityWebRequest.GetAssetBundle(assetToDownload[i], 0, 0);
www.Send();
//To remember the last progress
float lastProgress = progress;
while (!www.isDone)
{
//Calculate the current progress
progress = lastProgress + www.downloadProgress;
//Get a percentage
float progressPercentage = (progress / maxProgress) * 100;
}
}