/// <summary>
    /// 分析资源
    /// </summary>
    /// <param name="paths"></param>
    /// <param name="target"></param>
    /// <param name="outpath"></param>
    private static void AnalyzeResource(string[] paths, BuildTarget target, string outpath)
    {
        additionBuildPackageCache = new List <string>();
        curManifestConfig         = new ManifestConfig();
        fileHashMap = new Dictionary <string, string>();
        //加载

        ManifestConfig lastManifestConfig = null;

        if (File.Exists(configPath))
        {
            lastManifestConfig = new ManifestConfig(File.ReadAllText(configPath));
        }
        else
        {
            lastManifestConfig = new ManifestConfig();
        }

        //
        if (File.Exists(cachePath))
        {
            AssetCache = new ManifestConfig(File.ReadAllText(cachePath));
        }
        else
        {
            AssetCache = new ManifestConfig();
        }


        /***************************************开始分析资源****************************************/

        //获取图集信息
        CollectSpriteAtlas();
        EditorUtility.DisplayProgressBar("分析资源 -" + target, "收集SpriteAtlas", 0);
        //开始分析资源
        List <string> changeList   = new List <string>();
        float         curIndex     = 0;
        var           allAssetList = paths.ToList();

        for (int index = 0; index < allAssetList.Count; index++)
        {
            var path = allAssetList[index];

            var _path = path.Replace("\\", "/");

            EditorUtility.DisplayProgressBar(
                "分析资源 -" + target,
                "分析:" + Path.GetFileNameWithoutExtension(_path) + "   进度:" + curIndex + "/" + paths.Length,
                curIndex / paths.Length);
            curIndex++;


            //获取被依赖的路径
            var dependsource         = "Assets" + _path.Replace(Application.dataPath, "");
            var allDependObjectPaths = AssetDatabase.GetDependencies(dependsource).ToList();
            dependsource = dependsource.ToLower();

            //检查依赖是否存在,不存在的依赖需要人为剔除,
            for (int i = allDependObjectPaths.Count - 1; i >= 0; i--)
            {
                var dp = allDependObjectPaths[i];
                //全路径
                var fullPath = Application.dataPath + dp.TrimStart("Assets".ToCharArray());
                //
                if (!File.Exists(fullPath))
                {
                    //有可能是文件夹
                    if (!Directory.Exists(fullPath))
                    {
                        Debug.LogError("丢失依赖:" + dp);
                    }
                    //即使文件夹也要移除啊,么得办法
                    allDependObjectPaths.RemoveAt(i);
                }
            }



            GetCanBuildAssets(ref allDependObjectPaths);

            //处理依赖资源打包
            for (int i = 0; i < allDependObjectPaths.Count; i++)
            {
                var dp            = allDependObjectPaths[i];
                var dependObjPath = Application.dataPath + dp.TrimStart("Assets".ToCharArray());

                string uiid = null;

                //这里说明文件已经处理过
                if (fileHashMap.TryGetValue(dependObjPath, out uiid))
                {
                    continue;
                }
                else
                {
                    uiid = GetMD5HashFromFile(dependObjPath);
                    fileHashMap[dependObjPath] = uiid;
                }



                //判断是否打包
                ManifestItem lastItem = null;
                AssetCache.ManifestMap.TryGetValue(dp, out lastItem);
                //已经添加,不用打包
                if (lastItem != null && lastItem.UIID == uiid)
                {
                    //不用打包记录缓存
                    var _last = lastManifestConfig.ManifestMap.Values.ToList().Find((item) => item.UIID == lastItem.UIID);
                    if (_last != null)
                    {
                        curManifestConfig.AddDepend(_last.Name, _last.UIID, _last.Dependencies, _last.PackageName);
                    }
                    continue;
                }
                changeList.Add(dependsource);
                //
                AssetCache.AddDepend(dp, uiid, new List <string>());
                //开始设置abname  用以打包
                AssetImporter ai     = AssetImporter.GetAtPath(dp);
                string        abname = "Assets" + dependObjPath.Replace(Application.dataPath, "");
                //判断是否要打在同一个ab包内
                string packageName = null;
                var    list        = new List <string>();
                //嵌套引用prefab
                if (dp.ToLower() != dependsource && Path.GetExtension(dp).ToLower().Equals(".prefab"))
                {
                    list = AssetDatabase.GetDependencies(abname).ToList();


                    //检查依赖是否存在,不存在的依赖需要人为剔除,
                    for (int n = list.Count - 1; n >= 0; n--)
                    {
                        var _dp = list[n];
                        //全路径
                        var fullPath = Application.dataPath + _dp.TrimStart("Assets".ToCharArray());
                        //
                        if (!File.Exists(fullPath))
                        {
                            Debug.LogError("丢失依赖:" + _dp);

                            list.RemoveAt(i);
                        }
                    }
                    GetCanBuildAssets(ref list);

                    //转换成全小写
                    for (int j = 0; j < list.Count; j++)
                    {
                        list[j] = list[j].ToLower();
                    }
                }

                abname = abname.ToLower();

                if (IsMakePackage(abname, ref packageName))
                {
                    //增量打包时,如果遇到多包合一时,其中某个变动,剩余的也要一次性打出
                    if (!additionBuildPackageCache.Contains(packageName))
                    {
                        var lowPackgeName = packageName.ToLower();
                        var oldAsset      = lastManifestConfig.ManifestMap.Values.ToList().FindAll((item) => item.PackageName == lowPackgeName);
                        foreach (var oa in oldAsset)
                        {
                            AssetImporter _ai = AssetImporter.GetAtPath(oa.Name);
                            if (_ai == null)
                            {
                                Debug.LogError("资源不存在:" + oa.Name);
                                continue;
                            }

                            _ai.assetBundleName    = packageName;
                            _ai.assetBundleVariant = "";
                        }
                        Debug.LogFormat("<color=yellow>多合一打包:{0} , 依赖:{1}</color>", packageName, oldAsset.Count);
                        additionBuildPackageCache.Add(packageName);
                    }
                    //
                    ai.assetBundleName    = packageName;
                    ai.assetBundleVariant = "";
                    //被依赖的文件,不保存其依赖信息
                    if (abname != dependsource)
                    {
                        curManifestConfig.AddDepend(abname, uiid, list, packageName.ToLower());
                    }
                }
                else
                {
                    ai.assetBundleName    = abname;
                    ai.assetBundleVariant = "";
                    //被依赖的文件,不保存其依赖信息
                    if (abname != dependsource) //依赖列表中会包含自己
                    {
                        curManifestConfig.AddDepend(abname, uiid, list);
                    }
                }
            }

            //保存主文件的依赖
            {
                //获取MD5的UIID
                var UIID = GetMD5HashFromFile(_path);
                allDependObjectPaths.Remove(dependsource);
                for (int i = 0; i < allDependObjectPaths.Count; i++)
                {
                    allDependObjectPaths[i] = allDependObjectPaths[i].ToLower();
                }
                //
                string packageName = null;
                if (IsMakePackage(dependsource, ref packageName))
                {
                    //单ab包-多资源模式
                    curManifestConfig.AddDepend(dependsource, UIID, allDependObjectPaths, packageName.ToLower());
                }
                else
                {
                    //单ab包-单资源模式
                    curManifestConfig.AddDepend(dependsource, UIID, allDependObjectPaths);
                }
            }
        }

        EditorUtility.ClearProgressBar();

        Debug.LogFormat("<color=red>本地需要打包数量:{0}</color>", changeList.Count);


        var buidpath = string.Format("{0}/{1}_buid.json", outpath, target.ToString());

        FileHelper.WriteAllText(buidpath, JsonMapper.ToJson(changeList));
        Debug.Log("本地打包保存:" + buidpath);
    }
