Exemple #1
0
        public static void ClearDepend()
        {
            ProgressBarUtil.Title   = "依赖设置";
            ProgressBarUtil.Content = "正在清除AssetBundle...";
            ProgressBarUtil.Percent = 0;
            ProgressBarUtil.Show();

            string[] abNames = AssetDatabase.GetAllAssetBundleNames();
            for (int i = 0; i < abNames.Length; i++)
            {
                ProgressBarUtil.Percent = (float)i / (float)abNames.Length;
                string curABName = abNames[i];

                bool needClear = false;
                for (int j = 0; j < ABResUtil.PRE_LIST.Length; j++)
                {
                    if (curABName.StartsWith(ABResUtil.PRE_LIST[j]))
                    {
                        needClear = true;
                        break;
                    }
                }

                if (needClear)
                {
                    AssetDatabase.RemoveAssetBundleName(curABName, true);
                }
            }

            ProgressBarUtil.Close();

            AssetDatabase.Refresh();
        }
Exemple #2
0
        private static void ExportStats(string outputPath, AssetBundleManifest manifest)
        {
            ProgressBarUtil.Title   = "assetBundle资源统计";
            ProgressBarUtil.Content = "assetBundle资源统计...";
            ProgressBarUtil.Percent = 0;
            ProgressBarUtil.Show();

            string str       = "文件\t文件大小(KB)\t依赖文件数\t依赖文件大小(KB)\t总大小(KB)\n";
            string format    = "{0}\t{1}\t{2}\t{3}\t{4}\n";
            string pathFomat = "{0}/{1}.ab";

            long totalSize = 0;
            Dictionary <string, long> map = new Dictionary <string, long>();

            string[] assetBundles = manifest.GetAllAssetBundles();
            for (int i = 0; i < assetBundles.Length; i++)
            {
                string curAssetBundle  = assetBundles[i];
                long   assetBundleSize = 0;
                if (map.ContainsKey(curAssetBundle))
                {
                    assetBundleSize = map[curAssetBundle];
                }
                else
                {
                    assetBundleSize = File.ReadAllBytes(String.Format(pathFomat, outputPath, curAssetBundle)).Length;
                    map.Add(curAssetBundle, assetBundleSize);
                }

                string[] depends         = manifest.GetAllDependencies(curAssetBundle);
                int      dependFileCount = depends.Length;
                long     dependSize      = 0;
                for (int j = 0; j < depends.Length; j++)
                {
                    string curDepend = depends[j];
                    if (map.ContainsKey(curDepend))
                    {
                        dependSize += map[curDepend];
                    }
                    else
                    {
                        long size = File.ReadAllBytes(String.Format(pathFomat, outputPath, curDepend)).Length;
                        map.Add(curDepend, size);
                        dependSize += size;
                    }
                }

                totalSize += assetBundleSize;
                str       += String.Format(format, curAssetBundle, assetBundleSize / 1024, dependFileCount, dependSize / 1024, (assetBundleSize + dependSize) / 1024);

                ProgressBarUtil.Percent = (float)i / (float)assetBundles.Length;
            }

            str += "totalSize\t" + (totalSize / 1024) + "KB";
            File.WriteAllText(outputPath + "/stats.txt", str);

            ProgressBarUtil.Close();
        }
        private static List <ABSingleRes> BuildSingle()
        {
            ProgressBarUtil.Show("BuildSingle", "BuildSingle", 0);

            Dictionary <string, ABSingleRes> mapping = new Dictionary <string, ABSingleRes>();
            List <ABSingleRes> list = new List <ABSingleRes>();

            string[] abNames = AssetDatabase.GetAllAssetBundleNames();
            for (int i = 0; i < abNames.Length; i++)
            {
                string[] assetPaths = AssetDatabase.GetAssetPathsFromAssetBundle(abNames[i]);

                for (int j = 0; j < assetPaths.Length; j++)
                {
                    string      assetPath = assetPaths[j];
                    ABSingleRes res       = new ABSingleRes(assetPath);
                    list.Add(res);
                    mapping.Add(assetPath, res);
                }
            }

            int index = 0;

            while (index < list.Count)
            {
                ABSingleRes curABRes = list[index];
                index++;

                string[] dependencies = AssetDatabase.GetDependencies(curABRes.path, false);
                for (int i = 0; i < dependencies.Length; i++)
                {
                    string curDepend = dependencies[i];
                    if (curDepend.EndsWith(".cs") || curDepend.EndsWith(".js"))
                    {
                        continue;
                    }
                    ABSingleRes dependABRes = null;
                    if (!mapping.TryGetValue(curDepend, out dependABRes))
                    {
                        dependABRes = new ABSingleRes(curDepend);
                        mapping.Add(curDepend, dependABRes);
                        list.Add(dependABRes);
                    }
                    dependABRes.AddParent(curABRes);
                    curABRes.AddChild(dependABRes);
                }

                ProgressBarUtil.Percent = (float)index / (float)list.Count;
            }

            ProgressBarUtil.Close();
            return(list);
        }
        private static void BuildABNameGroup(List <ABSingleRes> list, ref List <ABGroupRes> groupList, ref Dictionary <string, ABGroupRes> abMapping, ref Dictionary <string, ABGroupRes> pathMapping)
        {
            ProgressBarUtil.Show("生成ABGroupRes", "BuildABNameGroup", 0);
            for (int i = 0; i < list.Count; i++)
            {
                ABSingleRes res    = list[i];
                string      abName = ABResUtil.GetABName(res.path, res.type, res.importer);
                AddGroup(abName, abName, res, ref groupList, ref abMapping, ref pathMapping);

                ProgressBarUtil.Percent = (float)i / (float)list.Count;
            }

            ProgressBarUtil.Close();
        }
        private static void BuildGroup(List <ABSingleRes> list)
        {
            List <ABGroupRes> groupList = new List <ABGroupRes>();
            Dictionary <string, ABGroupRes> abMapping   = new Dictionary <string, ABGroupRes>();
            Dictionary <string, ABGroupRes> pathMapping = new Dictionary <string, ABGroupRes>();

            Dictionary <ABResEnum, List <ABSingleRes> > typeList = new Dictionary <ABResEnum, List <ABSingleRes> >();

            //带AssetBundleName的资源生成ABGroupRes
            ProgressBarUtil.Show("生成ABGroupRes", "带AssetBundleName资源", 0);
            for (int i = 0; i < list.Count; i++)
            {
                ABSingleRes abSingleRes = list[i];
                if (!string.IsNullOrEmpty(abSingleRes.AssetBundleName))
                {
                    ABGroupRes abGroupRes = null;
                    if (!abMapping.TryGetValue(abSingleRes.AssetBundleName, out abGroupRes))
                    {
                        abGroupRes = new ABGroupRes(abSingleRes.AssetBundleName, abSingleRes.AssetBundleName, true);
                        abGroupRes.AssetBundleName = abSingleRes.AssetBundleName;
                        groupList.Add(abGroupRes);
                        abMapping.Add(abSingleRes.AssetBundleName, abGroupRes);
                    }

                    abGroupRes.Add(abSingleRes);
                    pathMapping.Add(abSingleRes.path, abGroupRes);
                }
                else
                {
                    List <ABSingleRes> reses = null;
                    if (!typeList.TryGetValue(abSingleRes.type, out reses))
                    {
                        reses = new List <ABSingleRes>();
                        typeList.Add(abSingleRes.type, reses);
                    }
                    reses.Add(abSingleRes);
                }

                ProgressBarUtil.Percent = (float)i / (float)list.Count;
            }

            //带AssetBundleName的资源生成ABGroupRes
            foreach (KeyValuePair <ABResEnum, List <ABSingleRes> > pair in typeList)
            {
                if (pair.Key == ABResEnum.atlas)
                {
                    BuildABNameGroup(pair.Value, ref groupList, ref abMapping, ref pathMapping);
                }
                else
                {
                    BuildPathGroup(pair.Value, ref groupList, ref abMapping, ref pathMapping);
                }
            }

            //构造引用关系
            ProgressBarUtil.Show("构造引用关系", "构造引用关系", 0);
            for (int i = 0; i < groupList.Count; i++)
            {
                ABGroupRes res = groupList[i];
                for (int j = 0; j < res.groups.Count; j++)
                {
                    ABRes target = res.groups[j];
                    for (int k = 0; k < target.childs.Count; k++)
                    {
                        string     path       = target.childs[k].path;
                        ABGroupRes childGroup = pathMapping[path];
                        if (childGroup != res)
                        {
                            res.AddChild(childGroup);
                        }
                    }

                    for (int k = 0; k < target.parents.Count; k++)
                    {
                        string     path        = target.parents[k].path;
                        ABGroupRes parentGroup = pathMapping[path];
                        if (parentGroup != res)
                        {
                            res.AddParent(parentGroup);
                        }
                    }
                }

                ProgressBarUtil.Percent = (float)i / (float)groupList.Count;
            }

            //清除重复节点  A->B->C  A->C   可将A->C移除
            ProgressBarUtil.Show("裁剪节点", "裁剪重复依赖", 0);
            List <ABRes> stack   = new List <ABRes>();
            List <ABRes> parents = new List <ABRes>();

            for (int i = 0; i < groupList.Count; i++)
            {
                ABGroupRes res = groupList[i];
                parents.Clear();
                parents.AddRange(res.parents);

                stack.Clear();
                for (int j = 0; j < parents.Count; j++)
                {
                    if (res.parents.Contains(parents[j]))
                    {
                        RecursionCut(res, parents[j], stack);
                    }
                }

                ProgressBarUtil.Percent = (float)i / (float)groupList.Count;
            }

            //材质不打包,处理依赖问题
            ProgressBarUtil.Show("裁剪节点", "裁剪材质节点", 0);
            List <ABSingleRes> matResList = null;

            if (typeList.TryGetValue(ABResEnum.material, out matResList))
            {
                for (int i = 0; i < matResList.Count; i++)
                {
                    ABSingleRes res         = matResList[i];
                    ABGroupRes  matGroupRes = pathMapping[res.path];
                    for (int j = 0; j < res.parents.Count; j++)
                    {
                        ABSingleRes parentRes = res.parents[j] as ABSingleRes;
                        if (parentRes.type == ABResEnum.material)
                        {
                            throw new Exception("材质的parent是材质");
                        }

                        ABGroupRes groupRes = pathMapping[parentRes.path];
                        groupRes.RemoveChild(matGroupRes);
                        for (int k = 0; k < res.childs.Count; k++)
                        {
                            ABSingleRes childRes = res.childs[k] as ABSingleRes;
                            if (childRes.type == ABResEnum.material)
                            {
                                throw new Exception("材质的child是材质");
                            }

                            ABGroupRes childGroupRes = pathMapping[childRes.path];
                            groupRes.AddChild(childGroupRes);
                            childGroupRes.AddParent(groupRes);
                        }
                    }
                    for (int k = 0; k < res.childs.Count; k++)
                    {
                        ABSingleRes childRes = res.childs[k] as ABSingleRes;

                        ABGroupRes childGroupRes = pathMapping[childRes.path];
                        childGroupRes.RemoveParent(matGroupRes);
                    }
                    matGroupRes.ClearParent();
                    matGroupRes.ClearChild();

                    ProgressBarUtil.Percent = (float)i / (float)matResList.Count;
                }
            }

            //设置AssetBundle
            ProgressBarUtil.Show("设置AssetBundle", "设置AssetBundle", 0);
            HashSet <ABRes> set = new HashSet <ABRes>();

            for (int i = 0; i < groupList.Count; i++)
            {
                if (groupList[i].parents.Count > 0)
                {
                    RecursionSetABName(groupList[i], set);
                }
                ProgressBarUtil.Percent = (float)i / (float)groupList.Count;
            }

            ProgressBarUtil.Close();
        }