【已经找到了问题关键,.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不会被执行