Search Unity

UnityWebRequest very slow on Android when Internet is available

Discussion in 'Android' started by allangamesxbr, Jun 11, 2019.

  1. allangamesxbr

    allangamesxbr

    Joined:
    Sep 9, 2015
    Posts:
    25
    Hi everybody,

    I am facing a weird problem using UnityWebRequest on Android. In my test I am loading a text file stored locally with only 5kb, but this task takes more than 10 seconds when the internet is available (wi-fi). When I let the device on airplane mode (with wi-fi disabled), the text file is loaded instantly.

    Is there some workaround to it on Unity 2018.2.5f1?

    Note: I intend to use the current version of Unity soon, but I really need to have it working properly today and an update can fix this specific problem but also bring new problems, so it should be done with enough time.

    Note 2: I know that I can use StreamReader to figure out it - and it worked in my tests -, but I also need to load MP3 files, and this workaround doesn't work in this case, so I really need to use UnityWebRequest working properly.
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    Can you show the code you use to load that file?
     
  3. allangamesxbr

    allangamesxbr

    Joined:
    Sep 9, 2015
    Posts:
    25
    Sure! I tried it with 3 differents codes and they took the same time and the same behaviour (with and without internet) described previously. The first one and the second are essentially the same and the third code I believe is a wrapper on top of UnityWebRequest.

    Code 1:
    Code (CSharp):
    1. string result;
    2.             string uri = "file:///" + Application.persistentDataPath + "/data/file.txt";
    3.             using (UnityWebRequest myFile = UnityWebRequest.Get(uri)) {
    4.                 yield return myFile.SendWebRequest();
    5.  
    6.                 if (myFile.isNetworkError) {
    7.                     Debug.Log("Error: " + myFile.error);
    8.                 } else {
    9.                     result = myFile.downloadHandler.text;
    10.                 }
    11.             }
    Code 2:
    Code (CSharp):
    1. string result;
    2.             string uri = "file:///" + Application.persistentDataPath + "/data/file.txt";
    3.  
    4.             UnityWebRequest myFile = UnityWebRequest.Get(uri);
    5.             yield return myFile.SendWebRequest();
    6.  
    7.             if (myFile.isNetworkError || myFile.isHttpError) {
    8.                 Debug.Log(myFile.error);
    9.             } else {
    10.                 result = myFile.downloadHandler.text;
    11.             }
    Code 3:
    Code (CSharp):
    1. string result;
    2. WWW myFile = new WWW("file:///" + Application.persistentDataPath + "/data/file.txt");
    3.             while (!myFile.isDone) {
    4.                 yield return null;
    5.             }
    6.             result = myFile.text;
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    Somewhat blind guess, but three slashes after file: are incorrect on Android, there should be only two, because Application.persistentDataPath is a UNIX path that already has a slash at the beginning. Could be that by seeing four slashes the backend "thinks" you are trying to access a network share and then falls back to local file read.
     
    allangamesxbr likes this.
  5. allangamesxbr

    allangamesxbr

    Joined:
    Sep 9, 2015
    Posts:
    25
    I finally figure out this issue.
    Thanks for warning me about the four slashes, in the truth it is not causing the problem, but of course should be fixed too.

    After your last reply I created a new project and I noticed that it worked perfectly. After some hours trying everything possible I discovered that the previous scene was affecting the scene where the problem happens. In the previous scene my game loads a rank with 50 profile pictures from Facebook, and if you go to the next scene without it is complete, the problem happens.

    In the old 4.7 version I believe unity aborts automatically these requests, but in my current version (2018.2.5f1) it waits until the request is completed, even when you are in another scene. It is a good behaviour in some conditions, but cause this problem that I described.

    I am fixing the code yet, but UnityWebRequest.Abort should do the work in the previous scene and fix this problem.
     
  6. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    In Unity versions prior to 2019.2 there is a limitation on how many threads can run concurrently (16 or 32, don't remember exactly), if you submit more, the additional requests will wait for other requests to complete.
    Since 2019.2 this limitation has been removed, but the requests still may impact each others performance.
    Requests are not automatically aborted, they by design work across scene loads.
     
    allangamesxbr likes this.