2017-08-11 68 views
0

我正在创建一个需要下载大文件的应用程序(大约650MB)。我目前正在使用这种方法来下载文件。 http://answers.unity3d.com/questions/322526/downloading-big-files-on-ios-www-will-give-out-of.html在统一应用程序暂停时继续下载。 Android和iOS

但是,如果应用程序发送到后台或暂停下载失败。由于我的应用程序适用于Gear VR,因此关闭耳机会暂停播放,这样基本上所有用户都会发生这种情况。

我认为答案可能与使用Android服务有关,但不知道该怎么做,或者如果这是解决这个问题的正确方法。

+0

您正处在正确的轨道上。 * Android服务*是您需要的。您必须从Java开始提供服务,并且必须使用不带C#代码的Java代码进行下载。这被称为插件。你链接的代码是无用的。您只能将其用作写入/移植到Java的参考。当你启动服务时,你可以命令该服务下载文件。 Unity关闭时,应继续下载。对于iOS,您必须使用Object-C创建服务而不是Java。这些不是件容易的事,但谷歌搜索他们应该帮助。 – Programmer

回答

0

这是我要去的代码。 我在这里找到了。 Downloading large file in Unity3d using WebClient 这似乎是最好的方法吗?另外我应该用什么iOS?

void DownloadFile() 
{ 
    loadingBar.gameObject.SetActive(true); 
    downloadButton.SetActive(false); 
    downloadingFile = true; 
    WebClient client = new WebClient(); 
    client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged); 
    client.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(DownloadFileCompleted); 
    client.DownloadFileAsync(new Uri(url), Application.persistentDataPath + "/" + fileName); 
} 

void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) 
{ 
    double bytesIn = double.Parse(e.BytesReceived.ToString()); 
    double totalBytes = double.Parse(e.TotalBytesToReceive.ToString()); 
    double percentage = bytesIn/totalBytes * 100; 
    downloadProgressText = "Downloaded " + e.BytesReceived + " of " + e.TotalBytesToReceive; 
    downloadProgress = int.Parse(Math.Truncate(percentage).ToString()); 
    totalBytes = e.TotalBytesToReceive; 
    bytesDownloaded = e.BytesReceived; 
} 

void DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) 
{ 
    if (e.Error == null) 
    { 
     AllDone(); 
    } 
} 

void AllDone() 
{ 
    Debug.Log("File Downloaded"); 
    FileExists = 1; 
} 

public void DeleteVideo() 
{ 
    print("Delete File"); 
    PlayerPrefs.DeleteKey("videoDownloaded"); 
    FileExists = 0; 
    enterButton.SetActive(false); 
    downloadButton.SetActive(true); 
    File.Delete(Application.persistentDataPath + "/" + fileName); 
} 
相关问题