private void OnDownloadFinish(GameResDownloadTask task) { _curTask = null; if (task.onDownloadState != null) { task.onDownloadState((int)task.state, 1.0f, task.totalSize, _taskList.Count); } for (int i = 0; i < _taskList.Count; i++) { if (_taskList[i].taskId == task.taskId) { _taskList.RemoveAt(i); break; } } if (_taskList.Count > 0 && _taskList[0].state == GameResDownloadState.WAITING) { StartCoroutine(_DownloadAsync(_taskList[0])); } }
private IEnumerator _DownloadAsync(GameResDownloadTask task) { _curTask = task; var realHash = Hash128.Parse(task.hash); task.state = GameResDownloadState.DOWNLOADING; #if UNITY_ANDROID && !UNITY_EDITOR Caching.compressionEnabled = !task.bundleName.Contains("video"); #endif bool success = false; foreach (string url in task.urls) { float checkTimeOutTime = Time.time; float progress = 0; FTDebug.Log(string.Format("Download {0} From {1} Size[{2}]", task.bundleName, url, task.totalSize)); _curWebRequest = GameWebRequest.SendAssetBundleWebRequest(url, realHash, true); if (task.onDownloadState != null) { task.onDownloadState((int)GameResDownloadState.DOWNLOADING, 0, 0, _taskList.Count); } while (_curWebRequest != null && !_curWebRequest.request.isDone) { if (!string.IsNullOrEmpty(_curWebRequest.request.error)) { break; } var _progress = _curWebRequest.request.downloadProgress; if (task.timeOut != 0) { if (progress != _progress) { checkTimeOutTime = Time.time; } else if (Time.time - checkTimeOutTime >= task.timeOut) { break; } } if (progress != _progress && task.onDownloadState != null) { task.onDownloadState((int)GameResDownloadState.DOWNLOADING, _progress, (int)(_progress * task.totalSize), _taskList.Count); } progress = _progress; yield return(null); } FTDebug.LogWarning(string.Format("Download Result {0} From {1} Error[{2}] {3}", task.bundleName, url, _curWebRequest.request.error, _curWebRequest.request.isDone)); if (!string.IsNullOrEmpty(_curWebRequest.request.error) || !_curWebRequest.request.isDone) { FTDebug.LogWarning(string.Format("Failed To Download {0} From {1} Error[{2}]", task.bundleName, url, _curWebRequest.request.error)); GameWebRequest.DestroyAssetBundleWebRequest(_curWebRequest); _curWebRequest = null; continue; } FTDebug.Log(string.Format("Download Success {0} From {1} Size[{2}]", task.bundleName, url, task.totalSize)); GameWebRequest.DestroyAssetBundleWebRequest(_curWebRequest); _curWebRequest = null; success = true; break; } #if UNITY_ANDROID && !UNITY_EDITOR if (!Caching.compressionEnabled) { Caching.compressionEnabled = true; } #endif task.state = success ? GameResDownloadState.SUCCESS : GameResDownloadState.FAILED; OnDownloadFinish(task); }