继续在Unity3d中使用WWW下载文件

时间:2015-12-22 16:40:21

标签: c# unity3d

我有一个主要场景,我可以下载视频播放。 如果正在进行下载,并且我播放视频到另一个场景,则下载停止。 如何保持下载?

当我点击主场景中的一个对象时,我启动了这个协程:

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);
}

如果我想播放另一个视频,我必须去另一个场景,当我回到主场景时,下载已停止

1 个答案:

答案 0 :(得分:1)

实际上可以在返回主屏幕后在后台继续下载。但是,仅使用Unity无法实现这一点。 Objective-C plugin。

Executing Finite-Length Tasks

  

如果您的应用程序处于任务中间并且需要一点额外时间来完成该任务,它可以调用beginBackgroundTaskWithName:expirationHandler:或beginBackgroundTaskWithExpirationHandler:UIApplication对象的方法来请求一些额外的执行时间。

你没有获得无限的时间,但我已经读过操作系统给你大约10分钟。根据您需要的文件大小(以及用户的网络速度),这可能是一个可行的解决方案。

另一种选择是使用某种类型的网络库来允许暂停和恢复下载。同样,这可能需要一个外部插件。

相关问题