直接下载不显示进度
private void OnDownloadAssets()//下载资源{StartCoroutine(DownloadFormServer_IE(url, savePath));}//其他方法private IEnumerator DownloadFormServer_IE(string url, string path)//从服务器下载资源{Debug.Log("正在下载" + url);UnityWebRequest request = UnityWebRequest.Get(url);//直接下载不显示进度yield return request.SendWebRequest();if (request.result== UnityWebRequest.Result.ProtocolError || request.result== UnityWebRequest.Result.ConnectionError){Debug.Log(request.responseCode);Debug.Log(request.ersror);yield break;}DownloadHandler downloadHandler = request.downloadHandler;if (!downloadHandler.isDone){Debug.Log("正在下载");yield return downloadHandler;}else{Debug.Log("下载完成");byte[] data = request.downloadHandler.data;using (FileStream fs = new FileStream(path, FileMode.Create)){fs.Write(data, 0, data.Length);}this.SendEvent(new OnFinishDownloadOneAsset_AssetsManager());}}
显示下载进度
//注册事件的方法
private void OnDownloadAssets()//下载资源
{
GameController.Instance.StartCoroutine(DownloadFormServer_IE(url, savePath));
}
//其他方法
private IEnumerator DownloadFormServer_IE(string url, string path)//从服务器下载资源
{Debug.Log("正在下载" + url);UnityWebRequest request = UnityWebRequest.Get(url);//显示下载进度和速度request.SendWebRequest();if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError){Debug.Log(request.responseCode);Debug.Log(request.error);yield break;}while (!request.isDone){//下载速度string netSpeedStr = "0kb";if (request.downloadHandler != null && request.downloadHandler.data != null){float speed = request.downloadHandler.data.Length / 1024;if (speed > 1024){speed = (speed / 1024);netSpeedStr = speed.ToString("f") + "mb"; }else{ netSpeedStr = speed + "kb"; }}//下载进度 和速度Debug.Log("进度" + (request.downloadProgress*100).ToString("f") + "% "+ netSpeedStr );yield return null;}DownloadHandler downloadHandler = request.downloadHandler;Debug.Log("下载完成");byte[] data = request.downloadHandler.data;using (FileStream fs = new FileStream(path, FileMode.Create)){fs.Write(data, 0, data.Length);}this.SendEvent(new OnFinishDownloadOneAsset_AssetsManager());
}