Ejemplo n.º 2
0
    /// <summary>
    /// 分析资源
    /// </summary>
    /// <param name="paths"></param>
    /// <param name="target"></param>
    /// <param name="outpath"></param>
    private static void AnalyzeResource(string[] paths, BuildTarget target, string outpath)
    {
        curManifestConfig = new ManifestConfig();
        //加载存在的配置
        ManifestConfig lastManifestConfig = null;
        var            lastConfigPath     = IPath.Combine(outpath, "Art/Config.json");

        if (File.Exists(lastConfigPath))
        {
            lastManifestConfig = new ManifestConfig(File.ReadAllText(lastConfigPath));
        }
        else
        {
            lastManifestConfig = new ManifestConfig();
        }

        List <string> changeList = new List <string>();
        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 UIID = GetMD5HashFromFile(_path);
            if (string.IsNullOrEmpty(UIID))
            {
                continue;
            }


            List <string> dependAssets = 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;
                }


                var dependObjPath = Application.dataPath + dependPath.TrimStart("Assets".ToCharArray());

                var uiid = GetMD5HashFromFile(dependObjPath);
                if (string.IsNullOrEmpty(uiid))
                {
                    continue;
                }

                string abname = "assets" + dependObjPath.Replace(Application.dataPath, "").ToLower();

                ManifestItem manifestItem = null;
                lastManifestConfig.Manifest.TryGetValue(abname, out manifestItem);
                //last没有或者 uiid不一致被改动,
                if (manifestItem == null || manifestItem.UIID != uiid)
                {
                    ai.assetBundleName    = abname;
                    ai.assetBundleVariant = "";

                    changeList.Add(abname);
                }
                else
                {
                    ai.assetBundleName = null;
                }

                //被依赖的文件,不保存其依赖信息
                if (abname != dependsource.ToLower()) //依赖列表中会包含自己
                {
                    curManifestConfig.AddDepend(abname, uiid, new List <string>());
                }

                dependAssets.Add(abname);
            }


            //保存主文件的依赖
            if (dependAssets.Count > 0)
            {
                dependAssets.Remove(dependsource.ToLower());
                curManifestConfig.AddDepend(dependsource.ToLower(), UIID, dependAssets);
            }
        }



        EditorUtility.ClearProgressBar();
        Debug.Log("本地需要打包资源:" + changeList.Count);
    }
    /// <summary>
    /// 分析资源
    /// </summary>
    /// <param name="paths"></param>
    /// <param name="target"></param>
    /// <param name="outpath"></param>
    private static void AnalyzeResource(string[] paths, BuildTarget target, string outpath)
    {
        var lastConfigPath = IPath.Combine(outpath, "Art/Config.json");

        if (File.Exists(lastConfigPath))
        {
            curManifestConfig = new ManifestConfig(File.ReadAllText(lastConfigPath));
        }
        else
        {
            curManifestConfig = new ManifestConfig();
        }

        List <string> changeList = new List <string>();
        float         curIndex   = 0;

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

            EditorUtility.DisplayProgressBar(
                "分析资源 -" + target,
                "分析:" + Path.GetFileNameWithoutExtension(_path) + "   进度:" + curIndex + "/" + paths.Length,
                curIndex / paths.Length);
            curIndex++;


            var UIID = GetMD5HashFromFile(_path);
            if (string.IsNullOrEmpty(UIID))
            {
                continue;
            }

            //获取被依赖的路径
            var dependsource         = "Assets" + _path.Replace(Application.dataPath, "");
            var allDependObjectPaths = AssetDatabase.GetDependencies(dependsource).ToList();
            dependsource = dependsource.ToLower();

            List <string> dependAssets = new List <string>();

            GetCanBuildAssets(ref allDependObjectPaths);

            //判断是否重新打包
            ManifestItem lastItem = null;
            curManifestConfig.Manifest.TryGetValue(dependsource, out lastItem);
            //对比列表

            if (lastItem != null && lastItem.UIID == UIID)
            {
                continue;
            }

            //处理依赖资源打包
            for (int i = 0; i < allDependObjectPaths.Count; i++)
            {
                var dp = allDependObjectPaths[i];
                //脚本不打包
                AssetImporter ai = AssetImporter.GetAtPath(dp);
                if (ai == null)
                {
                    Debug.Log("资源不存在:" + dp);
                    continue;
                }

                var dependObjPath = Application.dataPath + dp.TrimStart("Assets".ToCharArray());
                var uiid          = GetMD5HashFromFile(dependObjPath);
                if (string.IsNullOrEmpty(uiid))
                {
                    continue;
                }

                string abname = "Assets" + dependObjPath.Replace(Application.dataPath, "");

                //判断是否要打在同一个包内
                string packageName = null;
                var    list        = new List <string>();

                //嵌套引用prefab
                if (dp.ToLower() != dependsource &&
                    Path.GetExtension(dp).ToLower().Equals(".prefab"))
                {
                    list = AssetDatabase.GetDependencies(abname).ToList();
                    GetCanBuildAssets(ref list);

                    //转换成全小写
                    for (int j = 0; j < list.Count; j++)
                    {
                        list[j] = list[j].ToLower();
                    }
                }

                abname = abname.ToLower();
                if (IsMakePackage(abname, ref packageName))
                {
                    ai.assetBundleName    = packageName;
                    ai.assetBundleVariant = "";
                    //被依赖的文件,不保存其依赖信息
                    if (abname != dependsource) //依赖列表中会包含自己
                    {
                        curManifestConfig.AddDepend(abname, uiid, list, packageName.ToLower());
                    }
                }
                else
                {
                    ai.assetBundleName    = abname;
                    ai.assetBundleVariant = "";
                    //被依赖的文件,不保存其依赖信息
                    if (abname != dependsource) //依赖列表中会包含自己
                    {
                        curManifestConfig.AddDepend(abname, uiid, list);
                    }
                }
                dependAssets.Add(abname);
            }

            //
            changeList.Add(dependsource);
            //保存主文件的依赖
            if (dependAssets.Count > 0)
            {
                dependAssets.Remove(dependsource);
                string packageName = null;
                if (IsMakePackage(dependsource, ref packageName))
                {
                    //单ab包 多资源模式
                    curManifestConfig.AddDepend(dependsource, UIID, dependAssets, packageName.ToLower());
                }
                else
                {
                    //单ab包  单资源模式
                    curManifestConfig.AddDepend(dependsource, UIID, dependAssets);
                }
            }
        }

        EditorUtility.ClearProgressBar();

        Debug.Log("本地需要打包资源:" + changeList.Count);
        if (changeList.Count < 100)
        {
            Debug.Log("本地需要打包资源:" + JsonMapper.ToJson(changeList));
        }
    }
