public void CleanAllUsing() { if (_usingAsset == null) { return; } for (int i = _usingAsset.Count - 1; i >= 0; i--) { BaseAsset ba = _usingAsset[i]; if (!AssetProcessor.ProcessDestroy(ba)) { continue; } if (ba.Resource != null) { ResService.UnloadResource(ba.Resource); ba.Resource = null; } ba.BaseData.Factory.DestroyObject(ba); // _usingAsset.RemoveAt(i); } }
/// <summary> /// 添加资产 /// </summary> /// <param name="ba">资产</param> public void AddAsset(BaseAsset ba) { if (ba == null || string.IsNullOrEmpty(ba.BaseData.Name)) { JW.Common.Log.LogE("AssetCache.AddAsset : invalid parameter"); return; } JWArrayList <BaseAsset> ast; if (!_assets.TryGetValue(ba.BaseData.Name, out ast)) { ast = new JWArrayList <BaseAsset>(); _assets.Add(ba.BaseData.Name, ast); } AssetProcessor.ProcessRestore(ba); if (ba.RootGo != null) { ba.RootGo.ExtSetActive(false); } if (ba.RootTf != null) { ba.RootTf.SetParent(_parentTf, false); } ast.Add(ba); }
/// <summary> /// 销毁 /// </summary> public void Destroy() { foreach (KeyValuePair <string, JWArrayList <BaseAsset> > ast in _assets) { for (int i = 0; i < ast.Value.Count; i++) { BaseAsset ba = ast.Value[i]; UIFormAsset oa = ba as UIFormAsset; if (oa != null) { oa.OnFormAssetDestroy(); } if (ba.RootGo != null) { ba.RootGo.ExtDestroy(); } if (ba.Resource != null) { ResService.UnloadResource(ba.Resource); } } } _assets = null; _parentTf.gameObject.ExtDestroy(); _parentTf = null; }
/// <summary> /// 卸载 /// </summary> /// <param name="ba">资源</param> public void Unload(BaseAsset ba) { if (ba == null) { return; } if (_assetManager != null) { _assetManager.Unload(ba, SingletonState == SingletonStateEnum.Destroying); } else { if (!AssetProcessor.ProcessDestroy(ba)) { return; } if (ba.Resource != null) { ResService.UnloadResource(ba.Resource); ba.Resource = null; } ba.BaseData.Factory.DestroyObject(ba); } }
/// <summary> /// 装载具体窗口对象 /// </summary> /// <typeparam name="T">对象类型</typeparam> /// <param name="life">生命周期</param> /// <returns>对象</returns> public UIFormAsset LoadFormAsset <T>(int life = LifeType.Immediate) where T : UIFormAsset, new() { //创建 T instance = new T(); Type t = typeof(T); AssetData data; data.Priority = LoadPriority.Wait; data.Callback = null; data.Type = AssetType.UIForm; data.Name = t.Name; data.Filename = instance.GetPath(); data.Factory = AssetClassFactory.GetUIFormAssetClassFactory(instance); data.Life = life; data.StateName = string.Empty; data.Count = 1; // BaseAsset ba = _assetManager.Load(ref data, false); if (ba == null) { JW.Common.Log.LogE("AssetService.LoadAsset : failed to load asset - {0}", t.Name); return(null); } // AdjustAssetLife(ba, life); return((UIFormAsset)ba); }
/// <summary> /// 获取所有的UI状态名 /// </summary> /// <param name="uiStateName">返回UI状态名</param> public void GetUIState(ref JWArrayList <string> uiStateName) { Dictionary <string, JWArrayList <BaseAsset> > .Enumerator enumerator = _assets.GetEnumerator(); while (enumerator.MoveNext()) { JWArrayList <BaseAsset> val = enumerator.Current.Value; if (val == null) { continue; } for (int i = 0; i < val.Count; i++) { BaseAsset ba = val[i]; if (ba == null || ba.BaseData.Life != LifeType.UIState || string.IsNullOrEmpty(ba.BaseData.StateName)) { continue; } if (uiStateName.IndexOf(ba.BaseData.StateName, StringComparer.OrdinalIgnoreCase) == -1) { uiStateName.Add(ba.BaseData.StateName); } } } }
/// <summary> /// 关联资源 /// </summary> /// <param name="ba"></param> public static void ProcessAssociateResource(BaseAsset ba) { if (ba == null) { JW.Common.Log.LogE("AssetProcessor.ProcessAssociateResource : invalid parameter"); return; } }
private void AdjustAssetLife(BaseAsset ba, int life) { if (life >= ba.BaseData.Life) { return; } ba.BaseData.Life = life; ba.BaseData.StateName = GetLifeStateName(life); }
/// <summary> /// 添加缓存 /// </summary> /// <param name="data">资产数据</param> /// <param name="resource">资源</param> /// <returns>添加到缓存的资产(null标识添加失败)</returns> public BaseAsset AddCache(AssetData data, ResObj resource) { BaseAsset ba = CreateAsset(data, resource); if (ba == null) { return(null); } _cache.AddAsset(ba); return(ba); }
/// <summary> /// 销毁 /// </summary> /// <param name="ba"></param> private static void DestroyAsset(BaseAsset ba) { if (ba.RootGo != null) { ba.RootGo.ExtSetActive(false); ba.RootGo.ExtDestroy(); } ba.RootGo = null; ba.RootTf = null; ba.RootTf = null; ba.RootPLink = null; }
/// <summary> /// 清理 /// </summary> /// <param name="uiState">保留的UI状态名</param> public void Clear(JWArrayList <string> uiState) { if (uiState == null) { JW.Common.Log.LogE("AssetCache.ClearCache : invalid parameter"); return; } Dictionary <string, JWArrayList <BaseAsset> > .Enumerator enumerator = _assets.GetEnumerator(); while (enumerator.MoveNext()) { JWArrayList <BaseAsset> val = enumerator.Current.Value; if (val == null) { continue; } for (int i = val.Count - 1; i >= 0; i--) { BaseAsset ba = val[i]; if (ba == null) { val.RemoveAt(i); continue; } if (!AssetAssistor.IsAssetDead(ref ba.BaseData, uiState)) { continue; } if (AssetProcessor.ProcessDestroy(ba)) { if (ba.Resource != null) { ResService.UnloadResource(ba.Resource); ba.Resource = null; } ba.BaseData.Factory.DestroyObject(ba); } else { JW.Common.Log.LogE("AssetCache.ClearCache : failed to process Destroy - {0}", ba.BaseData.Name); } val.RemoveAt(i); } } }
public BaseAsset GetClass(int type) { if (type < 0 || type >= _cache.Length) { JW.Common.Log.LogE("ResourceClassCache.GetClass : invalid parameter - {0}", type); return(null); } if (_cache[type] == null || _cache[type].Count <= 0) { return(null); } BaseAsset ba = _cache[type][_cache[type].Count - 1]; _cache[type].RemoveAt(_cache[type].Count - 1); return(ba); }
/// <summary> /// 获取资产 /// </summary> /// <param name="name">资产名</param> /// <returns>资产</returns> public BaseAsset GetAsset(string name) { if (string.IsNullOrEmpty(name)) { JW.Common.Log.LogE("AssetCache.GetAsset : invalid parameter"); return(null); } JWArrayList <BaseAsset> ast; if (!_assets.TryGetValue(name, out ast) || ast.Count <= 0) { return(null); } BaseAsset ba = ast[ast.Count - 1]; ast.RemoveAt(ast.Count - 1); return(ba); }
/// <summary> /// 处理回收 /// </summary> /// <param name="ba"></param> public static void ProcessRestore(BaseAsset ba) { if (ba == null) { JW.Common.Log.LogE("AssetProcessor.ProcessRestore : invalid parameter"); return; } //基础重置 if (ba.RootTf != null) { ba.RootTf.localPosition = ba.OrignalPosition; ba.RootTf.localRotation = ba.OrignalRotate; ba.RootTf.localScale = ba.OrignalScale; } // switch (ba.BaseData.Type) { case AssetType.Model: { //ModelAsset ma = ba as ModelAsset; //回收处理 } break; case AssetType.UIForm: { //回收处理 } break; case AssetType.UI: { //回收处理 UIAsset aa = ba as UIAsset; aa.FormCom = null; aa.RootPLink = null; } break; } }
/// <summary> /// 实例化资产 /// </summary> /// <param name="ba"></param> /// <returns></returns> private static bool InstantiateAsset(BaseAsset ba) { if (ba.RootGo == null) { ba.RootGo = (GameObject)ba.Resource.Content.ExtInstantiate(); } if (ba.RootGo == null) { JW.Common.Log.LogE("AssetProcessor.Instantiate : failed to instantiate - {0}", ba.BaseData.Name); return(false); } ba.RootGo.ExtSetActive(false); ba.RootTf = ba.RootGo.transform; ba.OrignalPosition = ba.RootTf.localPosition; ba.OrignalRotate = ba.RootTf.localRotation; ba.OrignalScale = ba.RootTf.localScale; ba.RootPLink = ba.RootGo.GetComponent <PrefabLink>(); // return(true); }
/// <summary> /// 卸载 /// </summary> /// <param name="ba">待卸载的资产</param> /// <param name="forceDestroy">是否强制销毁</param> public void Unload(BaseAsset ba, bool forceDestroy = false) { if (ba == null) { JW.Common.Log.LogE("AssetManager.Unload : invalid parameter"); return; } int found = _usingAsset.IndexOf(ba); if (found == -1) { JW.Common.Log.LogE("AssetManager.Unload : can't find asset - {0}", ba.BaseData.Name); return; } _usingAsset.RemoveAt(found); //销毁手动和Imediate if (forceDestroy || ba.BaseData.Life == LifeType.Immediate || ba.BaseData.Life == LifeType.Manual) { if (!AssetProcessor.ProcessDestroy(ba)) { return; } if (ba.Resource != null) { ResService.UnloadResource(ba.Resource); ba.Resource = null; } ba.BaseData.Factory.DestroyObject(ba); } else { _cache.AddAsset(ba); } }
public bool AddClass(BaseAsset ba) { if (ba == null) { JW.Common.Log.LogE("ResourceClassCache.ReleaseClass : invalid parameter"); return(false); } if (ba.BaseData.Type < 0 || ba.BaseData.Type >= _cache.Length) { return(false); } if (_cache[ba.BaseData.Type] == null) { _cache[ba.BaseData.Type] = new JWArrayList <BaseAsset>(); } _cache[ba.BaseData.Type].Add(ba); return(true); }
/// 加载 private BaseAsset LoadAsset(int type, string filename, int life, bool clone = false) { AssetData data; data.Priority = LoadPriority.Wait; data.Callback = null; data.Type = type; data.Name = filename; //data.Filename = todo Lod FileName data.Filename = filename; data.Factory = AssetClassFactory.GetFactory(type); data.Life = life; data.StateName = GetLifeStateName(life); data.Count = 1; BaseAsset ba = _assetManager.Load(ref data, clone); if (ba == null) { JW.Common.Log.LogE("AssetService.LoadAsset : failed to load asset - {0}", data.Filename); return(null); } if (ba.BaseData.Type != type) { JW.Common.Log.LogE("AssetService.LoadAsset : type mismatch - {0}, type - {1};{2}", data.Filename, ba.BaseData.Type, type); Unload(ba); return(null); } AdjustAssetLife(ba, life); return(ba); }
private BaseAsset CreateAsset(AssetData data, ResObj resource) { if (resource == null || resource.Content == null) { JW.Common.Log.LogE("AssetManager.CreateAsset : invalid parameter - {0}", data.Name); return(null); } data.Callback = null; BaseAsset ba = data.Factory.CreateObject(); if (ba == null) { JW.Common.Log.LogE("AssetManager.CreateAsset : failed to create asset - {0}", data.Name); return(null); } ba.BaseData = data; ba.Resource = resource; if (!AssetProcessor.ProcessCreate(ba)) { JW.Common.Log.LogE("AssetManager.CreateAsset : failed to process create - {0}", data.Name); ba.BaseData.Callback = null; ba.BaseData.Factory = null; ba.Resource = null; data.Factory.DestroyObject(ba); return(null); } //AssetProcessor.ProcessAssociateResource(ba); return(ba); }
/// <summary> /// 装载 /// </summary> /// <param name="data">资产数据</param> /// <param name="clone">是否Clone一个副本</param> /// <returns>资产</returns> public BaseAsset Load(ref AssetData data, bool clone) { if (string.IsNullOrEmpty(data.Filename)) { JW.Common.Log.LogE("AssetManager.Load : invalid parameter"); return(null); } //缓存找 BaseAsset ba = _cache.GetAsset(data.Name); if (ba != null) { //缓存复制 if (clone) { BaseAsset cloneBa = AssetProcessor.ProcessClone(ba); _cache.AddAsset(ba); ba = cloneBa; } if (ba != null) { if (!AssetProcessor.ProcessCreate(ba)) { JW.Common.Log.LogE("AssetManager.CreateAsset : failed to process create - {0}", data.Name); ba.BaseData.Callback = null; ba.BaseData.Factory = null; ba.Resource = null; data.Factory.DestroyObject(ba); return(null); } _usingAsset.Add(ba); } return(ba); } //没在缓存 同步创建 ResObj resource = ResService.GetResource(data.Filename); if (resource == null) { JW.Common.Log.LogE("AssetManager.Load : failed to load resource - {0}", data.Filename); return(null); } if (resource.Content == null) { JW.Common.Log.LogE("AssetManager.Load : failed to load resource - {0}", data.Filename); ResService.UnloadResource(resource); return(null); } ba = CreateAsset(data, resource); if (ba == null) { ResService.UnloadResource(resource); return(null); } if (clone) { BaseAsset cloneBa = AssetProcessor.ProcessClone(ba); _cache.AddAsset(ba); ba = cloneBa; } if (ba != null) { _usingAsset.Add(ba); } return(ba); }
public void DestroyObject(BaseAsset o) { }
/// <summary> /// 资产创建预处理 /// </summary> /// <param name="ba"></param> /// <returns></returns> public static bool ProcessCreate(BaseAsset ba) { if (ba == null || ba.Resource == null || ba.Resource.Content == null) { JW.Common.Log.LogE("AssetProcessor.ProcessCreate : invalid parameter"); return(false); } switch (ba.BaseData.Type) { case AssetType.UIForm: { UIFormAsset ast = ba as UIFormAsset; if (ast == null) { JW.Common.Log.LogE("AssetProcessor.ProcessCreate : type is not object - {0}", ba.BaseData.Name); return(false); } if (!InstantiateAsset(ast)) { return(false); } //逻辑处理 ast.OnFormAssetCreate(); } break; case AssetType.Model: { ModelAsset ast = ba as ModelAsset; if (ast == null) { JW.Common.Log.LogE("AssetProcessor.ProcessCreate : type is not model - {0}", ba.BaseData.Name); return(false); } if (!InstantiateAsset(ast)) { return(false); } ast.Render = ast.RootGo.ExtGetComponentsInChildren <Renderer>(true); ast.AnimationCpt = ast.RootGo.ExtGetComponentInChildren <Animation>(); ast.AnimatorCtrl = ast.RootGo.ExtGetComponentInChildren <Animator>(); } break; case AssetType.UI: { UIAsset ast = ba as UIAsset; if (ast == null) { JW.Common.Log.LogE("AssetProcessor.ProcessCreate : type is not uiasset - {0}", ba.BaseData.Name); return(false); } if (!InstantiateAsset(ba)) { return(false); } ast.FormCom = ast.RootGo.ExtGetComponent <UIForm>(); ast.PLinkCom = ast.RootGo.ExtGetComponent <PrefabLink>(); } break; //基础实例 case AssetType.Instantiate: { if (!InstantiateAsset(ba)) { return(false); } } break; case AssetType.Audio: { AudioAsset ast = ba as AudioAsset; if (ast == null) { JW.Common.Log.LogE("AssetProcessor.ProcessCreate : type is not audioclip - {0}", ba.BaseData.Name); return(false); } ast.Clip = ba.Resource.Content as AudioClip; if (ast.Clip == null) { JW.Common.Log.LogE("AssetProcessor.ProcessCreate : resource is not audioclip - {0}", ba.BaseData.Name); return(false); } } break; case AssetType.Sprite: { SpriteAsset ast = ba as SpriteAsset; if (ast == null) { JW.Common.Log.LogE("AssetProcessor.ProcessCreate : type is not SpriteAsset - {0}", ba.BaseData.Name); return(false); } #if UNITY_EDITOR || !USE_PACK_RES Texture2D tt = ba.Resource.Content as Texture2D; ast.SpriteObj = Sprite.Create(tt, new Rect(0, 0, tt.width, tt.height), new Vector2(0.5f, 0.5f)); #else Sprite ss = ba.Resource.Content as Sprite; if (ss == null) { JW.Common.Log.LogD("SpriteAsset Load From AB Is Texture2D Type"); Texture2D tt1 = ba.Resource.Content as Texture2D; ast.SpriteObj = Sprite.Create(tt1, new Rect(0, 0, tt1.width, tt1.height), new Vector2(0.5f, 0.5f)); } else { ast.SpriteObj = ss; } #endif if (ast.SpriteObj == null) { JW.Common.Log.LogE("AssetProcessor.ProcessCreate : resource is not Sprite - {0}", ba.BaseData.Name); return(false); } } break; } return(true); }
/// <summary> /// 处理销毁 /// </summary> /// <param name="ba"></param> /// <returns></returns> public static bool ProcessDestroy(BaseAsset ba) { if (ba == null) { JW.Common.Log.LogE("AssetProcessor.ProcessDestroy : invalid parameter"); return(false); } switch (ba.BaseData.Type) { case AssetType.UIForm: { UIFormAsset ast = ba as UIFormAsset; if (ast == null) { JW.Common.Log.LogE("AssetProcessor.ProcessDestroy : type is not object - {0}", ba.BaseData.Name); return(false); } ast.OnFormAssetDestroy(); DestroyAsset(ast); } break; case AssetType.Model: { ModelAsset ast = ba as ModelAsset; if (ast == null) { JW.Common.Log.LogE("AssetProcessor.ProcessDestroy : type is not model - {0}", ba.BaseData.Name); return(false); } ast.AnimationCpt = null; ast.AnimatorCtrl = null; DestroyAsset(ast); } break; case AssetType.UI: { UIAsset ast = ba as UIAsset; if (ast == null) { JW.Common.Log.LogE("AssetProcessor.ProcessDestroy : type is not ui - {0}", ba.BaseData.Name); return(false); } ast.FormCom = null; ast.PLinkCom = null; DestroyAsset(ast); } break; case AssetType.Instantiate: { DestroyAsset(ba); } break; case AssetType.Audio: { AudioAsset ast = ba as AudioAsset; if (ast == null) { JW.Common.Log.LogE("AssetProcessor.ProcessDestroy : type is not audio - {0}", ba.BaseData.Name); return(false); } ast.Clip = null; } break; case AssetType.Sprite: { SpriteAsset ast = ba as SpriteAsset; if (ast == null) { JW.Common.Log.LogE("AssetProcessor.ProcessDestroy : type is not SpriteAsset - {0}", ba.BaseData.Name); return(false); } ast.SpriteObj = null; } break; } return(true); }
/// <summary> /// clone处理 /// </summary> /// <param name="s"></param> /// <returns></returns> public static BaseAsset ProcessClone(BaseAsset s) { //todo 上层逻辑 池化 这里禁止clone return(null); /* * if (s == null) * { * JW.Common.Log.LogE("ProcessClone Error Param"); * return null; * } * BaseAsset ba =s.BaseData.Factory.CreateObject(); * if (ba == null) * { * JW.Common.Log.LogE("ProcessClone : failed to create asset - {0}", s.BaseData.Name); * return null; * } * // * ba.BaseData = s.BaseData; * ba.Resource = s.Resource; * //资源对象引用++ * ba.Resource.RefCnt++; * // * if (ba == null || ba.Resource == null || ba.Resource.Content == null) * { * JW.Common.Log.LogE("AssetProcessor.ProcessCreate : invalid parameter"); * return null; * } * // * switch (ba.BaseData.Type) * { * case AssetType.UIForm: * { * //窗口禁止clone * return null; * } * case AssetType.Model: * { * ModelAsset ast = ba as ModelAsset; * if (ast == null) * { * JW.Common.Log.LogE("AssetProcessor.ProcessCreate : type is not model - {0}", ba.BaseData.Name); * return null; * } * if (!InstantiateAsset(ast)) * { * return null; * } * ast.Render = ast.RootGo.ExtGetComponentsInChildren<Renderer>(true); * ast.AnimationCpt = ast.RootGo.ExtGetComponentInChildren<Animation>(); * ast.AnimatorCtrl = ast.RootGo.ExtGetComponentInChildren<Animator>(); * } * break; * //基础实例 * case AssetType.Instantiate: * { * if (!InstantiateAsset(ba)) * { * return null; * } * } * break; * } * return ba; */ }