Ejemplo n.º 1
0
    private static void AnalyzeResource(string[] paths, BuildTarget target, string outpath)
    {
        var configPath = Path.Combine(outpath, "Art/Config.json");

        if (File.Exists(configPath))
        {
            var content = File.ReadAllText(configPath);
            manifestConfig = new ManifestConfig(content);
        }
        else
        {
            manifestConfig = new ManifestConfig();
        }

        int   counter  = 0;
        float curIndex = 0;

        foreach (var path in paths)
        {
            var _path = path.Replace("\\", "/");

            EditorUtility.DisplayProgressBar("分析资源 -" + target.ToString(),
                                             "分析:" + Path.GetFileNameWithoutExtension(_path) + "   进度:" + curIndex + "/" + paths.Length,
                                             curIndex / paths.Length);
            curIndex++;
            //获取被依赖的路径
            var dependsource         = "Assets" + _path.Replace(Application.dataPath, "");
            var allDependObjectPaths = AssetDatabase.GetDependencies(dependsource).ToList();

            var manifestItem = manifestConfig.GetManifestItem(dependsource.ToLower());
            var Uiid         = GetMD5HashFromFile(_path);
            //
            var           isEquals  = manifestItem != null && Uiid == manifestItem.UIID;
            List <string> newAssets = new List <string>();
            //处理依赖资源是否打包
            for (int i = 0; i < allDependObjectPaths.Count; i++)
            {
                //
                var dependPath = allDependObjectPaths[i];
                var ext        = Path.GetExtension(dependPath).ToLower();
                if (ext == ".cs" || ext == ".js")
                {
                    continue;
                }

                //
                AssetImporter ai = AssetImporter.GetAtPath(dependPath);
                if (ai == null)
                {
                    BDebug.Log("not find Resource " + dependPath);
                    continue;
                }


                //重新组建ab名字,带上路径名
                dependPath = Path.GetFullPath(dependPath);
                dependPath = dependPath.Replace("\\", "/");
                //根据是否相等,判断是否打包
                if (isEquals)
                {
                    //本次不打包
                    ai.assetBundleName = null;
                }
                else
                {
                    //本次打包
                    string derictory = "assets" + dependPath.Replace(Application.dataPath, "");
                    ai.assetBundleName = derictory.ToLower();

                    newAssets.Add(ai.assetBundleName);
                    ai.assetBundleVariant = "";
                }
            }


            //将现在的目录结构替换配置中的
            if (newAssets.Count > 0)
            {
                manifestConfig.AddDepend(dependsource.ToLower(), Uiid, newAssets);
                counter++;
            }
        }

        Debug.Log("本地需要打包资源:" + counter);
    }