在单个进度条中下载多个 unity3d assetbundle?

Multiple unity3d assetbundle download in a single progress bar?

我正在尝试为多个资产包制作一个下载进度条。所有 assetbundle 的总大小是通过添加它的 webRequest.GetResponseHeader("Content-Length") 来计算的。但是www.downloadProgressreturns一个从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;

    }
}

不要通过不同的请求获取内容大小而让自己变得如此困难。您只需要使用 unity 中的 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;
    }
}