简体   繁体   中英

Continue downloading file with WWW in Unity3d

I have a main scene where I can download videos to play. If there's a download in progress, and I play a video going to another scene, the download stops. How can I keep the download going?

When I hit an object in the main scene I start this coroutine:

WWW example = new WWW(strURL);
StartCoroutine(ShowProgress(example, video));

yield return example;

if (string.IsNullOrEmpty(example.error))
{
    if (System.IO.Directory.Exists(Application.persistentDataPath + "/Data") == false)
        System.IO.Directory.CreateDirectory(Application.persistentDataPath + "/Data");

    string write_path = Application.persistentDataPath + "/Data" + strURL.Substring(strURL.LastIndexOf("/"));
    Debug.Log(write_path);
    System.IO.File.WriteAllBytes(write_path, example.bytes);
}
else
{
    Debug.Log(example.error);
}

And if I want to play another video, I have to go to another scene, and when I go back to the main scene, the download has stopped

It is actually possible to have a download continue in the background after returning to the home screen. This can't be accomplished with Unity alone, however. Some sort of Objective-C plugin would be required.

Executing Finite-Length Tasks

If your app is in the middle of a task and needs a little extra time to complete that task, it can call the beginBackgroundTaskWithName:expirationHandler: or beginBackgroundTaskWithExpirationHandler: method of the UIApplication object to request some additional execution time.

You don't get an unlimited amount of time, but I've read that the operating system gives you about 10 minutes. Depending how big the files you need are (and the network speed of the user) this may be a feasible solution.

Another option would be to use some type of network library that would allow for pausing and resuming downloads. Again, this probably will require an external plugin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM