Search code examples
c#unity-game-engineprogress-barassetbundle

Multiple unity3d assetbundle download in a single progress bar?


I'm trying to make a single download progress bar for multiple assetbundle. The total size of all the assetbundle was computed by adding its webRequest.GetResponseHeader("Content-Length"). But the www.downloadProgress returns a value from 0 to 1 only.

Here's the sample code:

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;

    }
}

Solution

  • Do not make it yourself so hard by getting the content-size with a diffrent request. You just need to use the 0-1 values from unity and add them together. This won't make a diffrence when viewing it from a progressbar and isn't such a pain in the a** to implement. I hope this helps.

    //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;
        }
    }