/// <summary> /// 获取资源 /// 场景不会返回对象资源,文本是同步加载,并且它们都不缓存 /// </summary> /// <param name="scriptObject">引用资源的脚本</param> /// <param name="path">资源包全路径(带名字)</param> /// <param name="name">要请求的资源名</param> /// <param name="progress"></param> /// <param name="complete"></param> /// <param name="type">ResourceType</param> /// <param name="order">优先级</param> /// <param name="isInstance">是否实例化</param> /// <param name="args"></param> /// <returns>原始对象,如果能直接得到则为从缓存中取出</returns> public Object GetLocalResource(System.Object scriptObject, string path, string name, LoadingProgressCallback progress, LoadCompleteCallback complete, ResourceType type, Order order, bool isInstance, params string[] args) { ResInfo t_info; ResLoader t_loader; bool t_isNeedDownload = true; if (m_dictLocalResource.TryGetValue(path, out t_info)) { if (t_info.assetBundle != null)//之前已经加载过相同路径的资源 { Object t_assetObj = t_info.GetAsset(name); if (t_assetObj != null) { #if Debug Debug.Log("from cache"); #endif if (progress != null) { progress(path, name, 1f, args); } if (complete != null) { if (IsNeedIncreaseRefCount(type, isInstance)) { t_info.AddRefCount(scriptObject); } return(ResCallBackReady(path, name, complete, t_assetObj, isInstance, args)); } return(null); } else { if (IsNeedIncreaseRefCount(type, isInstance)) { t_info.AddRefCount(scriptObject); } t_loader = new ResLoader(); t_loader.LoadFromAssetBundle(t_info.assetBundle, scriptObject, path, name, type, order, isInstance, progress, complete, args); m_dictDownload[(int)order].Add(t_loader); ++m_loaderCount; return(null); } } else//第一个加载此路径的资源包还未加载完成,第2次已经请求了 { if (IsNeedIncreaseRefCount(type, isInstance)) { t_info.AddRefCount(scriptObject); } t_isNeedDownload = false; } } else//加载资源包 { t_info = new ResInfo(path); m_dictLocalResource.Add(path, t_info); if (IsNeedIncreaseRefCount(type, isInstance)) { t_info.AddRefCount(scriptObject); } } t_loader = new ResLoader(); t_loader.LoadFromWWW(scriptObject, path, name, type, order, isInstance, t_isNeedDownload, progress, complete, args); m_dictDownload[(int)order].Add(t_loader); ++m_loaderCount; return(null); }
void Update() { if (m_loaderCount == 0) { return; } ResInfo t_info = null; for (int order = 0; order < (int)Order.o_max; order++) { List <ResLoader> t_downloadList = m_dictDownload[order]; ResLoader t_loader = null; int t_loaderListCount = t_downloadList.Count; int t_toDeleteCount = 0;//要删除的加载器个数 for (int i = 0; i < t_loaderListCount - t_toDeleteCount;) { t_loader = t_downloadList[i]; if (!m_dictLocalResource.ContainsKey(t_loader.SrcPath)) { Debug.LogWarning(t_loader.SrcPath + " already discard from loading list"); t_loader.Release(true); CollectionUtil.SwapDestroyObject <ResLoader>(t_downloadList, i, t_loaderListCount - t_toDeleteCount - 1);//把错误的loader都移动到尾部 ++t_toDeleteCount; continue; } t_info = m_dictLocalResource[t_loader.SrcPath]; if (t_loader.LoadingType == LoadType.fromWWW) { if (t_loader.IsNeedDownload) { if (t_loader.HasWWWError) { // Debug.LogError(t_loader.SrcPath + ":loaded error"); // MDebug.Log(t_loader.SrcPath + ":loaded error"); t_loader.Release(true); CollectionUtil.SwapDestroyObject <ResLoader>(t_downloadList, i, t_loaderListCount - t_toDeleteCount - 1);//把错误的loader都移动到尾部 ++t_toDeleteCount; continue; } } else { ++i; if (t_info.assetBundle == null)//正在下载的加载器还没完成下载 { continue; } t_loader.LoadFromAssetBundle(t_info.assetBundle);//下一个优先级里的等待loader可能并不会被本次回调操作遍历到,所以需要转换,并且在下一帧判断加载完成 } } else { } if (t_loader.ProgressCallback != null) { t_loader.ProgressCallback(t_loader.SrcPath, t_loader.SrcName, t_loader.Progress, t_loader.CustomData); } if (t_loader.IsComplete) { // MDebug.Log(t_info.ResPath + " is loaded"); ProcessLoaded(t_info, t_loader, t_downloadList, i, ref t_toDeleteCount); continue; } ++i; } if (t_toDeleteCount > 0) { t_downloadList.RemoveRange(t_loaderListCount - t_toDeleteCount, t_toDeleteCount); m_loaderCount -= t_toDeleteCount; } } }