/// <summary> /// 加载资源 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="abName"></param> /// <param name="objName"></param> /// <returns></returns> private Object LoadFormAssetBundle(string assetName, ManifestItem item, Type t) { Object o = null; AssetBundleWapper abr = null; if (AssetbundleMap.TryGetValue(item.Path, out abr)) { switch ((ManifestItem.AssetTypeEnum)item.Type) { //暂时需要特殊处理的只有一个 case ManifestItem.AssetTypeEnum.SpriteAtlas: { o = abr.LoadTextureFormAtlas(assetName); } break; case ManifestItem.AssetTypeEnum.Prefab: case ManifestItem.AssetTypeEnum.Texture: case ManifestItem.AssetTypeEnum.Others: default: { o = abr.LoadAsset(assetName, t); } break; } } else { BDebug.Log("资源不存在:" + assetName + " - " + item.Path, "red"); return(null); } return(o); }
/// <summary> /// 加载AssetBundle /// </summary> /// <param name="abPath"></param> /// <returns></returns> public AssetBundle LoadAssetBundle(string abPath, int offset = 0) { AssetBundleWapper abw = null; if (AssetbundleMap.TryGetValue(abPath, out abw)) { abw.Use(); return(abw.AssetBundle); } else { //寻找加载路径 var loadPath = FindMultiAddressAsset(abPath); #if UNITY_EDITOR if (!File.Exists(loadPath)) { return(null); } #endif //TODO 这里需要判断Lock列表,异步转同步 var ab = AssetBundle.LoadFromFile(loadPath, 0u, (ulong)offset); //添加 this.AddAssetBundle(abPath, ab); return(ab); } return(null); }
/// <summary> /// 卸载 /// </summary> /// <param name="assetName">根据加载路径卸载</param> /// <param name="isForceUnload">强制卸载</param> public void UnloadAsset(string assetName, bool isForceUnload = false) { //非hash模式,需要debugRuntime // if (!this.AssetConfigLoder.IsHashName) // { // assetName = ZString.Format(DEBUG_RUNTIME, assetName); // } var(assetBundleItem, dependAssetList) = AssetConfigLoder.GetDependAssets(assetName); //添加主资源一起卸载 dependAssetList.Add(assetBundleItem); //卸载 for (int i = 0; i < dependAssetList.Count; i++) { var assetPath = dependAssetList[i].AssetBundlePath; AssetBundleWapper abw = null; if (AssetbundleMap.TryGetValue(assetPath, out abw)) { if (isForceUnload) { abw.UnLoad(); } else { abw.Unuse(); } } } }
/// <summary> /// 卸载 /// </summary> /// <param name="path">根据加载路径卸载</param> /// <param name="isForceUnload">强制卸载</param> public void UnloadAsset(string path, bool isForceUnload = false) { if (!this.loder.Manifest.IsHashName) { path = ZString.Format(RUNTIME, path); } var assetList = loder.Manifest.GetDependenciesByName(path); if (assetList == null) { return; } //卸载 for (int i = 0; i <= assetList.Count; i++) { var assetPath = assetList[i]; AssetBundleWapper abw = null; if (AssetbundleMap.TryGetValue(assetPath, out abw)) { if (isForceUnload) { abw.UnLoad(); } else { abw.Unuse(); } } } }
/// <summary> /// 加载AssetBundle文件 /// </summary> /// <param name="assetbundleFileName">ab文件名</param> /// <returns></returns> public void LoadAssetBundle(string assetbundleFileName, int offset = 0) { AssetBundleWapper abw = GetAssetBundleFromCache(assetbundleFileName); if (abw == null) { //取消加载任务 this.CancelUnloadTask(assetbundleFileName); //寻找加载路径 var abLocalPath = FindMultiAddressAsset(assetbundleFileName); #if UNITY_EDITOR if (!File.Exists(abLocalPath)) { return; } #endif //这里需要判断task列表,异步转同步 var loadTask = GetExsitLoadTask(abLocalPath); if (loadTask != null) { if (loadTask.IsAsyncTask) { loadTask.ToSynchronizationTask(); BDebug.Log("【AssetbundleV2】异步转同步:" + loadTask.LocalPath); } else { BDebug.LogError("【AssetbundleV2】同步任务调度错误~"); } } else { //同步也要创建任务 loadTask = new LoadTask(abLocalPath, 0u, (ulong)offset); AddGlobalLoadTask(loadTask); { //同步加载 loadTask.Load(); } RemoveGlobalLoadTask(loadTask); } #if UNITY_EDITOR if (loadTask.AssetBundle == null) { Debug.LogError($"【AssetBundleV2】 ab加载失败{loadTask.LocalPath}"); } #endif //添加 abw = this.AddAssetBundleToCache(assetbundleFileName, loadTask.AssetBundle); } //使用 abw.Use(); }
/// <summary> /// ab包计数器 /// </summary> /// <param name="assetPath"></param> /// <param name="ab"></param> public void AddAssetBundle(string assetPath, AssetBundle ab) { AssetBundleWapper abw = null; // if (!AssetbundleMap.TryGetValue(assetPath, out abw)) { abw = new AssetBundleWapper(ab); AssetbundleMap[assetPath] = abw; } abw.Use(); }
/// <summary> /// 添加ab到缓存 /// </summary> /// <param name="assetbundleFileName">这里是ab文件名,不是路径</param> /// <param name="ab"></param> public AssetBundleWapper AddAssetBundleToCache(string assetbundleFileName, AssetBundle ab) { AssetBundleWapper abw = null; // if (!AssetbundleCacheMap.TryGetValue(assetbundleFileName, out abw)) { abw = new AssetBundleWapper(ab); AssetbundleCacheMap[assetbundleFileName] = abw; } return(abw); }
/// <summary> /// ab包计数器 /// </summary> /// <param name="hash"></param> /// <param name="ab"></param> private void AddAssetBundle(string hash, AssetBundle ab) { // if (!AssetbundleMap.ContainsKey(hash)) { AssetBundleWapper abr = new AssetBundleWapper() { AssetBundle = ab }; AssetbundleMap[hash] = abr; } AssetbundleMap[hash].Use(); }
/// <summary> /// ab包计数器 /// </summary> /// <param name="assetPath"></param> /// <param name="ab"></param> public void AddAssetBundle(string assetPath, AssetBundle ab) { // if (!AssetbundleMap.ContainsKey(assetPath)) { AssetBundleWapper abr = new AssetBundleWapper() { AssetBundle = ab }; AssetbundleMap[assetPath] = abr; } AssetbundleMap[assetPath].Use(); }
/// <summary> /// 加载资源 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="abName"></param> /// <param name="objName"></param> /// <returns></returns> private Object LoadFormAssetBundle(string assetName, AssetBundleItem item, Type t) { Object o = null; AssetBundleWapper abr = null; if (AssetbundleMap.TryGetValue(item.AssetBundlePath, out abr)) { //var assetType = this.assetConfigLoder.AssetTypeList[item.AssetType]; //优先处理图集 if (item.AssetType == this.AssetConfigLoder.TYPE_SPRITE_ATLAS) { o = abr.LoadTextureFormAtlas(assetName); } //其他需要处理的资源类型,依次判断. else { o = abr.LoadAsset(assetName, t); } // switch ((AssetBundleItem.AssetTypeEnum)item.AssetType) // { // //暂时需要特殊处理的只有一个 // case AssetBundleItem.AssetTypeEnum.SpriteAtlas: // { // // } // break; // case AssetBundleItem.AssetTypeEnum.Prefab: // case AssetBundleItem.AssetTypeEnum.Texture: // case AssetBundleItem.AssetTypeEnum.Others: // default: // { // // } // break; // } } else { BDebug.Log("资源不存在:" + assetName + " - " + item.AssetBundlePath, "red"); return(null); } return(o); }
/// <summary> /// 加载AssetBundle /// </summary> /// <param name="path"></param> /// <returns></returns> public AssetBundle LoadAssetBundle(string path) { AssetBundleWapper abw = null; if (AssetbundleMap.TryGetValue(path, out abw)) { abw.Use(); return(abw.AssetBundle); } else { var p = FindMultiAddressAsset(path); var ab = AssetBundle.LoadFromFile(p); //添加 this.AddAssetBundle(path, ab); return(ab); } return(null); }
/// <summary> /// 加载AssetBundle /// </summary> /// <param name="path"></param> /// <returns></returns> private AssetBundle LoadAssetBundle(string path) { if (AssetbundleMap.ContainsKey(path)) { AssetBundleWapper abw = null; if (AssetbundleMap.TryGetValue(path, out abw)) { abw.Use(); return(abw.AssetBundle); } } else { var p = FindAsset(path); var ab = AssetBundle.LoadFromFile(p); //添加 AddAssetBundle(path, ab); return(ab); } return(null); }