/// <summary> /// 根据路径创建新的资源; /// </summary> /// <param name="path">资源路径</param> /// <returns>资源节点</returns> public AssetNode CreateNewAssetNode(string path) { return(new AssetNode() { type = AssetBundleDefine.GetAssetType(path), assetName = Path.GetFileNameWithoutExtension(path), assetPath = path, parentDependentAssets = new HashSet <string>(), sonDependentAssets = new HashSet <string>() }); }
/// <summary> /// 根据路径创建新的资源; /// </summary> /// <param name="path">资源路径</param> /// <returns>资源节点</returns> public AssetNode CreateNewAssetNode(string path) { return(new AssetNode() { type = AssetBundleDefine.GetAssetType(path), assetName = path.Replace(FilePathHelper.resPath, ""), assetPath = path, parentDependentAssets = new HashSet <string>(), sonDependentAssets = new HashSet <string>() }); }
/// <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(); }