【问题标题】:How to download files in background regardless of Application state?无论应用程序状态如何,如何在后台下载文件?
【发布时间】:2016-11-23 11:54:37
【问题描述】:

有人在 Unity Questions 中向 coupletimes 提出过此问题,但从未回答。

我需要做的就是创建一个 Android 插件,它可以从给定的 url 下载少量文件,并在通知面板中显示下载进度。即使我的 Unity 应用程序失焦,下载仍应继续。


(来源:cuelogic.com

这是我现在拥有的一段代码:

void DownloadFiles(string[] urls)
{
    foreach(var url in urls)
    {
        StartCoroutine(DownloadFile_CR(url));
    }
}

IEnumerator DownloadFile_CR(string url)
{
    WWW www = new WWW(url);
    while(!www.isDone)
    {
        yield return null;
    }
    if(www.error == null)
    {            
        //file downloaded. do something...
    }
}

这些是一些纹理文件。那么如何从原生 android 代码中获取纹理结果呢?

感谢任何帮助之王。

【问题讨论】:

    标签: java c# android unity3d plugins


    【解决方案1】:

    我遇到了同样的问题。起初,我使用了一项在后台运行的服务并下载了我需要的文件,包括计算进度和完成事件。

    然后,我让我的插件更加简单易用。您创建一个 Java 对象的实例,为它提供 GameObject 名称和响应的方法名称。我使用json 来序列化和反序列化java 和C# 对象,因为Unity 的MonoBehaviour 对象和java 对象之间只能传递字符串。

    这是下载在 android 插件中的样子:

                Uri Download_Uri = Uri.parse(url);
                DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
    
                //Restrict the types of networks over which this download may proceed.
                request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
                //Set whether this download may proceed over a roaming connection.
                request.setAllowedOverRoaming(true);
                //Set the local destination for the downloaded file to a path within the application's external files directory
                String[] split = url.split("/");
                request.setDestinationInExternalFilesDir(activity, null, split[split.length-1]);
                //Set the title of this download, to be displayed in notifications (if enabled).
                request.setTitle("Downloading " + title);
                //Set a description of this download, to be displayed in notifications (if enabled)
                request.setDescription("Downloading " + name);
    
                request.setVisibleInDownloadsUi(false);
    
                //Enqueue a new download and get the reference Id
                long downloadReference = downloadManager.enqueue(request);
    

    然后您可以将参考 ID 发送回 unity,这样您就可以获取进度并在您的应用重新启动后检查文件是否仍在下载(使用 SharedPreferences \ PlayerPrefs 来存储它们)

    【讨论】:

    • 看起来不错,更接近我的想法。但我想知道如何处理超过 1 个文件。例如,我有 500 个图像文件要下载。而且我不想让通知面板充斥着 500 个下载进度条(如果它允许下载并发文件)。是否有可能以某种方式展示集体进步。在 Unity 中,我会使用文件计数来计算进度(下载的 100/500 个文件 = 完成了 20%)。最后,如何从存储中访问 Unity 中下载的文件?对nooby问题表示歉意,因为我对原生开发或插件没有太多经验:)
    • 您添加request.setNotificationVisibility(false),然后只需输入您自己的显示进度的通知。
    【解决方案2】:

    如果您希望它即使在不关注统一性的情况下也能继续,那么您不能在 Unity 的 C# 中使用 WWW 类来实现。

    如果我想这样做,我可能会编写一个原生 Android 插件来启动下载服务

    来自谷歌官方文档:

    服务是可以执行长时间运行的应用程序组件 后台操作,不提供用户 界面。另一个应用程序组件可以启动一个服务,并且它 即使用户切换到 另一个应用程序。

    服务并不复杂,您可以像使用活动一样使用 Intent 来启动它们,并且此类服务的在线示例很多。

    这里是关于服务的官方 Android 文档:https://developer.android.com/guide/components/services.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-05
      • 1970-01-01
      • 1970-01-01
      • 2019-02-15
      • 2014-12-07
      • 1970-01-01
      • 2020-04-18
      • 2011-08-27
      相关资源
      最近更新 更多