Pankti Shah
Pankti Shah

Reputation: 387

Unity Download Problem WWW and UnityWebRequest

I am trying to download an asset bundle using a URL following is the code I am using:

WWW www = new WWW(assetsFilepath);

while (!www.isDone)
{
    Debug.Log("Downloading asset: " + www.progress);
    yield return null;
}

yield return www;

if (www.error == null)               
{                    
    Debug.Log("No Error");
    string tempPath = Path.Combine(Application.persistentDataPath, assetsFilename);
    FileStream cache = new FileStream(path, FileMode.Create);
    cache.Write(www.bytes, 0, www.bytes.Length);
    cache.Close();
}                
else
{
    Debug.Log(www.error);
}

Log Output: Downloading asset: 0

I am aware that WWW request is obsolete, so I tried the following:

UnityWebRequest www = UnityWebRequestAssetBundle.GetAssetBundle(assetsFilepath);

while (!www.isDone)
{
    Debug.Log("Downloading asset: " + www.downloadProgress);
    yield return null;
}

yield return www.SendWebRequest();
if (www.error == null)
{
    Debug.Log("No Error");
    string tempPath = Path.Combine(Application.persistentDataPath, assetsFilename);
    FileStream cache = new FileStream(path, FileMode.Create);
    cache.Write(www.downloadHandler.data, 0, www.downloadHandler.data.Length);
    cache.Close();
}
else
{
    Debug.Log(www.error);
}

Log Output : Downloading asset: 0

Unity: 2018.3.8f

Write Permissions in Player Settings: External SD Card

External Read and Write Permissions are granted

This is the first time I am using UnityWebRequest, I am unable to find why I am getting this every time. Am I missing any step? Or any setting?

Upvotes: 1

Views: 4814

Answers (2)

Pankti Shah
Pankti Shah

Reputation: 387

This worked for me...

UnityWebRequest www = UnityWebRequestAssetBundle.GetAssetBundle(assetsFilepath);
www.SendWebRequest();
while (!www.isDone)
{
    Debug.Log("Downloading asset: " + www.downloadProgress);
}
if (www.error == null)
{
    string tempPath = Path.Combine(Application.persistentDataPath, assetsFilename);
    FileStream cache = new FileStream(path, FileMode.Create);
    cache.Write(www.downloadHandler.data, 0, www.downloadHandler.data.Length);
    cache.Close();
}

Upvotes: 1

ChoopTwisk
ChoopTwisk

Reputation: 1334

You're waiting for the download to be done, before starting it. Your code should read as follows.

1) Create a request: UnityWebRequest www = UnityWebRequestAssetBundle.GetAssetBundle(assetsFilepath);

2)

  • Send the request and wait for it using one line: yield return www.SendWebRequest();

  • OR Send it without a yield if you want to track the progress.

        www.SendWebRequest();
        while (!www.isDone)
        {
            Debug.Log(www.progress);
            yield return null;
        }
    

3) Now it finished, wait for the download handler to finish processing the data:

while(!www.downloadHandler.isDone)
   yield return null;

4) Now enjoy your downloaded data at www.downloadHandler.data

Upvotes: 2

Related Questions