/// <summary> /// Resource异步加载; /// </summary> /// <typeparam name="T">ctrl</typeparam> /// <param name="type">资源类型</param> /// <param name="assetName">资源名字</param> /// <param name="action">资源回调</param> /// <param name="progress">进度回调</param> /// <returns></returns> public IEnumerator <float> LoadResAsync <T>(AssetType type, string assetName, Action <T> action, Action <float> progress) where T : Object { string path = FilePathUtility.GetResourcePath(type, assetName); IAssetLoader <T> loader = CreateLoader <T>(type); T ctrl = null; bool isInstance = false; if (path != null) { ResourceRequest request = Resources.LoadAsync <T>(path); while (request.progress < 0.99) { if (progress != null) { progress(request.progress); } yield return(Timing.WaitForOneFrame); } while (!request.isDone) { yield return(Timing.WaitForOneFrame); } ctrl = loader.GetAsset(request.asset as T, out isInstance); } if (action != null) { action(ctrl); } else { LogUtil.LogUtility.PrintError(string.Format("[ResourceMgr]LoadResAsync Load Asset {0} failure!", assetName + "." + type.ToString())); } }
/// <summary> /// 通用资源AssetBundle卸载方法[Unload(true)]; /// </summary> /// <param name="type">资源类型</param> /// <param name="assetName">资源名字</param> public void UnloadAsset(AssetType type, string assetName) { if (type == AssetType.Non || type == AssetType.Shader || type == AssetType.Lua || type == AssetType.Scripts || string.IsNullOrEmpty(assetName)) { return; } string assetBundleName = FilePathUtility.GetAssetBundleFileName(type, assetName); string[] DependentAssetBundle = Manifest.GetAllDependencies(assetBundleName); foreach (string tempAssetBundle in DependentAssetBundle) { if (tempAssetBundle == FilePathUtility.GetAssetBundleFileName(AssetType.Shader, "Shader")) { continue; } string tempPtah = FilePathUtility.AssetBundlePath + tempAssetBundle; UnloadAsset(tempPtah, true); } string assetBundlePath = FilePathUtility.GetAssetBundlePath(type, assetName); if (assetBundlePath != null) { UnloadAsset(assetBundlePath, true); } }
/// <summary> /// AssetBundle同步加载; /// </summary> /// <param name="type">资源类型</param> /// <param name="assetName">资源名字</param> /// <returns>AssetBundle</returns> public AssetBundle LoadAssetBundleSync(AssetType type, string assetName) { if (type == AssetType.Non || string.IsNullOrEmpty(assetName)) { return(null); } string assetBundlePath = FilePathUtility.GetAssetBundlePath(type, assetName); if (assetBundlePath == null) { return(null); } string assetBundleName = FilePathUtility.GetAssetBundleFileName(type, assetName); AssetBundle assetBundle = LoadSingleSync(assetBundlePath); if (assetBundle == null) { return(null); } //返回AssetBundleName; string[] DependentAssetBundle = Manifest.GetAllDependencies(assetBundleName); foreach (string tempAssetBundle in DependentAssetBundle) { if (tempAssetBundle == FilePathUtility.GetAssetBundleFileName(AssetType.Shader, "Shader")) { continue; } string tempPtah = FilePathUtility.AssetBundlePath + tempAssetBundle; LoadSingleSync(tempPtah); } return(assetBundle); }
private void SetLuaAssetName() { AssetImporter importer = AssetImporter.GetAtPath(FilePathUtility.luaPath); if (importer != null) { importer.assetBundleName = FilePathUtility.GetAssetBundleFileName(AssetType.Lua, "lua"); AssetDatabase.ImportAsset(FilePathUtility.luaPath); } }
/// <summary> /// AssetBundle 镜像卸载方法[Unload(false)],使用资源为一般初始化就全局保存不在销毁的资源,如:Shader; /// </summary> /// <param name="type">资源类型</param> /// <param name="assetName">资源名字</param> public void UnloadMirroring(AssetType type, string assetName) { if (type == AssetType.Non || type == AssetType.Scripts || string.IsNullOrEmpty(assetName)) { return; } string assetBundlePath = FilePathUtility.GetAssetBundlePath(type, assetName); if (assetBundlePath != null) { UnloadAsset(assetBundlePath, false); } }
/// <summary> /// Resource同步加载; /// </summary> /// <typeparam name="T">ctrl</typeparam> /// <param name="type">资源类型</param> /// <param name="assetName">资源名字</param> /// <returns>ctrl</returns> public T LoadResSync <T>(AssetType type, string assetName) where T : Object { string path = FilePathUtility.GetResourcePath(type, assetName); IAssetLoader <T> loader = CreateLoader <T>(type); bool isInstance = false; if (path != null) { T ctrl = Resources.Load <T>(path); if (ctrl != null) { return(loader.GetAsset(ctrl, out isInstance)); } } LogUtil.LogUtility.PrintError(string.Format("[ResourceMgr]LoadResSync Load Asset {0} failure!", assetName + "." + type.ToString())); return(null); }
/// <summary> /// AssetBundle异步加载; /// </summary> /// <param name="type">资源类型</param> /// <param name="assetName">资源名字</param> /// <param name="action">AssetBundle回调</param> /// <param name="progress">progress回调</param> /// <returns></returns> public IEnumerator <float> LoadAssetBundleAsync(AssetType type, string assetName, Action <AssetBundle> action, Action <float> progress) { if (type == AssetType.Non || string.IsNullOrEmpty(assetName)) { yield break; } string assetBundlePath = FilePathUtility.GetAssetBundlePath(type, assetName); if (assetBundlePath == null) { yield break; } string assetBundleName = FilePathUtility.GetAssetBundleFileName(type, assetName); //先加载依赖的AssetBundle; string[] DependentAssetBundle = Manifest.GetAllDependencies(assetBundleName); foreach (string tempAssetBundle in DependentAssetBundle) { if (tempAssetBundle == FilePathUtility.GetAssetBundleFileName(AssetType.Shader, "Shader")) { continue; } string tempPtah = FilePathUtility.AssetBundlePath + tempAssetBundle; IEnumerator <float> itor = LoadSingleAsync(tempPtah, null, null); while (itor.MoveNext()) { yield return(Timing.WaitForOneFrame); } } //加载目标AssetBundle; IEnumerator <float> itorTarget = LoadSingleAsync(assetBundlePath, action, progress); while (itorTarget.MoveNext()) { yield return(Timing.WaitForOneFrame); } }
/// <summary> /// 分析全部资源依赖关系; /// </summary> public void AnalysisAllAsset() { Stopwatch watch = Stopwatch.StartNew();//开启计时; string[] allPath = Directory.GetFiles(FilePathUtility.resPath, "*.*", SearchOption.AllDirectories); //剔除.meta文件; List <string> allAssetPath = new List <string>(); foreach (string tempPath in allPath) { string path = tempPath.Replace("\\", "/"); if (Path.GetExtension(path) == ".meta") { continue; } allAssetPath.Add(path); } //开始分析资源依赖关系; for (int i = 0; i < allAssetPath.Count; i++) { EditorUtility.DisplayProgressBar("依赖关系分析", "全部资源分析进度", (i / allAssetPath.Count)); //还未遍历到该资源; if (!allAsset.ContainsKey(allAssetPath[i])) { allAsset[allAssetPath[i]] = CreateNewAssetNode(allAssetPath[i]); } //获取依赖关系; string[] allDirectDependencies = AssetDatabase.GetDependencies(allAssetPath[i], false); foreach (string tempPath in allDirectDependencies) { //依赖脚本直接添加到脚本队列; if (AssetBundleDefine.GetAssetType(tempPath) == AssetType.Scripts) { continue; } //依赖Shader直接添加到Shader队列; if (AssetBundleDefine.GetAssetType(tempPath) == AssetType.Shader) { allShaderAsset.Add(tempPath); continue; } if (tempPath.Contains(FilePathUtility.resPath)) { //添加依赖的资源信息; allAsset[allAssetPath[i]].sonDependentAssets.Add(tempPath); //添加被依赖的资源信息; if (!allAsset.ContainsKey(tempPath)) { allAsset[tempPath] = CreateNewAssetNode(tempPath); } allAsset[tempPath].parentDependentAssets.Add(allAssetPath[i]); } else { //需要打包AssetBundle的资源目录下的资源,引用非该目录下的资源!!! LogUtil.LogUtility.PrintError("[error Reference] path:" + allAssetPath[i] + " Reference--->>>>: " + tempPath); } } } EditorUtility.ClearProgressBar(); //找出需要打包的资源; for (int i = 0; i < allAssetPath.Count; i++) { EditorUtility.DisplayProgressBar("依赖关系分析", "单一资源分析进度", (i / allAssetPath.Count)); //图集特殊处理; /* * if (allAssetPath[i].Contains("Atlas") && Path.GetExtension(allAssetPath[i]) == ".prefab")//ngui * { * independenceAsset[allAssetPath[i]] = allAsset[allAssetPath[i]]; * continue; * } */ if (allAssetPath[i].Contains("Shaders") && Path.GetExtension(allAssetPath[i]) == ".shader") { allShaderAsset.Add(allAssetPath[i]); continue; } if (allAsset[allAssetPath[i]].parentDependentAssets.Count == 0 || //没有被依赖的资源; allAsset[allAssetPath[i]].parentDependentAssets.Count > 1 || //被超过一个资源依赖的资源; allAssetPath[i].Contains(FilePathUtility.singleResPath)) //指定要求单独打包的资源; { independenceAsset[allAssetPath[i]] = allAsset[allAssetPath[i]]; } } EditorUtility.ClearProgressBar(); //设置资源AssetBundle Name; for (int i = 0; i < allAssetPath.Count; i++) { EditorUtility.DisplayProgressBar("设置资源AssetBundle Name", "AssetBundle Name 设置进度", (i / allAssetPath.Count)); AssetImporter importer = AssetImporter.GetAtPath(allAssetPath[i]); if (importer != null) { if (independenceAsset.ContainsKey(allAssetPath[i])) { importer.assetBundleName = FilePathUtility.GetAssetBundleFileName(independenceAsset[allAssetPath[i]].type, independenceAsset[allAssetPath[i]].assetName); } else { importer.assetBundleName = null; } AssetDatabase.ImportAsset(allAssetPath[i]); } } EditorUtility.ClearProgressBar(); int index = 0; //设置Shader AssetBundle Name; foreach (string tempPath in allShaderAsset) { index++; EditorUtility.DisplayProgressBar("设置Shader AssetBundle Name", "Shader AssetBundle Name 设置进度", (index / allShaderAsset.Count)); AssetImporter importer = AssetImporter.GetAtPath(tempPath); if (importer != null) { importer.assetBundleName = FilePathUtility.GetAssetBundleFileName(AssetType.Shader, "Shader"); AssetDatabase.ImportAsset(tempPath); } } SetLuaAssetName(); SetAtlasName(); EditorUtility.ClearProgressBar(); watch.Stop(); LogUtil.LogUtility.PrintWarning(string.Format("[AssetDependenciesAnalysis]Asset Dependencies Analysis Spend Time:{0}s", watch.Elapsed.TotalSeconds)); AssetDatabase.Refresh(); }
public AssetBundle LoadLuaAssetBundle() { string path = FilePathUtility.GetAssetBundlePath(AssetType.Lua, "lua"); return(LoadSingleSync(path)); }
/// <summary> /// 加载Shader AssetBundle; /// </summary> /// <returns>AssetBundle</returns> public AssetBundle LoadShaderAssetBundle() { string path = FilePathUtility.GetAssetBundlePath(AssetType.Shader, "Shader"); return(LoadSingleSync(path)); }