Exemple #1
0
        /// <summary>
        /// 分析全部资源依赖关系;
        /// </summary>
        /// <returns></returns>
        public List <AssetBundleBuild> AnalysisAllAsset()
        {
            Stopwatch watch = Stopwatch.StartNew();//开启计时;

            string[] allPath = Directory.GetFiles(FilePathHelper.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++)
            {
                if (!CheckAssetNode(allAssetPath[i]))
                {
                    continue;
                }

                //还未遍历到该资源;
                if (!allAsset.ContainsKey(allAssetPath[i]))
                {
                    allAsset[allAssetPath[i]] = CreateNewAssetNode(allAssetPath[i]);
                }

                //获取依赖关系;
                string[] allDirectDependencies = AssetDatabase.GetDependencies(allAssetPath[i], false);

                foreach (string tempPath in allDirectDependencies)
                {
                    if (!CheckAssetNode(tempPath))
                    {
                        continue;
                    }

                    //添加依赖的资源信息;
                    allAsset[allAssetPath[i]].sonDependentAssets.Add(tempPath);
                    //添加被依赖的资源信息;
                    if (!allAsset.ContainsKey(tempPath))
                    {
                        allAsset[tempPath] = CreateNewAssetNode(tempPath);
                    }
                    allAsset[tempPath].parentDependentAssets.Add(allAssetPath[i]);
                }
            }

            foreach (var tempAsset in allAsset)
            {
                if (tempAsset.Value.parentDependentAssets.Count == 0 || //没有被依赖的资源;
                    tempAsset.Value.parentDependentAssets.Count > 1 ||  //被超过一个资源依赖的资源;
                    tempAsset.Key.Contains(FilePathHelper.resPath))     //Bundles资源目录下的资源,允许加载所以单独打包;
                {
                    independenceAsset[tempAsset.Key] = tempAsset.Value;
                }
            }

            //TODO:AssetBundleBuild修改为可以包含多个资源.
            List <AssetBundleBuild> builderList = new List <AssetBundleBuild>();

            foreach (var asset in independenceAsset)
            {
                var node = asset.Value;
                AssetBundleBuild build = new AssetBundleBuild
                {
                    assetBundleName = FilePathHelper.GetAssetBundleFileName(node.assetPath)
                };
                List <string> 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);
            }

            AssetBundleBuild shaderBuild = new AssetBundleBuild
            {
                assetBundleName = FilePathHelper.GetAssetBundleFileName(FilePathHelper.shaderAssetBundleName)
            };
            List <string> shaderList = new List <string>();

            foreach (var shader in allShaderAsset)
            {
                shaderList.Add(shader);
            }
            shaderBuild.assetNames = shaderList.ToArray();
            builderList.Add(shaderBuild);

            AssetBundleBuild luaBuild = new AssetBundleBuild
            {
                assetBundleName = FilePathHelper.GetAssetBundleFileName(FilePathHelper.luaAssetBundleName)
            };
            List <string> 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);
        }