Ejemplo n.º 1
0
        /// <summary>
        /// 检测资源
        /// </summary>
        /// <param name="resRootPath"></param>
        /// <param name="outPath"></param>
        /// <param name="target"></param>
        public static void CheackAssets(string resRootPath, string outPath, BuildTarget target)
        {
            //1.分析资源
            string rootPath = IPath.Combine(Application.dataPath, resRootPath);
            //扫描所有文件
            var allFiles = Directory.GetFiles(rootPath, "*.*", SearchOption.AllDirectories);
            var fileList = new List <string>(allFiles);

            //剔除不打包的部分
            for (int i = fileList.Count - 1; i >= 0; i--)
            {
                var fi        = allFiles[i];
                var extension = Path.GetExtension(fi.ToLower());
                //
                if (extension.ToLower() == ".meta" || extension.ToLower() == ".cs" || extension.ToLower() == ".js")
                {
                    fileList.RemoveAt(i);
                }
            }

            AnalyzeResource(fileList.ToArray(), target, IPath.Combine(outPath, "Art"));

            //2.配置写入本地

            var configPath = IPath.Combine(outPath, "Art/Config_Check.json");
            var direct     = Path.GetDirectoryName(configPath);

            if (Directory.Exists(direct) == false)
            {
                Directory.CreateDirectory(direct);
            }

            File.WriteAllText(configPath, CurManifestConfig.ToString());
        }
Ejemplo n.º 2
0
        //
        /// <summary>
        /// 生成AssetBundle
        /// </summary>
        /// <param name="resRootPath"></param>
        /// <param name="outPath"></param>
        /// <param name="target"></param>
        public static void GenAssetBundle(string resRootPath,
                                          string outPath,
                                          BuildTarget target,
                                          BuildAssetBundleOptions options = BuildAssetBundleOptions.ChunkBasedCompression,
                                          bool isClearAssets = true)
        {
            //0.cache path的路径
            cachePath  = IPath.Combine(outPath, "Art/Cache.json");
            configPath = IPath.Combine(outPath, "Art/Config.json");
            //1.环境准备
            string rootPath = IPath.Combine(Application.dataPath, resRootPath);
            //扫描所有文件
            var allFiles = Directory.GetFiles(rootPath, "*.*", SearchOption.AllDirectories);
            var fileList = new List <string>(allFiles);

            //剔除不打包的部分
            for (int i = fileList.Count - 1; i >= 0; i--)
            {
                var fi        = allFiles[i];
                var extension = Path.GetExtension(fi.ToLower());
                //
                if (extension.ToLower() == ".meta" || extension.ToLower() == ".cs" || extension.ToLower() == ".js")
                {
                    fileList.RemoveAt(i);
                }
            }


            var artOutpath = IPath.Combine(outPath, "Art");

            //2.分析ab包
            AnalyzeResource(fileList.ToArray(), target, artOutpath);
            //3.生成AssetBundle
            BuildAssetBundle(target, outPath, options);

            //assetBundle 转 hash
            foreach (var item in allfileHashMap)
            {
                var sub    = item.Key.Replace(BApplication.ProjectRoot + "/", "").ToLower();
                var source = IPath.Combine(artOutpath, sub);
                var copyto = IPath.Combine(artOutpath, item.Value);
                if (File.Exists(source) && !File.Exists(copyto))
                {
                    File.Copy(source, copyto);
                }
            }
            Directory.Delete(IPath.Combine(artOutpath, "assets"), true);

            //保存配置
            FileHelper.WriteAllText(configPath, CurManifestConfig.ToString());
            //保存Cache.json
            FileHelper.WriteAllText(cachePath, JsonMapper.ToJson(allfileHashMap, true));

            //4.清除AB Name
            if (isClearAssets)
            {
                RemoveAllAbName();
            }


            //删除无用文件
            var delFiles = Directory.GetFiles(artOutpath, "*", SearchOption.AllDirectories);

            foreach (var df in delFiles)
            {
                var ext = Path.GetExtension(df);
                if (ext == ".meta" || ext == ".manifest")
                {
                    File.Delete(df);
                }

                //避免删除配置
                if (df.EndsWith("Cache.json") || df.EndsWith("Config.json"))
                {
                    continue;
                }

                //
                var fn   = Path.GetFileName(df);
                var item = CurManifestConfig.GetManifestItemByHash(fn);
                if (item == null)
                {
                    File.Delete(df);
                }
            }
        }