/// <summary> /// 分析全部资源依赖关系; /// </summary> /// <returns></returns> public List <AssetBundleBuild> AnalysisAllAsset() { var watch = Stopwatch.StartNew();//开启计时; var allPaths = Directory.GetFiles(AssetBundleHelper.ResPath, "*.*", SearchOption.AllDirectories); //剔除.meta文件; var allAssetPaths = new List <string>(); foreach (var tempPath in allPaths) { var path = tempPath.Replace("\\", "/"); if (Path.GetExtension(path) == ".meta") { continue; } allAssetPaths.Add(path); } //开始分析资源依赖关系; for (var i = 0; i < allAssetPaths.Count; i++) { if (!CheckAssetNode(allAssetPaths[i])) { continue; } //还未遍历到该资源; if (!allAsset.ContainsKey(allAssetPaths[i])) { allAsset[allAssetPaths[i]] = CreateNewAssetNode(allAssetPaths[i]); } //获取依赖关系; var allDirectDependencies = AssetDatabase.GetDependencies(allAssetPaths[i], false); foreach (var tempPath in allDirectDependencies) { if (!CheckAssetNode(tempPath)) { continue; } //添加依赖的资源信息; allAsset[allAssetPaths[i]].sonDependentAssets.Add(tempPath); //添加被依赖的资源信息; if (!allAsset.ContainsKey(tempPath)) { allAsset[tempPath] = CreateNewAssetNode(tempPath); } allAsset[tempPath].parentDependentAssets.Add(allAssetPaths[i]); } } foreach (var tempAsset in allAsset) { if (tempAsset.Value.parentDependentAssets.Count == 0 || //没有被依赖的资源; tempAsset.Value.parentDependentAssets.Count > 1 || //被超过一个资源依赖的资源; tempAsset.Key.Contains(AssetBundleHelper.ResPath) || //Bundles资源目录下的资源,允许加载所以单独打包; tempAsset.Value.IsExistSceneInParent()) { independenceAsset[tempAsset.Key] = tempAsset.Value; } } //TODO:AssetBundleBuild修改为可以包含多个资源. var builderList = new List <AssetBundleBuild>(); foreach (var asset in independenceAsset) { var node = asset.Value; var build = new AssetBundleBuild { assetBundleName = AssetBundleHelper.GetAssetBundleFileName(node.assetPath) }; var assetLis = new List <string> { node.assetPath }; foreach (var tempAsset in node.sonDependentAssets) { if (!independenceAsset.ContainsKey(tempAsset)) { assetLis.Add(tempAsset); } } build.assetNames = assetLis.ToArray(); builderList.Add(build); } var shaderBuild = new AssetBundleBuild { assetBundleName = AssetBundleHelper.GetAssetBundleFileName(AssetBundleHelper.ShaderAssetBundleName) }; var shaderList = new List <string>(); foreach (var shader in allShaderAsset) { shaderList.Add(shader); } shaderBuild.assetNames = shaderList.ToArray(); builderList.Add(shaderBuild); var luaBuild = new AssetBundleBuild { assetBundleName = AssetBundleHelper.GetAssetBundleFileName(AssetBundleHelper.LuaAssetBundleName) }; var luaList = new List <string>(); foreach (var lua in allLuaAsset) { luaList.Add(lua); } luaBuild.assetNames = luaList.ToArray(); builderList.Add(luaBuild); watch.Stop(); LogHelper.PrintWarning($"[AssetDependenciesAnalysis]Asset Dependencies Analysis Spend Time:{watch.Elapsed.TotalSeconds}s"); SaveBuildInfo(builderList); return(builderList); }