public IEnumerator LoadAssetBundleAsync(string key, AssertBundleAsyncLoader asyncLoader) { asyncLoader.progress = 0; if (_resourceList == null) { yield break; } if (!_resourceList.Resources.ContainsKey(key)) { yield break; } AssetBundle asset = null; if (_loadedAssetBundles.ContainsKey(key)) { asset = _loadedAssetBundles[key]; if (asset != null) { asyncLoader.assetBundle = asset; yield break; } } ResourceData rd = _resourceList.Resources[key]; float childRate = 1 / (rd.Depends.Count + 1); int loadCount = 0; for (int i = 0; i < rd.Depends.Count; ++i) { LoadAssetBundle(rd.Depends[i], key); asyncLoader.progress = (++loadCount) * childRate; TimeSpan ts = DateTime.Now - asyncLoader.lastUpdateTime; if (ts.TotalSeconds > 0.3d) { asyncLoader.lastUpdateTime = DateTime.Now; yield return(0); } } string filename = GetResourceFileName(key); string assetFile = ""; bool isStreaming = false; if (rd.IsOptional()) { assetFile = _optionalPath + filename; } else { assetFile = _dataPath + filename; if (!File.Exists(assetFile)) { #if UNITY_ANDROID && !UNITY_EDITOR assetFile = filename; #else assetFile = _streamingPath + filename; #endif isStreaming = true; } } if (asset == null) { asset = LoadAssetBundleFile(assetFile, isStreaming); } if (asset == null) { Debugger.LogError("Load AssetBundle : " + key + " fail! 2"); Debugger.LogError("Resource path : " + rd.Path); } else { AddUnreferenceAssetBundle(key, asset); asyncLoader.assetBundle = asset; } asyncLoader.progress = 1; }
void DownloadServerResource() { if (_currentDownloader != null) { _currentDownloader.PauseDownload(false); GameClient.Instance.StartCoroutine(CheckDownloadingNetWork()); return; } _currentProgress.Progreess = 0f; _onShowUpdateProgress(_currentProgress); string newResourceListPath = Application.persistentDataPath + "/ResourceList.ab"; if (File.Exists(newResourceListPath)) { _finishDatas.Clear(); var itor = ResourceManager.Instance.ResourceList.Resources.GetEnumerator(); while (itor.MoveNext()) { _currentResourclist.Resources.Add(itor.Current.Key, itor.Current.Value); } } int toltalSize = 0; List <DownloadFile> downloadfiles = new List <DownloadFile>(_needUpdateResources.Count); var a = new TraverseInThread <string>(_needUpdateResources, _needUpdateResources.Count, (arg) => { string key = arg as string; ResourceData rd = _newResources.Resources[key]; string filename = ResourceManager.GetResourceFileName(key); string saveName = rd.IsOptional() ? ResourceManager.OptionalPath + filename : ResourceManager.DataPath + filename; DownloadFile downloadFile = new DownloadFile(ResourceManager.ResourceUrl + key + ".ab", saveName, rd.Size, rd.Crc); toltalSize += rd.Size; lock (_threadLock) { downloadfiles.Add(downloadFile); } if (rd.Path.Contains("Install/Unpackage/GameLogic.bytes") || rd.Path.Contains("Scenes/Install/UI")) { _needRestart = true; } else if (rd.Path.Contains("Install/Unpackage/Data/")) { _needReload = true; } }, () => { _currentProgress.Progreess = 0f; _currentProgress.TotalSize = toltalSize; _onShowUpdateProgress(_currentProgress); _lastSaveTime = DateTime.Now; _currentDownloader = Downloader.DownloadFiles(downloadfiles, (arg) => //onFinish { _currentDownloader = null; _currentResourclist.Resources.Clear(); _finishDatas.Clear(); if (_needUpdateResources.Count > 0) { _onShowUpdateStepFail(0); return; } if (File.Exists(newResourceListPath)) { try { lock (_threadLock) { File.Copy(newResourceListPath, ResourceManager.DataPath + "ResourceList.ab", true); } File.Delete(newResourceListPath); } catch (Exception ex) { Debugger.LogException(ex); } ResourceManager.Instance.LoadResourceList(); } if (_needRestart) { ChangeCurrentUpdateState(UpdateState.RestartClient, false); } else { _newResources.Resources.Clear(); _newResources = null; if (_needReload) { ChangeCurrentUpdateState(UpdateState.ReloadConfigs); } else { ChangeCurrentUpdateState(UpdateState.UpdateFinish); } } }, (arg) => //onProgress; { object[] args = arg as object[]; _currentProgress.Progreess = (float)args[0]; _currentProgress.TotalSize = toltalSize; _onShowUpdateProgress(_currentProgress); }, (arg) => //onSingleFileFinish { if (!File.Exists(newResourceListPath)) { return; } DownloadFile file = arg as DownloadFile; string key = Path.GetFileNameWithoutExtension(file.savePath); if (File.Exists(file.savePath)) { ResourceData rd = _newResources.Resources[key]; KeyValuePair <string, ResourceData> pair = new KeyValuePair <string, ResourceData>(key, rd); lock (_saveThreadLock) { _finishDatas.Add(pair); } _needUpdateResources.Remove(key); } if (_currentDownloader != null && !_isSaving && (_lastSaveTime - DateTime.Now).Seconds > 20) { _isSaving = true; _lastSaveTime = DateTime.Now; Thread thread = new Thread(SaveResourceList); thread.Start(); _isSaving = false; } } ); GameClient.Instance.StartCoroutine(CheckDownloadingNetWork()); }); }
public AssetBundle LoadAssetBundle(string key, string root = null) { if (_resourceList == null) { return(null); } if (!_resourceList.Resources.ContainsKey(key)) { return(null); } if (string.IsNullOrEmpty(root)) { root = key; } AssetBundle asset = null; if (_loadedAssetBundles.ContainsKey(key)) { asset = _loadedAssetBundles[key]; if (asset != null) { return(asset); } } ResourceData rd = _resourceList.Resources[key]; for (int i = 0; i < rd.Depends.Count; ++i) { LoadAssetBundle(rd.Depends[i], root); } string filename = GetResourceFileName(key); string assetFile = ""; bool isStreaming = false; if (rd.IsOptional()) { assetFile = _optionalPath + filename; } else { assetFile = _dataPath + filename; if (!File.Exists(assetFile)) { #if UNITY_ANDROID && !UNITY_EDITOR assetFile = filename; #else assetFile = _streamingPath + filename; #endif isStreaming = true; } } if (asset == null) { asset = LoadAssetBundleFile(assetFile, isStreaming); } if (asset == null) { Debugger.LogError("Load AssetBundle : " + key + " fail!"); Debugger.LogError("Resource path : " + rd.Path); } else { if (rd.Reference > 0) { AddLoadedAssetBundle(key, asset); } else { AddUnreferenceAssetBundle(root, asset); } } return(asset); }
void NeedDownloadResource() { _needUpdateResources.Clear(); int totalSize = 0; #if UNITY_ANDROID && !UNITY_EDITOR ResourceDatas streamingResources = ResourceManager.LoadResourceDatas(ResourceManager.GetStreamingFile("ResourceList.ab")); #else ResourceDatas streamingResources = ResourceManager.LoadResourceDatas(ResourceManager.StreamingPath + "ResourceList.ab"); #endif var a = new TraverseInThread <KeyValuePair <string, ResourceData> >(_newResources.Resources, _newResources.Resources.Count, (obj) => { KeyValuePair <string, ResourceData> pair = (KeyValuePair <string, ResourceData>)obj; string key = pair.Key; ResourceData rd = pair.Value; string filename = ResourceManager.GetResourceFileName(key); if (!rd.IsOptional()) { bool streaminghas = false; if (streamingResources.Resources.ContainsKey(key)) { ResourceData srd = streamingResources.Resources[key]; if (rd.Crc == srd.Crc) { streaminghas = true; } } if (FileHelper.GetFileCrc(ResourceManager.DataPath + filename) != rd.Crc && !streaminghas) { lock (_threadLock) { _needUpdateResources.Add(key); } totalSize += rd.Size; } } else { if (File.Exists(ResourceManager.OptionalPath + filename)) { if (FileHelper.GetFileCrc(ResourceManager.OptionalPath + filename) != rd.Crc) { lock (_threadLock) { _needUpdateResources.Add(key); } totalSize += rd.Size; } } } }, () => { if (_needUpdateResources.Count == 0) { if (File.Exists(Application.persistentDataPath + "/ResourceList.ab")) { try { File.Copy(Application.persistentDataPath + "/ResourceList.ab", ResourceManager.DataPath + "ResourceList.ab", true); File.Delete(Application.persistentDataPath + "/ResourceList.ab"); } catch (Exception ex) { Debugger.LogException(ex); } } ChangeCurrentUpdateState(UpdateState.UpdateFinish); } else { _currentProgress.TotalSize = totalSize; object[] obj = new object[2]; obj[0] = totalSize; obj[1] = _resourceUpdateTips; ChangeCurrentUpdateState(UpdateState.DownloadServerResource, false, obj); } }); }