Mickey丶笑风沙 2020-05-18 14:34 采纳率: 33.3%
浏览 694

Unity中使用C#的WebClient的DownloadFileAsync异步下载时,DownloadProgressChanged不执行

【已经找到了问题关键,.net3.5升级到.net4.6会出现这种问题】

我的问题很奇怪,在将一个项目由Unity2017升级到2019后,使用WebClient异步下载功能,在Unity编辑器中运行,异步回调方法会正常执行,但是windows打包后却无法执行

Unity按钮的注册事件代码如下:

void Start()
        {
            downloader = new Downloader(assetName, downloadUrl, savePath);
            downloadBtn.onClick.AddListener(DownlodClick);
        }

        private void DownlodClick()
        {
            downloader.DownloadFileAsync(progress => { }, status =>
            {
                var result = (Downloader.DownloadResult)status;
                Debug.Log("download status:" + result);
            });
        }

Downloader的内容如下:

 private WebClient client;
        private Action<float> progressHandler;
        private Action<int> completedHandler;

        public Downloader(string fileName, string downloadUrl, string savePath)
        {
            this.savePath = savePath;
            this.fileName = fileName;
            this.downloadUrl = downloadUrl;
            uri = new Uri(downloadUrl);
        }

        /// <summary>
        /// 异步下载bundle
        /// </summary>
        /// <param name="progressHandler">传输进度handler,返回百分比进度值0-100</param>
        /// <param name="completedHandler">下载完成handler,DownloadResult类型</param>
        public void DownloadFileAsync(Action<float> progressHandler, System.Action<int> completedHandler)
        {
            cancelled = false;
            client = new WebClient();
            this.progressHandler = progressHandler;
            this.completedHandler = completedHandler;
            if (!Directory.Exists(savePath))
            {
                Directory.CreateDirectory(savePath);
            }
            Debug.Log("fileName:" + fileName);
            client.DownloadFileAsync(uri, Path.Combine(savePath, fileName));
            downloading = true;
            client.DownloadProgressChanged += DownloadProgressChanged;
            client.DownloadFileCompleted += DownloadFileCompleted;
        }

在Unity编辑器中运行点击下载按钮,DownloadProgressChanged和DownloadFileCompleted都 能正常执行;
可是把项目打包(Windows)后,点击下载按钮,文件能够正常下载下来,但是DownloadProgressChanged和DownloadFileCompleted不会被执行

  • 写回答

1条回答 默认 最新

  • 啊蹦蹦 2020-07-19 21:23
    关注

    我在手机真机上也遇到了你这个问题。现在已经解决了。
    1.添加一个捕获异常,可以查看对应的error

    .....
    client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
    client.DownloadFileAsync(new System.Uri(url), currDownFile);
    client.DownloadFileCompleted += new AsyncCompletedEventHandler(webClient_DownloadFileCompleted);
    .....

         public void webClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (sender is WebClient)
            {
                ((WebClient)sender).CancelAsync();
                ((WebClient)sender).Dispose();
            }
            if (e.Error != null)
            {
                Debuger.LogError("error>>" + e.Error.Message); //正常捕获
            }
        }
    
                2.我之前没有跑起来,也没有报错,也是找了2天,后来看到通过这个异常捕获,加进去之后,发现是 currDownFile 中的最后一级目录没有创建,
                所以在 OnDownloadFile 之前,我就判断要创建这个目录 
                //判断本地是否有该目录
                string dir = currDownFile.Substring(0, currDownFile.LastIndexOf("/"));
                Directory.CreateDirectory(dir);
    
    评论

报告相同问题?