static void SyncList(List <string> keyList) { Init(); List <string> addList = new List <string>(); foreach (string key in keyList) { PathConfig config = _configs[key]; Sync(config); if (!config.toResmap) { continue; } //收集需要动态到resmap的文件 foreach (OptCmdProp cmdProp in cmdPropList) { if (cmdProp.isDir) { continue; //文件夹直接跳过 } switch (cmdProp.opt) { case Opt.New: addList.Add(cmdProp.dstPath); break; } } } AssetDatabase.Refresh(); if (addList.Count > 0) { ResmapUtility.ToResmapExternal(addList); } }
static void BuildData() { ResmapUtility.canAuto2Resmap = false; //不自动导入到resmap中 // 创建文件夹 Directory.CreateDirectory(ORIGINABPATH); DeleteFiles("data_"); //删除旧文件 // 重命名assetbundle FileInfo oldFile = new FileInfo(ORIGINABPATH + "/assetbundle"); if (oldFile.Exists) { oldFile.MoveTo(ORIGINABPATH + "/Tmp"); } ABNameUtility.ClearAllABNames(); EditorUtility.DisplayProgressBar("打包前准备", "正在生成resmap文件...", 0.1f); //重新生成Resmap文件 ResmapUtility.AddAllAsset(); EditorUtility.DisplayProgressBar("打包前准备", "正在处理Data文件...", 0.3f); ABNameUtility.SetDataABNames(); EditorUtility.DisplayProgressBar("打包", "正在打包Data文件...", 0.8f); FileEncrypt.Encrypt();//加密Txt AssetDatabase.Refresh(); BuildPipeline.BuildAssetBundles(ORIGINABPATH, BuildAssetBundleOptions.ChunkBasedCompression, EditorUserBuildSettings.activeBuildTarget); AssetDatabase.Refresh(); ABNameUtility.ClearDataABNames(); EditorUtility.DisplayProgressBar("打包", "Data文件加密中...", 0.9f); EncryptAB("data_"); FileEncrypt.Decrypt();//还原Txt EditorUtility.ClearProgressBar(); // 删除新assetbundle UnityEditor.FileUtil.DeleteFileOrDirectory(ORIGINABPATH + "/assetbundle"); // 还原文件 if (oldFile.Exists) { oldFile.MoveTo(ORIGINABPATH + "/assetbundle"); } AssetDatabase.Refresh(); }
private static void CreateTxt() { List <string> addList = new List <string>(); //清理旧的txt文件:保留文件名称首字母小写的数据文件 foreach (string dir in ResmapUtility.LocalTypeList) { string dirPath = string.Format(TxtDir, dir); if (Directory.Exists(dirPath)) { ClearOldTxt(dirPath); } else { Directory.CreateDirectory(dirPath); } } localIdDic.Clear(); localValueDic.Clear(); //生成新的txt文件 for (int k = 0; k < tableList.Count; k++) { TableInfo tableInfo = tableList[k]; EditorUtility.DisplayProgressBar("正在生成Txt文件", tableInfo.fileName + ".txt", (k + 1) * 1f / tableList.Count); StringBuilder sb = new StringBuilder(); DataRow exportRow = tableInfo.dataTable.Rows[0]; DataRow typeRow = tableInfo.dataTable.Rows[1]; //记录字段名称 DataRow fieldRow = tableInfo.dataTable.Rows[2]; for (int i = 0; i < tableInfo.colCount; i++) { if (!exportRow[i].ToString().Trim().ToLower().Contains("c")) { continue; //注释用字段,不导入txt文件中 } string value = fieldRow[i].ToString().Trim(); if (string.IsNullOrEmpty(value)) { Debug.LogError(tableInfo.fileName + "表格中字段名不能为空!"); return; } sb.Append(value); if (i != tableInfo.colCount - 1) { sb.Append("\t"); } } sb.Append("\n"); //主键:表格中第一列含有c的为主键 int mainKey = 0; for (int j = 0; j < tableInfo.colCount; j++) { string value = exportRow[j].ToString().ToLower(); string type = typeRow[j].ToString().ToLower(); if (value.Contains("c") && !type.Contains("[]") && !type.Contains("bool")) { mainKey = j; break; } } //多语言文本结构 string[] dirSplits = tableInfo.dir.Split(new[] { '/' }, 1); string localModuleName = dirSplits[0]; if (!localIdDic.ContainsKey(localModuleName)) { localIdDic.Add(localModuleName, new Dictionary <string, string>()); localValueDic.Add(localModuleName, new Dictionary <string, string>()); } //记录内容 for (int i = StartRow; i < tableInfo.rowCount; i++) { DataRow rowValue = tableInfo.dataTable.Rows[i]; string mainKeyStr = rowValue[mainKey].ToString().Trim(); //记录每行的值 for (int j = 0; j < tableInfo.colCount; j++) { string format = exportRow[j].ToString().Trim().ToLower(); if (!format.Contains("c")) { continue; //注释用字段,不导入txt文件中 } string type = typeRow[j].ToString().Trim().ToLower(); string value = rowValue[j].ToString().Trim(); //内容空的时候,设置默认值 if (string.IsNullOrEmpty(value)) { if (format.Contains("t") && !format.Contains("e")) { Debug.LogErrorFormat("表 {0} 中第 {1} 行的 {2} 值不能为空,请检查!", tableInfo.fileName, i + 1, fieldRow[j].ToString().Trim()); } else { if (type.Contains("[]")) { value = ""; } else { if (type.Contains("string")) { value = ""; } else { value = "0"; } } } } else { //检查数据是否合法 if (type.Contains("int") || type.Contains("float") || type.Contains("double")) { if (Regex.IsMatch(value, @"[^ 0123456789.|-]")) { Debug.LogErrorFormat("表 {0} 中第 {1} 行的 {2} 值不合法,请检查!", tableInfo.fileName, i + 1, fieldRow[j].ToString().Trim()); } } else if (type.Contains("bool")) { if (type.Contains("[]")) { value = TransBoolArray(value); } else { value = TransBool(value); } } else if (type.Contains("string") && format.Contains("t"))//含有多语言文本 { if (type.Contains("[]")) { string[] splits = value.Split('|'); value = ""; for (int m = 0; m < splits.Length; m++) { string str = splits[m]; string localId; if (!localValueDic[localModuleName].TryGetValue(str, out localId)) {//判断字符串是否已经存在 localId = tableInfo.fileName + "_" + fieldRow[j] + "_" + mainKeyStr + "_" + (m + 1); localIdDic[localModuleName].Add(localId, str); localValueDic[localModuleName].Add(str, localId); } if (m == splits.Length - 1) { value += localId; } else { value += localId + "|"; } } } else { string localId; if (!localValueDic[localModuleName].TryGetValue(value, out localId)) {//判断字符串是否已经存在 localId = tableInfo.fileName + "_" + fieldRow[j] + "_" + mainKeyStr; localIdDic[localModuleName].Add(localId, value); localValueDic[localModuleName].Add(value, localId); } value = localId; } } } //保存内容 sb.Append(value); if (j != tableInfo.colCount - 1) { sb.Append("\t"); } } //换行 if (i != tableInfo.rowCount - 1) { sb.Append("\n"); } } //根据多语言,分割txt文件 string subDirPath = dirSplits.Length > 1 ? dirSplits[1] : ""; if (string.IsNullOrEmpty(subDirPath)) { string filePath = string.Format(TxtDir, dirSplits[0]) + "/" + tableInfo.fileName + ".txt"; File.WriteAllText(filePath, sb.ToString()); addList.Add(filePath); } else { string dirPath = string.Format(TxtDir, dirSplits[0]) + "/" + subDirPath; Directory.CreateDirectory(dirPath); string filePath = dirPath + "/" + tableInfo.fileName + ".txt"; File.WriteAllText(filePath, sb.ToString()); addList.Add(filePath); } } CreateLocalFile(addList);//创建多语言文件 AssetDatabase.Refresh(); if (addList.Count > 0) { ResmapUtility.ToResmapExternal(addList); } }
static void BuildAll() { ResmapUtility.canAuto2Resmap = false; //不自动导入到resmap中 // 创建文件夹 Directory.CreateDirectory(ORIGINABPATH); if (Directory.Exists(ABPATH)) { FileUtil.DeleteFileOrDirectory(ABPATH); } //记录所有的ab包,打包后,对无用的资源进行删除 EditorUtility.DisplayProgressBar("打包前准备", "收集已有AB资源...", 0.05f); Dictionary <string, ABInfo> _infoDic = new Dictionary <string, ABInfo>(); string[] files = Directory.GetFiles(ORIGINABPATH); for (int i = 0; i < files.Length; i++) { string path = files[i]; if (path.EndsWith(".meta") || path.EndsWith(".manifest")) { continue; } string fileName = Path.GetFileNameWithoutExtension(path); if (fileName.Equals("assetbundle")) { continue; } _infoDic.Add(fileName, new ABInfo() { abName = fileName, path = path }); } ABNameUtility.ClearAllABNames(); EditorUtility.DisplayProgressBar("打包前准备", "开始处理多语言资源...", 0.1f); MultiLngResDetach.StartDeal(); EditorUtility.DisplayProgressBar("打包前准备", "正在重新生成Resmap文件...", 0.2f); ResmapUtility.AddAllAsset(); EditorUtility.DisplayProgressBar("打包前准备", "Lua文件加密中...", 0.25f); LuaEncode.StartEncode(EditorUserBuildSettings.activeBuildTarget); EditorUtility.DisplayProgressBar("打包前准备", "配置文件加密中...", 0.3f); FileEncrypt.Encrypt(); AssetDatabase.Refresh(); EditorUtility.DisplayProgressBar("开始打包", "正在自动设置AB名...", 0.4f); ABNameUtility.SetAllABNames(); // 打包 EditorUtility.DisplayProgressBar("打包", "AB打包中...", 0.6f); AssetBundleManifest manifest = BuildPipeline.BuildAssetBundles(ORIGINABPATH, BuildAssetBundleOptions.ChunkBasedCompression, EditorUserBuildSettings.activeBuildTarget); string[] bundles = manifest.GetAllAssetBundles(); for (int i = 0; i < bundles.Length; i++) { if (_infoDic.ContainsKey(bundles[i])) { _infoDic.Remove(bundles[i]); } } //删除老旧的ab包 foreach (KeyValuePair <string, ABInfo> pair in _infoDic) { ABInfo info = pair.Value; UnityEditor.FileUtil.DeleteFileOrDirectory(info.path); UnityEditor.FileUtil.DeleteFileOrDirectory(info.path + ".manifest"); } EditorUtility.DisplayProgressBar("收尾", "配置文件还原中...", 0.75f); FileEncrypt.Decrypt();//还原txt EditorUtility.DisplayProgressBar("收尾", "Lua文件还原中...", 0.8f); LuaEncode.EndEncode(); EditorUtility.DisplayProgressBar("收尾", "多语言资源还原中...", 0.85f); MultiLngResDetach.Revert(); EditorUtility.DisplayProgressBar("收尾", "清除AB名...", 0.9f); ABNameUtility.ClearAllABNames(); EditorUtility.ClearProgressBar(); AssetDatabase.Refresh(); ResmapUtility.canAutoToResmap = true; }