Пример #1
0
    private IEnumerator LoadAssetBundleFromCache(Dictionary <string, GameResAssetBundle> bundleGroupMap, string bundleName, string manifestName, string hash, string[] urls, System.Action <bool> onLoadDone)
    {
        var realHash = Hash128.Parse(hash);

        bool success = false;

        foreach (string url in urls)
        {
            float checkTimeOutTime = Time.time;
            float progress         = 0;

            var webRequest = GameWebRequest.SendAssetBundleWebRequest(url, realHash, true);
            while (!webRequest.request.isDone)
            {
                if (!string.IsNullOrEmpty(webRequest.request.error))
                {
                    break;
                }
                if (progress != webRequest.request.downloadProgress)
                {
                    checkTimeOutTime = Time.time;
                }
                else if (Time.time - checkTimeOutTime >= 5)
                {
                    break;
                }
                yield return(null);
            }

            if (!string.IsNullOrEmpty(webRequest.request.error) || !webRequest.request.isDone)
            {
                FTDebug.LogWarning(string.Format("Failed To LoadAssetBundleFromCache Name[{0}] From[{1}] Error[{2}]", bundleName, url, webRequest.request.error));
                GameWebRequest.DestroyAssetBundleWebRequest(webRequest);
                Caching.ClearCachedVersion(bundleName, realHash);
                continue;
            }

            AssetBundle assetBundle = null;
            try
            {
                assetBundle = DownloadHandlerAssetBundle.GetContent(webRequest.request);
                if (assetBundle == null)
                {
                    FTDebug.LogWarning(string.Format("Failed To LoadAssetBundleFromCache Name[{0}] From[{1}] Error[AssetBundle is Null]", bundleName, url));
                    GameWebRequest.DestroyAssetBundleWebRequest(webRequest);
                    Caching.ClearCachedVersion(bundleName, realHash);
                    continue;
                }
            }
            catch (System.Exception e)
            {
                FTDebug.LogWarning(string.Format("Failed To LoadAssetBundleFromCache Name[{0}] From[{1}] Error[{2}]", bundleName, url, e.Message));
                GameWebRequest.DestroyAssetBundleWebRequest(webRequest);
                Caching.ClearCachedVersion(bundleName, realHash);
                continue;
            }

            if (bundleGroupMap.ContainsKey(bundleName) && bundleGroupMap[bundleName].hash != hash)
            {
                UnLoadAssetBundle(bundleName, manifestName);
            }

            if (!bundleGroupMap.ContainsKey(bundleName))
            {
                bundleGroupMap.Add(bundleName, new GameResAssetBundle()
                {
                    hash        = hash,
                    assetBundle = assetBundle,
                });
            }

            GameWebRequest.DestroyAssetBundleWebRequest(webRequest);
            success = true;
            FTDebug.Log(string.Format("LoadAssetBundleFromCache Name[{0}] From[{1}]", bundleName, url));

            // 清除除当前使用的
            Caching.ClearOtherCachedVersions(bundleName, realHash);

            break;
        }

        onLoadDone(success);
    }
Пример #2
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);
    }