/// <summary> /// 同步加载资源,这里不提供进度 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="resType">从哪里加载</param> /// <param name="assetPath"></param> /// <param name="isSprite">是否是加载Sprite</param> /// isRecycleOnDestroy : 当ResLoader被销毁时是否从内存中释放 /// <returns></returns> public T LoadSync <T>(ResFromType resType, string assetPath, bool isSprite = false, bool DestroyCache = false) where T : Object { if (CheckNoCanLoad()) { AFLogger.e("此Loader已经被释放,请注意!"); return(null); } ResLoadInfo resLoadInfo = ResLoadInfo.Allocate(resType, assetPath, isSprite, null, null, DestroyCache); Object resAsset = ResManager.Instance.LoadSync(resLoadInfo); if (resAsset != null) { //加载完成后添加到加载资源列表中 mResList.AddValue(resLoadInfo.mResPath); } resLoadInfo.Recycle2Cache(); return(resAsset as T); }
public void LoadResAsyncForObj(ObjLoadInfo objLoadInfo) { ResInfo resInfo = ResManager.Instance.GetRes(objLoadInfo.mCRC); if (resInfo == null) { ResLoadInfo resLoadInfo = ResLoadInfo.Allocate(objLoadInfo); //资源没有加载 LoadAsync(resLoadInfo); resLoadInfo.Recycle2Cache(); } else if (resInfo.State != ResState.Ready) { //资源正在加载,是其他loader的异步加载 resInfo.RegisterListen(objLoadInfo.loadResCall); } else { //资源已经加载完成,开始回调 objLoadInfo.loadResCall(true, resInfo); } }
/// <summary> /// 异步加载 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="resType"></param> /// <param name="assetPath"></param> /// <param name="loadFinish"></param> /// <param name="isSprite"></param> /// <param name="DestroyCache">当ResLoader被销毁时是否从内存中释放</param> /// <param name="loadResPriority">加载的优先级</param> public void LoadAsync <T>(ResFromType resType, string assetPath, System.Action <bool, ResInfo> loadFinish, bool isSprite = false, bool DestroyCache = false, LoadResPriority loadResPriority = LoadResPriority.RES_NUM) where T : Object { if (CheckNoCanLoad()) { AFLogger.e("此Loader已经被释放,请注意!"); return; } ResLoadInfo resLoadInfo = ResLoadInfo.Allocate(resType, assetPath, isSprite, AsyncResCallBack, loadFinish, DestroyCache); //添加回调函数 if (mAsyncLoadingResDic.ContainsKey(resLoadInfo.mResPath)) { mAsyncLoadingResDic[resLoadInfo.mResPath].Add(resLoadInfo); } else { AsyncCount += 1; mAsyncLoadingResDic.AddValue(resLoadInfo.mResPath, resLoadInfo); ResManager.Instance.LoadAsync(resLoadInfo); } }
public ResObject LoadResSyncForObj(ObjLoadInfo objLoadInfo) { ResObject resObject = ResObject.Allocate(objLoadInfo); ResInfo resInfo = ResManager.Instance.GetRes(objLoadInfo.mCRC); //之前就未加载 if (resInfo == null) { ResLoadInfo resLoadInfo = ResLoadInfo.Allocate(objLoadInfo); resObject.mObj = LoadSync(resLoadInfo); resLoadInfo.Recycle2Cache(); } //之前就加载完成 else if (resInfo.State == ResState.Ready) { resInfo.Retain(); resObject.mObj = resInfo.ResObject; } else { AFLogger.e("同步遇到异步正在或者等待加载的资源 : " + resInfo.ResPath + ",请检查调用代码!"); } return(resObject); }
/// <summary> /// 加载Androidmanifest /// </summary> /// <returns></returns> public bool LoadAssetManifest() { AssetBundleConfig config = null; switch (ABConfig.configWritingMode) { case ConfigWritingMode.Binary: if (FileHelper.JudgeFilePathExit(GetABConfigLocalPath())) { if (configAB != null) { configAB.Unload(false); } configAB = AssetBundle.LoadFromFile(GetABConfigLocalPath()); TextAsset textAsset = configAB.LoadAsset <TextAsset>(ABConfigName); if (textAsset == null) { Debug.LogError("AssetBundleConfig is no exist!"); return(false); } //反序列化,得到打包的信息 MemoryStream stream = new MemoryStream(textAsset.bytes); BinaryFormatter bf = new BinaryFormatter(); config = (AssetBundleConfig)bf.Deserialize(stream); stream.Close(); } else { AFLogger.e("AssetbundleConfig文件不存在"); return(false); } break; case ConfigWritingMode.TXT: string abJson = ""; if (ABConfig.ABResLoadfrom == ABResLoadFrom.PersistentDataPathAB) { abJson = FileHelper.ReadTxtToStr(GetABConfigLocalPath()); if (abJson.Equals("")) { AFLogger.e("AssetbundleConfig文件不存在或者内容为空"); return(false); } } else if (ABConfig.ABResLoadfrom == ABResLoadFrom.StreamingAssetAB) { string abConfigPath = "AF-ABForLocal/AF-InfoFile" + GetVersionStr() + "/" + GetStrByPlatform() + ABConfigName; TextAsset textAsset = ResManager.Instance.LoadSync(ResLoadInfo.Allocate(ResFromType.ResourcesRes, abConfigPath, false)) as TextAsset; abJson = textAsset.text; } config = SerializeHelper.FromJson <AssetBundleConfig>(abJson); break; case ConfigWritingMode.XML: XmlDocument xml = new XmlDocument(); MemoryStream ms = null; if (ABConfig.ABResLoadfrom == ABResLoadFrom.PersistentDataPathAB) { xml.Load(GetABConfigLocalPath()); } else { string abConfigPath = "AF-ABForLocal/AF-InfoFile" + GetVersionStr() + "/" + GetStrByPlatform() + ABConfigName; TextAsset textAsset = ResManager.Instance.LoadSync(ResLoadInfo.Allocate(ResFromType.ResourcesRes, abConfigPath, false)) as TextAsset; //由于编码问题,要设置编码方式 byte[] encodeString = System.Text.Encoding.UTF8.GetBytes(textAsset.text); //以流的形式来读取 ms = new MemoryStream(encodeString); xml.Load(ms); } if (xml == null) { AFLogger.e("AssetbundleConfig文件不存在或者内容为空"); return(false); } XmlSerializer xmldes = new XmlSerializer(typeof(AssetBundleConfig)); StringReader sr = new StringReader(xml.InnerXml); config = (AssetBundleConfig)xmldes.Deserialize(sr); if (ms != null) { ms.Close(); ms.Dispose(); } if (sr != null) { sr.Close(); sr.Dispose(); } break; } ResManager.Instance.CacheABConfig(config); return(true); }