Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

UnityWebRequest.downloadProgress skips end

Discussion in 'Connected Games' started by Aeonvirtual, Sep 8, 2018.

  1. Aeonvirtual

    Aeonvirtual

    Joined:
    Oct 14, 2015
    Posts:
    28
    Hi All,

    I'm trying to make a fancy loading wheel for my application and it works but it skips the end because downloadProgress skips its end

    The end of download progress goes something like this:
    ....85%86%87%89% Done!
    or this..
    0% 21% 43% 50% Done!

    I figure it might have something to do with the way I serve up files so here is the PHP code that is serving up these files:
    Code (CSharp):
    1.  
    2. header('Content-Description: Asset Bundle Transfer');
    3. header('Content-Type: application/octet-stream');
    4. header("Content-Disposition: attachment; filename*=UTF-8''".rawurlencode($name));
    5. header('Content-Transfer-Encoding: binary');
    6. header('Expires: 0');
    7. header('Cache-Control: must-revalidate');
    8. header('Pragma: public');
    9. header('Content-Length: ' . filesize($filePath));
    10.    
    11. // echo file_get_contents($filePath);        // tried with this, same issue
    12.                
    13. // read in chunks so we don't get a memory error          
    14. $chunkSize = 1024 * 1024;
    15. $handle = fopen($filePath, 'rb');
    16. while (!feof($handle))
    17. {
    18.       $buffer = fread($handle, $chunkSize);
    19.        echo $buffer;
    20.        ob_flush();
    21.        flush();
    22. }
    23. fclose($handle);
    One thing that is suspicious is that if I use my browser to make this request, the Content-Length header is suspiciously missing, with both the chunked, and the naive approach.

    For completeness sake, here is my unity code:
    Code (CSharp):
    1. uwr.SendRequest();
    2. while(!uwr.isDone) {
    3. Debug.Log(uwr.downloadProgress);
    4. yield return waitFrame;
    5. }
    Has anyone else encountered this problem?
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,599
    Try outputing uwr.bytesDownloaded besides downloadProgress.
    Also, what DownloadHandler are you using?
     
  3. Aeonvirtual

    Aeonvirtual

    Joined:
    Oct 14, 2015
    Posts:
    28
    The code is in a using (UnityWebRequest uwr = UnityWebRequestAssetBundle.GetAssetBundle(url)){
    block and I don't specify a downloadhandler.

    I checked downloaded bytes and it reports exactly the same number of bytes as the file, however, when it reaches this number, downloadprogress is at 0.88
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,599
    The helper utility you call attaches DownloadHandlerAssetBundle.
    As for progress, this is specific to AssetBundles, the progress attempts to indicate the progress of the whole operation, which is download + AssetBundle load.
     
  5. Aeonvirtual

    Aeonvirtual

    Joined:
    Oct 14, 2015
    Posts:
    28
    so why does it skip the end then? Seems like a bug to me. It would make more sense for the downloadprogress to be 1 when the loop ends.
     
  6. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,599
    That end indicates how much of work is left to load AssetBundle. After you take AssetBundle out from DownloadHandler, progress will be 1. I think this is a bit of legacy from old WWW class.
     
  7. Aeonvirtual

    Aeonvirtual

    Joined:
    Oct 14, 2015
    Posts:
    28
    Well, do you know how I could make a loading bar then that smoothly goes all the way to the end, because I can't use downloadprogress because that smoothly goes to 90%, or 70%, or 50% and then skips to the end.
     
  8. Aeonvirtual

    Aeonvirtual

    Joined:
    Oct 14, 2015
    Posts:
    28
    So, just to make sure, is the current behavior correct? Or should I file a bug report?
     
  9. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,599
    It is considered correct.
     
  10. Aeonvirtual

    Aeonvirtual

    Joined:
    Oct 14, 2015
    Posts:
    28
    Ok, so you can confirm then that there is no way to make a progress bar that smoothly goes to the end? It will always smoothly go to 70 or 80% and then skip? I'm not trying to be an ass, just wondering what I'm doing wrong.
     
  11. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,599
    I don't know whether the actual figure are correct, but UnityWebRequest by design reports progress from AssetBundle system, so it's progress is equal to the progress of underlying AssetBundle load operation.
     
  12. FlorianBernard

    FlorianBernard

    Joined:
    Jun 9, 2015
    Posts:
    107
    Hey @Aeonvirtual , can I ask you how/where you host your files?

    I'm trying to use AppEngine and PHP but I can not figure out how to get download progress. I just don't get any, like if Content-size was missing from the response.

    Code (CSharp):
    1. using (UnityWebRequest uwr = UnityWebRequestAssetBundle.GetAssetBundle("https://myurl/mybundle"))
    2.         {
    3.             yield return uwr.SendWebRequest();
    4.  
    5.             if (uwr.isNetworkError || uwr.isHttpError)
    6.             {
    7.                 Debug.Log(uwr.error);
    8.             }
    9.             else
    10.             {
    11.                 while(!uwr.isDone)
    12.                     Debug.Log(uwr.downloadProgress);
    13.             }
    14. }
    My code yield on return uwr.SendWebRequest() until the file is loaded and uwr.isDone returns true.

    Any help would be much appreciated!
     
  13. andrew_pearce_

    andrew_pearce_

    Joined:
    Nov 5, 2018
    Posts:
    78
    After couple of days searching and wasting time implementing my own download handler, I found solution:

    Code (CSharp):
    1.     using (UnityWebRequest uwr = UnityWebRequestAssetBundle.GetAssetBundle(uri, hash)) {
    2.       uwr.SendWebRequest();
    3.  
    4.       int bundleSize = 0;
    5.       if (!Caching.IsVersionCached(uri, hash)) {
    6.         using (UnityWebRequest request = UnityWebRequest.Head(uri)) {
    7.           yield return request.SendWebRequest();
    8.           bundleSize = Int32.Parse(request.GetResponseHeader("Content-Length"));
    9.         }
    10.       }
    11.  
    12.       while (!uwr.isDone) {
    13.         bundleData.progress = (bundleSize != 0) ? (uwr.downloadedBytes / (float)bundleSize) : uwr.downloadProgress;
    14.         yield return new WaitForEndOfFrame();
    15.       }
    16.     }
    Since UnityWebRequestAssetBundle is sealed (for no reason) we cannot override CompleteContent method to get bundle size. So I decided to send head request and get bundle size. The size will help us calculate actual download progress (because downloaded byes property hold true information).

    IMPORTANT make sure that you do not do yield return uwr.SendWebRequest() as official documentation demonstrates in samples (the issue which @FlorianBernard has) because that will paused coroutine until download is fully complete.

    Also we request bundle size only after download process was started. That way we do not waste time... the download is going by its own while we are getting bundle size from server. Lastly, we do head request, ONLY if current bundle does not exist in cache.

    I hope this will save your time.
     
    FlorianBernard likes this.
  14. FlorianBernard

    FlorianBernard

    Joined:
    Jun 9, 2015
    Posts:
    107
    Looks like a great workaround Andrew. Thank you so much for sharing!
     
  15. ChrisLau

    ChrisLau

    Joined:
    Jul 30, 2018
    Posts:
    1
    What is bundleData.progress for??
     
  16. andrew_pearce_

    andrew_pearce_

    Joined:
    Nov 5, 2018
    Posts:
    78
    The bundleData is the object which I pass by reference. It includes the URL of the bundle and the name of asset which needs to be loaded. It also contains the reference to loaded bundle and the list of assets loaded (so I can unload bundle automatically, if no assets reference it).

    So progress is just a public property which holds the download progress value (I have another progress for loading single asset as well but practically it's 0 and then 100%). Since the whole process of downloading/loading is done in the coroutine, this property is the way to send information (progress) back to main logic.

    You can do it simpler. Just update the loading progress bar from coroutine (you can send it by reference to downloader coroutine). If you have any other question, feel free to ask. Good luck!
     
    Last edited: Jul 28, 2020
unityunity