//不包含路径的预设体名, public UObj GetResource(string PrefabName) { UObj obj = null; string ABname = ResourceSetting.ConvertToAssetBundleName(PrefabName); if (dicAsset.TryGetValue(ABname, out obj) == false) { if (dicLoadingReq.ContainsKey(ABname)) { ConsoleEx.DebugLog("<GetResource Failed> Res is still loading, res.Name = " + ABname); } else { // ConsoleEx.DebugLog("<GetResource Failed> Res not exist, res.Name = " + ABname); } obj = null; } else { // 添加引用 RefAsset(ABname); } return(obj); }
UObj RefAssetBundle(string filename) { UObj obj = lstRefAsset.Get(filename); if (obj == null) { //TODO : 后期合成真实的本地路劲 string path = ResourceSetting.ConvertToABPath(filename); /// /// WWW.LoadFromCacheOrDownload 这个方法建议大家以后不要再用了 /// 因为是异步方法,而且还占用内存。 /// AssetBundle asset = AssetBundle.CreateFromFile(path); obj = asset.Load(filename); //释放Compressed资源 asset.Unload(false); //加入引用列表的同时,释放资源 releaseRes(lstRefAsset.Add(filename, obj)); } return(obj); }
IEnumerator AsyncAb(string filename, Action <UObj, string> finished) { yield return(null); bool cached = false; UObj obj = lstRefAsset.Get(filename); cached = obj != null; if (!cached) { //TODO : 后期合成真实的本地路劲 string path = ResourceSetting.ConvertToABPath(filename); AssetBundle asset = AssetBundle.CreateFromFile(path); AssetBundleRequest request = asset.LoadAsync(filename, typeof(UObj)); yield return(request); //加入引用列表的同时,释放资源 releaseRes(lstRefAsset.Add(filename, request.asset)); obj = request.asset; } if (finished != null) { finished(obj, filename); } }
public WWW GetLoadingWWW(string pbName) { string AssetBundleName = ResourceSetting.ConvertToAssetBundleName(pbName); WWW www = null; dicLoadingReq.TryGetValue(AssetBundleName, out www); return(www); }
IEnumerator ErrorOcurredRepeat(AssetTask task) { bool errorOcurred = false; string assetBundleName = task.AssetBundleName; ConsoleEx.DebugLog(" in error occrredddd "); string url = ResourceSetting.ConverToFtpPath(assetBundleName); int verNum = Core.Data.sourceManager.getNewNum(assetBundleName + ".unity3d"); WWW www = null; try { www = WWW.LoadFromCacheOrDownload(url, verNum); } catch (Exception ex) { errorOcurred = true; // ConsoleEx.DebugLog("LoadFromCacheOrDownload Error : " + ex.ToString()); if (task.LoadError != null) { task.LoadError("[" + assetBundleName + "]:" + ex.ToString()); } } if (!errorOcurred) { if (!dicLoadingReq.ContainsKey(assetBundleName)) { dicLoadingReq.Add(assetBundleName, www); } while (www.isDone == false) { if ((task.LoadType == AssetTask.loadType.Only_Download || task.LoadType == AssetTask.loadType.Both_Download_loadlocal) && task.reportProgress != null) { task.reportProgress(www.progress); } yield return(null); } // Print the error to the console if (!String.IsNullOrEmpty(www.error)) { //ConsoleEx.DebugLog("["+assetBundleName+"]"+www.error); //Debug.LogError ("[" + assetBundleName + "]" + www.error); if (task.LoadError != null) { task.LoadError(www.error); } ConsoleEx.DebugLog(" still error "); dicLoadingReq.Remove(assetBundleName); errorOcurred = true; } } }
//异步加载Assetbundle上的东西 IEnumerator AsyncAb(string filename, Action <UObj, string> finished) { yield return(null); bool cached = false; AssetBundle asset = lstRefAsset.Get(filename); cached = asset != null; if (!cached) { //TODO : 后期合成真实的本地路劲 string path = ResourceSetting.ConvertToABPath(filename); asset = AssetBundle.CreateFromFile(path); /// add Reference if it does't exist KeyValuePair <string, AssetBundle>[] toBeRm = lstRefAsset.Add(filename, asset); if (toBeRm != null && toBeRm.Length > 0) { foreach (KeyValuePair <string, AssetBundle> rm in toBeRm) { bool useless = Usless.ContainsKey(rm.Key); if (useless) { Usless.Remove(rm.Key); rm.Value.Unload(true); } else { rm.Value.Unload(false); } } } toBeRm = null; AssetBundleRequest request = asset.LoadAsync(filename, typeof(UObj)); yield return(request); if (finished != null) { finished(request.asset, filename); } } }
/// <summary> /// 添加引用, 如果已经有引用的时候,则会放到最近使用的位置,所以最不可能被删除 /// </summary> /// <param name="name">Name.</param> protected override AssetBundle RefAsset(string filename) { bool cached = false; AssetBundle asset = lstRefAsset.Get(filename); cached = asset != null; if (!cached) { //TODO : 后期合成真实的本地路劲 string path = ResourceSetting.ConvertToABPath(filename); /// /// WWW.LoadFromCacheOrDownload 这个方法建议大家以后不要再用了 /// 因为是异步方法,而且还占用内存。 /// asset = AssetBundle.CreateFromFile(path); /// add Reference if it does't exist KeyValuePair <string, AssetBundle>[] toBeRm = lstRefAsset.Add(filename, asset); if (toBeRm != null && toBeRm.Length > 0) { foreach (KeyValuePair <string, AssetBundle> rm in toBeRm) { bool useless = Usless.ContainsKey(rm.Key); if (useless) { Usless.Remove(rm.Key); rm.Value.Unload(true); } else { rm.Value.Unload(false); } } } toBeRm = null; } return(asset); }
public bool IsResLoaded(string pbName) { string AssetBundleName = ResourceSetting.ConvertToAssetBundleName(pbName); return(dicAsset.ContainsKey(AssetBundleName)); }
/// <summary> /// According to LoadType, it will download or load local sync /// 把AssetBundle载入内存,之后再异步Load出来,之后再次删除Assetbundle内存镜像. /// </summary> /// <returns>The load coroutine.</returns> /// <param name="task">Task.</param> private IEnumerator AsyncLoadCoroutine(AssetTask task) { bool errorOcurred = false; string assetBundleName = task.AssetBundleName; string url = ResourceSetting.ConverToFtpPath(assetBundleName); int verNum = 1;//Core.Data.sourceManager.getNewNum(assetBundleName + ".unity3d"); // 添加引用 RefAsset(assetBundleName); if (Caching.IsVersionCached(url, verNum) == false) { //ConsoleEx.DebugLog("Version Is not Cached, which will download from net!"); } WWW www = null; try { www = WWW.LoadFromCacheOrDownload(url, verNum); } catch (Exception ex) { errorOcurred = true; // ConsoleEx.DebugLog("LoadFromCacheOrDownload Error : " + ex.ToString()); if (task.LoadError != null) { task.LoadError("[" + assetBundleName + "]:" + ex.ToString()); } } if (!errorOcurred) { dicLoadingReq.Add(assetBundleName, www); while (www.isDone == false) { if ((task.LoadType == AssetTask.loadType.Only_Download || task.LoadType == AssetTask.loadType.Both_Download_loadlocal) && task.reportProgress != null) { task.reportProgress(www.progress); } yield return(null); } // Print the error to the console if (!String.IsNullOrEmpty(www.error)) { //ConsoleEx.DebugLog("["+assetBundleName+"]"+www.error); Debug.LogError("[" + assetBundleName + "]" + www.error); if (task.LoadError != null) { task.LoadError(www.error); } dicLoadingReq.Remove(assetBundleName); errorOcurred = true; } bool TaskEnd = false; if (task.LoadType == AssetTask.loadType.Only_loadlocal || task.LoadType == AssetTask.loadType.Both_Download_loadlocal) { if (!errorOcurred) { AssetBundleRequest req = www.assetBundle.LoadAsync(task.PrefabName, task.UType); while (req.isDone == false) { yield return(null); } dicAsset.Add(assetBundleName, req.asset); task.Obj = req.asset; dicLoadingReq.Remove(assetBundleName); www.assetBundle.Unload(false); //--- load local finished --- TaskEnd = true; } } else { dicLoadingReq.Remove(assetBundleName); //--- Downloading finished --- TaskEnd = true; } //release memeory if (www != null) { www.Dispose(); www = null; } //start to callback if (TaskEnd) { LoadEnd(); if (task.LoadFinished != null) { task.LoadFinished(task); } } } }