public string GetUnpackageResPath(string name, ref bool isStreaming) { isStreaming = false; string key = GetResourceKey(name); if (!string.IsNullOrEmpty(key)) { string fileName = GetResourceFileName(key); ResourceData rd = _resourceList.Resources[key]; if (!rd.IsUnpackage()) { Debugger.LogError(name + " is not Unpackage resource!! 1"); return(null); } if (rd.IsOptional()) { return(_optionalPath + fileName); } if (File.Exists(_dataPath + fileName)) { return(_dataPath + fileName); } isStreaming = true; #if UNITY_ANDROID && !UNITY_EDITOR return(fileName); #else return(_streamingPath + fileName); #endif } #if UNITY_EDITOR else { if (!name.Contains("Unpackage")) { Debugger.LogError(name + " is not Unpackage resource!!"); return(null); } return(Application.dataPath + "/Resources/" + name); } #else return(null); #endif }
public static T Load <T>(string path) where T : UnityEngine.Object { string key = ResourceManager.Instance.GetResourceKey(path); if (!string.IsNullOrEmpty(key)) { ResourceData rd = ResourceManager.Instance.GetResourceData(key); if (rd.IsUnpackage() || rd.IsOptional()) { Debugger.LogError("The source : " + path + " is not normal resource!"); return(null); } T obj = ResourceManager.Instance.GetReferenceResource <T>(key); if (obj != null) { return(obj); } AssetBundle asset = ResourceManager.Instance.LoadAssetBundle(key); if (asset != null) { string assetPaht = "Assets/Resources/" + path; obj = asset.LoadAsset <T>(assetPaht); if (obj != null) { ResourceManager.Instance.AddResourceReference(key, obj); } ResourceManager.Instance.RemoveUnreferenceAssetBundle(key); return(obj); } ResourceManager.Instance.RemoveUnreferenceAssetBundle(key); } if (path.Contains("Unpackage") || path.Contains("Optional")) { Debugger.LogError("The source : " + path + " is not normal resource!"); return(null); } path = path.Substring(0, path.LastIndexOf(".")); return(Resources.Load <T>(path)); }
public static UnityEngine.Object[] LoadAll(string path) { string key = ResourceManager.Instance.GetResourceKey(path); if (!string.IsNullOrEmpty(key)) { ResourceData rd = ResourceManager.Instance.GetResourceData(key); if (rd.IsUnpackage() || rd.IsOptional()) { Debugger.LogError("The source : " + path + " is not normal resource!"); return(null); } UnityEngine.Object[] objs = ResourceManager.Instance.GetReferenceResources(key); if (objs != null && objs.Length > 0) { return(objs); } AssetBundle asset = ResourceManager.Instance.LoadAssetBundle(key); if (asset != null) { objs = asset.LoadAllAssets(); if (objs != null && objs.Length > 0) { ResourceManager.Instance.AddResourcesReference(key, objs); } ResourceManager.Instance.RemoveUnreferenceAssetBundle(key); return(objs); } ResourceManager.Instance.RemoveUnreferenceAssetBundle(key); } if (path.Contains("Unpackage") || path.Contains("Optional")) { Debugger.LogError("The source : " + path + " is not normal resource!"); return(null); } path = path.Substring(0, path.LastIndexOf(".")); return(Resources.LoadAll(path)); }
public List <DownloadFile> GetOptionalNeedDownladList(string key) { List <DownloadFile> list = new List <DownloadFile>(); ResourceData rd = GetResourceData(key); if (rd != null && rd.IsOptional()) { string saveName = _optionalPath + GetResourceFileName(key); if (FileHelper.GetFileCrc(saveName) != rd.Crc) { list.Add(new DownloadFile(_resourceUrl + key + ".ab", saveName, rd.Size, rd.Crc)); } for (int i = 0; i < rd.Depends.Count; ++i) { list.AddRange(GetOptionalNeedDownladList(rd.Depends[i])); } } return(list); }
public static Downloader LoadOptionalUnpackageResBuffer(string path, Action <byte[]> callback) { string key = ResourceManager.Instance.GetResourceKey(path); if (!string.IsNullOrEmpty(key)) { ResourceData rd = ResourceManager.Instance.GetResourceData(key); if (!rd.IsOptional() || !rd.IsUnpackage()) { Debugger.LogError("The source : " + path + " is not Optional and UnpackageRes resource!"); callback(null); return(null); } List <DownloadFile> list = ResourceManager.Instance.GetOptionalNeedDownladList(key); if (list.Count > 0) { Downloader downloader = Downloader.DownloadFiles(list, (o) => { callback(LoadUnpackageResBuffer(path)); }); return(downloader); } callback(LoadUnpackageResBuffer(path)); return(null); } if (!path.Contains("Unpackage") || !path.Contains("Optional")) { Debugger.LogError("The source : " + path + " is not Optional and UnpackageRes resource!"); callback(null); return(null); } callback(LoadUnpackageResBuffer(path)); return(null); }
public static Downloader LoadOptionalResAll(string path, Action <UnityEngine.Object[]> callback) { string key = ResourceManager.Instance.GetResourceKey(path); if (!string.IsNullOrEmpty(key)) { ResourceData rd = ResourceManager.Instance.GetResourceData(key); if (!rd.IsOptional() || rd.IsUnpackage()) { Debugger.LogError("The source : " + path + " is not Optional resource!"); callback(null); return(null); } UnityEngine.Object[] objs = ResourceManager.Instance.GetReferenceResources(key); if (objs != null) { callback(objs); return(null); } AssetBundle asset = null; List <DownloadFile> list = ResourceManager.Instance.GetOptionalNeedDownladList(key); if (list.Count > 0) { Downloader downloader = Downloader.DownloadFiles(list, (o) => { asset = ResourceManager.Instance.LoadAssetBundle(key); if (asset != null) { objs = asset.LoadAllAssets(); if (objs != null && objs.Length > 0) { ResourceManager.Instance.AddResourcesReference(key, objs); } callback(objs); } else { callback(null); } ResourceManager.Instance.RemoveUnreferenceAssetBundle(key); }); return(downloader); } asset = ResourceManager.Instance.LoadAssetBundle(key); if (asset != null) { objs = asset.LoadAllAssets(); if (objs != null && objs.Length > 0) { ResourceManager.Instance.AddResourcesReference(key, objs); } callback(objs); } else { callback(null); } ResourceManager.Instance.RemoveUnreferenceAssetBundle(key); return(null); } if (!path.Contains("Optional") || path.Contains("Unpackage")) { Debugger.LogError("The source : " + path + " is not Optional resource!"); callback(null); return(null); } path = path.Substring(0, path.LastIndexOf(".")); callback(Resources.LoadAll(path)); return(null); }
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; }
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 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()); }); }
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); } }); }