public void LoadAnimation(string animPath, ResParamLoadCallBack<AnimationClip> loadCB, ResLoadProgressCallBack progressCB, object userParam) { if(string.IsNullOrEmpty(animPath)) return; ResCounter<AnimationClip> res = null; if(m_dictAnimationClip.ContainsKey(animPath)) { res = m_dictAnimationClip[animPath]; if(null != res.Res) { res.AddRef(); if(null != progressCB) progressCB(animPath, 1f, userParam); if(null != loadCB) loadCB(animPath, res.Res, userParam); return; } } else { //get path res = new ResCounter<AnimationClip>(); res.ResPath = animPath; m_dictAnimationClip.Add(animPath, res); m_listLoadingList.AddLast(res); } res.AddRef(); if(null != loadCB || null != progressCB) { res.AddLoadParam(userParam, loadCB, progressCB); } }
void OnAssetLoadCallBack(bool success, ResCounter<AnimationClip> res) { if(!m_dictAnimationClip.ContainsKey(res.ResPath)) { UnLoadRes(res); return; } if(success && null != res.AssetWWW && null != res.AssetWWW.assetBundle) { GameObject gameObj = res.AssetWWW.assetBundle.mainAsset as GameObject; Animation animation = gameObj.animation; if(null != animation) res.Res = animation.clip; res.AssetWWW.assetBundle.Unload(false); } else { m_dictAnimationClip.Remove(res.ResPath); UnLoadRes(res); } if(null != res.AssetWWW) { res.AssetWWW.Dispose(); res.AssetWWW = null; } res.DispatchLoaCallBack(); res.ClearLoadParam(); }
public void LoadEffect(string fxPath, ResParamLoadCallBack<GameObject> loadCB, ResLoadProgressCallBack progressCB, object userParam) { if(string.IsNullOrEmpty(fxPath)) return; ResCounter<GameObject> res = null; if(m_dictGameObject.ContainsKey(fxPath)) { res = m_dictGameObject[fxPath]; if(null != res.Res) { res.AddRef(); if(null != progressCB) progressCB(fxPath, 1f, userParam); if(null != loadCB) loadCB(fxPath, res.Res, userParam); return; } } else { //get path res = new ResCounter<GameObject>(); res.ResPath = fxPath; m_dictGameObject.Add(fxPath, res); m_listLoadingList.AddLast(res); } res.AddRef(); if(null != loadCB || null != progressCB) { res.AddLoadParam(userParam, loadCB, progressCB); } }
void OnAssetLoadCallBack(bool success, ResCounter<GameObject> res) { if(!m_dictGameObject.ContainsKey(res.ResPath)) { UnLoadRes(res); return; } if(success && null == res.Res && null != res.Bundle) { GameObject gameObj = res.Bundle.mainAsset as GameObject; res.Res = gameObj; /* 调用Unload(false)的话会减少asset数量,但是很少,测试只有1,而且只能调用一次,下次调用Unload(true)时,不会卸载资源 */ /* assetBundle.Unload 只能调用一次,为了防止泄露,只在删除模型的时候调用 */ //res.AssetWWW.assetBundle.Unload(false); } else { m_dictGameObject.Remove(res.ResPath); UnLoadRes(res); } if(null != res.AssetWWW) { res.AssetWWW.Dispose(); res.AssetWWW = null; } res.DispatchLoaCallBack(); res.ClearLoadParam(); }
public void LoadTexture(string filePath, ResParamLoadCallBack<Texture2D> loadCB, ResLoadProgressCallBack progressCB, object userParam) { if(string.IsNullOrEmpty(filePath)) return; // add to list first ResCounter<Texture2D> res = null; if(m_dictTexture.ContainsKey(filePath)) { res = m_dictTexture[filePath]; if(null != res.Res) { res.AddRef(); //if(null != progressCB) res.ProgressCallBack += progressCB; //if(null != loadCB) res.LoadCallBack += loadCB; if(null != progressCB) progressCB(res.ResPath, 1f, userParam); if(null != loadCB) loadCB(res.ResPath, res.Res, userParam); return; } /* 如果丢失的话,重新加载,并且清理一下无用资源,防止泄露 */ m_dictTexture.Remove(filePath); Resources.UnloadUnusedAssets(); } else { //get path res = new ResCounter<Texture2D>(); res.ResPath = filePath; m_dictTexture.Add(filePath, res); } res.AddRef(); m_listLoadingList.AddLast(res); //if(null != loadCB) res.LoadCallBack += loadCB; if(null != loadCB || null != progressCB) { res.AddLoadParam(userParam, loadCB, progressCB); } }
void UnLoadRes(ResCounter<AnimationClip> res) { if(null != res.Res) Object.DestroyImmediate(res.Res, true); Resources.UnloadUnusedAssets(); }
void OnAssetFailed(ResCounter<Texture2D> res) { if(null == res) return; /* 即使失败也要调用一下回调函数 */ OnAssetOK(res); Debug.LogException(new Exception("Model Load Failed, filePath = " + res.ResPath)); if(m_dictTexture.ContainsKey(res.ResPath)) { m_dictTexture.Remove(res.ResPath); } UnLoadRes(res); }
void UnLoadRes(ResCounter<Texture2D> res) { //Do Not Destroy Here if(null != res.Res) UnityEngine.Object.DestroyImmediate(res.Res, true); Resources.UnloadUnusedAssets(); }
void OnAssetOK(ResCounter<Texture2D> res) { if(null == res) return; res.DispatchLoaCallBack(); res.ClearLoadParam(); }
void OnAssetLoadCallBack(bool success, ResCounter<Texture2D> res) { if(!m_dictTexture.ContainsKey(res.ResPath)) { UnLoadRes(res); return; } if(!success) { OnAssetFailed(res); return; } if(success) { if(null == res.Res && null != res.AssetWWW && null != res.AssetWWW.assetBundle) { res.Res = res.AssetWWW.assetBundle.mainAsset as Texture2D; if(null == res.Res) { OnAssetFailed(res); return; } } OnAssetOK(res); res.AssetWWW.Dispose(); res.AssetWWW = null; //StartCoroutine(LoadFromBundle(res)); } }
void UnLoadRes(ResCounter<GameObject> res) { if(null == res) return; if(null != res.Res) Object.DestroyImmediate(res.Res, true); if(null != res.Bundle) res.Bundle.Unload(true); Resources.UnloadUnusedAssets(); }
void LoadModel(ModelCfg cfg, ResParamLoadCallBack<Model> loadCB, ResLoadProgressCallBack progressCB, object userParam) { if(null == cfg) return; // add to list first m_listModelCfg.Add(new ModelCfgLoadParam(loadCB, cfg, cfg.FilePath, userParam)); ResCounter<GameObject> res = null; if(m_dictModel.ContainsKey(cfg.FilePath)) { res = m_dictModel[cfg.FilePath]; if(null != res.Res) { res.AddRef(); //if(null != progressCB) res.ProgressCallBack += progressCB; //if(null != loadCB) res.LoadCallBack += loadCB; if(null != progressCB) progressCB(res.ResPath, 1f, userParam); OnAssetOK(res); return; } else { if(null != res.Bundle) { //if(null != loadCB) res.LoadCallBack += loadCB; //if(null != progressCB) res.ProgressCallBack += progressCB; if(null != progressCB) progressCB(res.ResPath, 1f, userParam); OnAssetLoadCallBack(true, res); return; } } } else { //get path res = new ResCounter<GameObject>(); res.ResPath = cfg.FilePath; m_dictModel.Add(cfg.FilePath, res); } res.AddRef(); m_listLoadingList.AddLast(res); if(null != progressCB) { res.AddLoadParam(userParam, null, progressCB); } }
void UnLoadRes(ResCounter<GameObject> res) { //Do Not Destroy Here if(null != res.Res) GameObject.DestroyImmediate(res.Res, true); if(null != res.Bundle) { res.Bundle.Unload(true); } Resources.UnloadUnusedAssets(); }
void OnAssetOK(ResCounter<GameObject> res) { if(null == res) return; for(int i = m_listModelCfg.Count - 1; i >= 0; --i) { // try // { ModelCfgLoadParam param = m_listModelCfg[i]; if(null != param && param.filePath == res.ResPath) { m_listModelCfg.RemoveAt(i); Model m = null; if(null != res.Res) { GameObject objModel = GameObject.Instantiate(res.Res) as GameObject; if(null != objModel) { m = objModel.AddComponent<Model>(); m.SetModelCfg(param.cfg); } } if(param.cb != null) { param.cb(res.ResPath, m, param.userParam); } } // } // catch(Exception e) // { // Debug.LogError("Model OnAssetOK, exception = " + e.Message); // } // // } //res.LoadCallBack(res.ResPath, res.Res); res.ClearLoadParam(); }
void OnAssetLoadCallBack(bool success, ResCounter<GameObject> res) { if(!m_dictModel.ContainsKey(res.ResPath)) { UnLoadRes(res); return; } if(!success) { Debug.LogException(new Exception("Model Load Failed, filePath = " + res.ResPath)); if(m_dictModel.ContainsKey(res.ResPath)) { m_dictModel.Remove(res.ResPath); } UnLoadRes(res); return; } if(success) { if(null == res.Res && null != res.Bundle) { res.Res = res.Bundle.mainAsset as GameObject; } /* 调用Unload(false)的话会减少asset数量,但是很少,测试只有1,而且只能调用一次,下次调用Unload(true)时,不会卸载资源 */ /* assetBundle.Unload 只能调用一次,为了防止泄露,只在删除模型的时候调用 */ //res.AssetWWW.assetBundle.Unload(false); OnAssetOK(res); res.AssetWWW.Dispose(); res.AssetWWW = null; //StartCoroutine(LoadFromBundle(res)); } }