Ejemplo n.º 4
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);
    }
Ejemplo n.º 5
0
    private static void AnalyzeResource(string[] paths, BuildTarget target, string outpath)
    {
        manifestConfig = new ManifestConfig();
        List <string> changeList = new List <string>();
        //需要保留的ab UID集合
        Dictionary <string, string> saveCache = new Dictionary <string, string>();

        abCacheDict = GetABCache(outpath);
        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 Uiid = GetMD5HashFromFile(_path);
            //
            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;
                }


                var    fullpath  = Application.dataPath + dependPath.TrimStart("Assets".ToCharArray());
                var    uid       = GetMD5HashFromFile(fullpath);
                string derictory = "assets" + fullpath.Replace(Application.dataPath, "").ToLower();

                string cacheid;
                if (abCacheDict.TryGetValue(derictory, out cacheid))
                {
                    if (!cacheid.Equals(uid))
                    {
                        ai.assetBundleName     = derictory;
                        ai.assetBundleVariant  = "";
                        abCacheDict[derictory] = uid;
                        changeList.Add(derictory);
                    }
                    else
                    {
                        //已经设置过abname的
                        if (!changeList.Contains(derictory))
                        {
                            ai.assetBundleName = "";
                        }
                    }
                }
                else
                {
                    ai.assetBundleName     = derictory;
                    ai.assetBundleVariant  = "";
                    abCacheDict[derictory] = uid;
                    changeList.Add(derictory);
                }

                if (!saveCache.ContainsKey(derictory))
                {
                    saveCache.Add(derictory, uid);
                }
                else
                {
                    saveCache[derictory] = uid;
                }


                newAssets.Add(derictory);
            }


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

        //保存用到的ab
        abCacheDict = SaveCache(saveCache, outpath);
        //移除没用到ab
        RemoveUnuseAb(outpath);

        EditorUtility.ClearProgressBar();
        Debug.Log("本地需要打包资源:" + changeList.Count);
    }