Search Unity

UnityWebRequest continues downloading after play mode exit

Discussion in 'Multiplayer' started by Wattosan, Jun 3, 2019.

  1. Wattosan

    Wattosan

    Joined:
    Mar 22, 2013
    Posts:
    460
    Hey,

    I am using the UnityWebRequest class along with the DownloadHandlerFile to download large files (>100MB). When I start play mode, the files start downloading. If the files have not been completely downloaded and I exit play mode, they still keep downloading (file size increases in windows file explorer). If I close the Unity editor, the files are deleted (removeFileOnAbort is set to true) and download cancels.

    This seems to be a bug. Using Unity 2019.1.4f1.
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    No, UnityWebRequest can be used in both play mode and edit mode. You have to explicitly call Abort() to cancel it.
     
  3. Wattosan

    Wattosan

    Joined:
    Mar 22, 2013
    Posts:
    460
    The fact that it works in both modes is great but shouldn't it be canceled automatically when moving from one mode to another? It would be much more intuitive this way. It is not written anywhere that the download continues even if you move from one state to another. At some point after exiting play mode I was wondering why internet suddenly got so slow. Only then I discovered that it is still downloading the files. For some reason I believe that people assume the download cancels when they exit the play mode. I mean...why shouldn't it be so? Only for very specific cases I guess. But for the majority of cases, do people really care about download continuing when moving between these 2 states? Seems like a weird design decision.
     
    DavidZobrist likes this.
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    It's not a specific decision, rather it just happened as a result of supporting UWR in edit mode. It's single engine under the hood.
    You can submit a feature request to cancel all playmode request when exiting play mode, but that will only affect future Unity versions.
     
  5. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    It's a very good decision. Editor scripts would just mess up when entering playmode otherwise. If you want it to cancel, it's such minimal amount of work on your end to get that behaviour.
     
    Joe-Censored likes this.
  6. Wattosan

    Wattosan

    Joined:
    Mar 22, 2013
    Posts:
    460
    That might be true and I believe it is a very good feature but when you are not focused on executing those in edit mode then the fact that that the requests still download after exciting play mode does not feel intuitive at all. I was very surprised when I discovered they are still active upon exiting playmode. What is worst is that it happens in total silence. If you have no knowledge about it happening you can easily congest your bandwidth without realizing the cause behind it. I am making an assumption here but my guess is that the majority of the UnityWebRequest usage is done in play mode.
     
  7. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    It is also the case that most download are not large. You seem to have a specific issue with a very large download.
    Don't know the exact use case, but sounds like you need a better setup.
     
  8. Wattosan

    Wattosan

    Joined:
    Mar 22, 2013
    Posts:
    460
    The use case is that at run time I am downloading large files (videos) and saving them on disk (they need to be available when the user is offline as well). I am testing the downloading functionality inside Unity editor.

    You might be right that the usual case would be to stream in the videos and thus what I am doing is not very common. However, it still feels as if the action of stopping play mode should stop everything that happens at run time, including downloading files.
     
  9. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    You can do a feature request (report a bug, just word it "feature request") with a sample project indicating your problem. I'll consider distinguishing play-mode requests and aborting them upon exiting play mode.
     
    Wattosan likes this.
  10. Yazuak

    Yazuak

    Joined:
    Mar 27, 2013
    Posts:
    1
    Hi, sorry to resurrect this old thread, but I'm having the same issue currently with an API that streams endless data in nd-json format: https://lichess.org/api#operation/tvChannels and the specific api uri is https://lichess.org/api/tv/feed

    Could someone elaborate on how this can be done please? I also posted a question here: https://answers.unity.com/questions/1883209/how-to-abortunitywebrequest-when-exiting-play-mode.html
     
  11. DavidZobrist

    DavidZobrist

    Joined:
    Sep 3, 2017
    Posts:
    234
    I agree with @Wattosan that as unity dev. I expected the webrequest to abort when playmode is stopped. As everything else does. And its most likely how it behaves in the final build as well.

    But its an easy fix as mentioned by @TwoTen .

    @Yazuak
    Keep a reference to the request and do:

    Code (CSharp):
    1.    private void OnDisable()
    2.        _request?.Abort();
    3.        _request?.Dispose();
    4.     }

    Note:
    I actually would cache the allocation yourself. In the moment you do it.
    As the null check still sometimes resulted in an error on editor play mode closed.
    Code (CSharp):
    1.  _request = UnityWebRequest.Get(url);
    2.         canGetDisposed = true;
    Code (CSharp):
    1.   private void OnDisable()
    2.     {
    3.        Dispose();
    4.     }
    5.  
    6.     private void Dispose()
    7.     {
    8.         if (!canGetDisposed)
    9.         {
    10.             return;}
    11.  
    12.         canGetDisposed = false;
    13.        
    14.         Debug.Log("aborting download");
    15.         _request?.Abort();
    16.         _request?.Dispose();
    17.     }
     
    Last edited: Nov 24, 2022