Ejemplo n.º 1
0
        private static bool CreatABPacker(BuildTarget platform)
        {
            List <string> updateFileList   = new List <string>();
            List <string> updateScriptList = new List <string>();

            Dictionary <string, string>         lastVersionMd5List          = ABHelper.ReadMd5File(PlatformABExportPath + "/" + (CurVersionNum - 1) + "/" + ABHelper.Md5FileName);
            Dictionary <string, List <string> > lastVersionDependenciesList = ABHelper.ReadDependFile(PlatformABExportPath + "/" + (CurVersionNum - 1) + "/" + ABHelper.DependFileName);

            foreach (KeyValuePair <string, List <string> > pair in CurVersionDependenciesList)
            {
                string pathName = pair.Key;
                if (!lastVersionDependenciesList.ContainsKey(pathName.ToLower()))
                {
                    if (IsScriptFileRes(pathName))
                    {
                        updateScriptList.Add(pathName);
                    }
                    else
                    {
                        updateFileList.Add(pathName);
                    }
                    continue;
                }
                foreach (string depend in pair.Value)
                {
                    if (!lastVersionDependenciesList[pathName.ToLower()].Contains(depend.ToLower()))
                    {
                        if (IsScriptFileRes(pathName))
                        {
                            updateScriptList.Add(pathName);
                        }
                        else
                        {
                            updateFileList.Add(pathName);
                        }
                        break;
                    }
                    else if (CurVersionMd5List[depend].ToLower() != lastVersionMd5List[depend.ToLower()])
                    {
                        if (IsScriptFileRes(pathName))
                        {
                            updateScriptList.Add(pathName);
                        }
                        else
                        {
                            updateFileList.Add(pathName);
                        }
                        break;
                    }
                }
            }

            // 检测cs和dll脚本
            if (updateScriptList.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var pair in updateScriptList)
                {
                    sb.AppendLine(pair);
                }
                if (EditorUtility.DisplayDialog("提示", "检测到有cs等脚本的更新!!\n “停止”则中断打包,“忽略”则继续。如下:\n" + sb, "停止", "忽略"))
                {
                    throw new Exception("打包中断!!!!!!!!!!");
                }
            }

            if (updateFileList.Count > 0)
            {
                ClearAssetBundlesName();
                AssetDatabase.Refresh();

                foreach (string path in updateFileList)
                {
                    string fileName;
                    switch (CurVersionFileType[path])
                    {
                    case "0":
                        foreach (string path2 in CurVersionDependenciesList[path])
                        {
                            fileName = (ABHelper.GetFileFolderPath(path2).Replace(ResFolder, "")) + ".ab";
                            fileName = CreatFileUrlMd5(fileName);
                            AssetImporter.GetAtPath(path2).assetBundleName = fileName;
                        }
                        break;

                    case "1":
                        CopyLuaFiles(path);
                        break;

                    case "2":
                        fileName = (ABHelper.GetFileFullPathWithoutFtype(path).Replace(ResFolder, "")) + ".ab";
                        fileName = CreatFileUrlMd5(fileName);
                        AssetImporter.GetAtPath(path).assetBundleName = fileName;
                        break;

                    case "3":
                        fileName = (ABHelper.GetFileFullPathWithoutFtype(path).Replace(ResFolder, "")) + ".ab";
                        fileName = CreatFileUrlMd5(fileName);
                        AssetImporter.GetAtPath(path).assetBundleName = fileName;
                        break;

                    default: break;
                    }
                }
                try
                {
                    // 生成ab文件
                    BuildPipeline.BuildAssetBundles(CurVersionABExportPath, BuildAssetBundleOptions.ChunkBasedCompression | BuildAssetBundleOptions.DeterministicAssetBundle, platform);
                }
                catch (Exception e)
                {
                    BuildFailure();
                    Debug.LogError("打包异常!!" + e.Message);
                }
                System.Threading.Thread.Sleep(1000);

                ClearAssetBundlesName();
                AssetDatabase.Refresh();
            }
            else
            {
                BuildFailure();
            }

            bool result = updateFileList.Count > 0;

            if (result)
            {
                // 删除不用的manifest
                List <string> allFiles = ABHelper.GetAllFilesPathInDir(PlatformABExportPath);
                foreach (string path in allFiles)
                {
                    if (path.EndsWith(".manifest"))
                    {
                        File.Delete(path);
                    }
                    if (path.EndsWith(CurVersionNum.ToString()))
                    {
                        File.Delete(path);
                    }
                }
            }
            return(result);
        }