1

我正在尝试为多个资产包制作一个下载进度条。所有资产包的总大小是通过将其相加来计算的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;

    }
}
4

1 回答 1

1

不要通过使用不同的请求获取内容大小来让自己变得如此困难。您只需要使用统一的 0-1 值并将它们加在一起。从进度条查看它时,这不会产生差异,并且在实现 a** 时也不那么痛苦。我希望这有帮助。

//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;
    }
}
于 2017-09-02T18:05:29.827 回答