/// <summary> /// 资源引用数减少(该资源的依赖也会减少) /// </summary> /// <param name="path"></param> public void DestoryAssetsCounter(string path) { if (assetsCaches.ContainsKey(path)) { AssetsData assets = assetsCaches[path]; assets.refCount--; if (assets.refCount < 0) { Debug.LogError("资源引用计数错误:(" + assets.refCount + ") " + assets.assetPath); assets.refCount = 0; } if (assets.refCount <= 0) { AssetsUnloadHandler.DiscardAssets(assets); } string[] dependenciesNames = loader.GetAllDependenciesName(path); //Debug.LogWarning("DestoryAssets:" + name + "=>" + string.Join("\n", dependenciesNames)); foreach (var item in dependenciesNames) { DestoryAssetsCounter(item); } } else { if (m_useCache) { Debug.LogError("未加载资源,不能Destroy :" + path); } } }
private static UnloadAssetInfo MakeUseLogic(string assetName, AssetsData assets = null, bool isHaveDependencies = true) { UnloadAssetInfo info = null; if (noUsedAssetsDic.ContainsKey(assetName)) { info = noUsedAssetsDic[assetName]; noUsedAssetsDic.Remove(assetName); if (noUsedAssetsList.Contains(info)) { noUsedAssetsList.Remove(info); } usedAssetsDic.Add(assetName, info); } else if (usedAssetsDic.ContainsKey(assetName)) { info = usedAssetsDic[assetName]; } else { info = new UnloadAssetInfo(isHaveDependencies, assets); usedAssetsDic.Add(assetName, info); //noUsedAssetsList.Add(info); } if (assets != null) { info.assets = assets; } info.useTimes++; return(info); }
public override IEnumerator LoadAssetsIEnumerator(string path, System.Type resType, CallBack <AssetsData> callBack) { AssetsData rds = null; string s = PathTool.RemoveExtension(path); ResourceRequest ass = null; if (resType != null) { ass = Resources.LoadAsync(s, resType); } else { ass = Resources.LoadAsync(s); } yield return(ass); if (ass.asset != null) { rds = new AssetsData(path); rds.Assets = new UnityEngine.Object[] { ass.asset }; } else { Debug.LogError("加载失败,Path:" + path); } if (callBack != null) { callBack(rds); } yield return(new WaitForEndOfFrame()); }
/// <summary> /// 直接释放资源(引用数为0时起作用) /// </summary> /// <param name="path"></param> public void Release(string path) { AssetsData assets = null; if (assetsCaches.TryGetValue(path, out assets)) { if (assets.refCount <= 0) { UnloadAssets(assets, true); assetsCaches.Remove(path); Debug.LogWarning("彻底释放" + path); } } }
public AssetsData LoadAssetsLogic(string path, CallBackR <bool> checkContainsAssets, CallBackR <AssetsData, string> loadMethod) { LoadAssetsDependencie(path); AssetsData assets = null; if (checkContainsAssets()) { assets = assetsCaches[path]; } else { assets = loadMethod(path); if (assets == null) { Debug.LogError("资源加载失败:" + path); return(assets); } else { if (assetsCaches.ContainsKey(path)) { List <Object> asList = new List <Object>(assetsCaches[path].Assets); foreach (var item in assets.Assets) { if (!asList.Contains(item)) { asList.Add(item); } } assetsCaches[path].Assets = asList.ToArray(); assets = assetsCaches[path]; } else { if (m_useCache) { assetsCaches.Add(path, assets); } } } } if (m_useCache) { assets.refCount++; AssetsUnloadHandler.MarkUseAssets(assets, loader.IsHaveDependencies(path)); } return(assets); }
/// <summary> /// 记录资源的加载 /// </summary> /// <param name="assets"></param> public static void MarkUseAssets(AssetsData assets, bool isHaveDependencies) { if (assets == null) { return; } UnloadAssetInfo info = MakeUseLogic(assets.assetName, assets, isHaveDependencies); //无依赖的资源进入卸载Bundle压缩包的队列 //if (assets.AssetBundle != null && !isHaveDependencies) //{ // info.unloadBundleTime = 5; // if (!unloadBundleQue.ContainsKey(info.assetsName)) // unloadBundleQue.Add(info.assetsName, info); //} }
public override AssetsData LoadAssets <T>(string path) { string s = PathTool.RemoveExtension(path); AssetsData rds = null; T ass = Resources.Load <T>(s); if (ass != null) { rds = new AssetsData(path); rds.Assets = new UnityEngine.Object[] { ass }; } else { Debug.LogError("加载失败,Path:" + path); } return(rds); }
/// <summary> /// 当资源在游戏中无引用时,传过来记录 /// </summary> /// <param name="assets"></param> public static void DiscardAssets(AssetsData assets) { if (!usedAssetsDic.ContainsKey(assets.assetName)) { //Debug.LogError("资源未加载=>:" + assets.assetPath); return; } UnloadAssetInfo info = null; info = usedAssetsDic[assets.assetName]; usedAssetsDic.Remove(assets.assetName); info.discardTime = Time.realtimeSinceStartup; noUsedAssetsDic.Add(info.assetsName, info); noUsedAssetsList.Add(info); noUsedAssetsList.Sort(SortNoUsedAssetsInfo); }
/// <summary> /// 加载资源 /// 注意释放资源,方法: DestoryAssetsCounter /// </summary> /// <typeparam name="T"></typeparam> /// <param name="name"></param> /// <returns></returns> public static T Load <T>(string name) where T : Object { T res = null; string path = ResourcesConfigManager.GetLoadPath(m_loadType, name); AssetsData assets = loadAssetsController.LoadAssets <T>(path); Debug.Log(assets.assetName + ":" + assets.GetObjectsMemorySize()); if (assets != null) { res = assets.GetAssets <T>(); } if (res == null) { Debug.LogError("Error=> Load Name :" + name + " Type:" + typeof(T).FullName + "\n" + " Load Object:" + res); } return(res); }
public void UnloadAssets(AssetsData assets, bool isForceAB) { if (!m_useCache) { return; } if (assets.Assets != null && isForceAB) { foreach (var item in assets.Assets) { Debug.LogWarning("释放资源" + item); UnloadObject(item); } assets.Assets = null; } //if (assets.AssetBundle != null) // assets.AssetBundle.Unload(isForceAB); }
/// <summary> /// 异步加载资源逻辑 /// </summary> /// <param name="path"></param> /// <param name="assetType"></param> /// <param name="callBack"></param> /// <returns></returns> private IEnumerator LoadAssetsIEnumerator(string path, System.Type assetType, CallBack <Object> callBack) { yield return(LoadAssetsIDependencieEnumerator(path)); if (assetsCaches.ContainsKey(path)) { AssetsData assets = assetsCaches[path]; if (m_useCache) { assets.refCount++; AssetsUnloadHandler.MarkUseAssets(assets, loader.IsHaveDependencies(path)); } if (callBack != null) { callBack(assets.Assets[0]); } } else { yield return(loader.LoadAssetsIEnumerator(path, assetType, (assets) => { if (m_useCache) { assetsCaches.Add(path, assets); } if (m_useCache) { assets.refCount++; AssetsUnloadHandler.MarkUseAssets(assets, loader.IsHaveDependencies(path)); } if (callBack != null) { callBack(assets.Assets[0]); } })); } yield return(0); }
public UnloadAssetInfo(bool isHaveDependencies, AssetsData assets) { this.assetsName = assets.assetName; this.isHaveDependencies = isHaveDependencies; this.assets = assets; }