let fs = require('fs'); let path = require('path'); module.exports = { 'exportInfo': function () { cc.log("export info"); let rootPath = Editor.url('db://assets/resources/edt_prefab', 'utf8') let allPrefabs = this.getAllUIPrefab(rootPath); let allPrefabInfos = this.getAllPrefabInfo(rootPath, allPrefabs); this.writeInfoFile(allPrefabInfos); // allPrefabInfos.forEach((info) => { // // cc.log(info.name, info.path); // }); }, isPrefab(file) { return /\.(prefab)$/.test(file); }, getAllUIPrefab(folder){ const files = fs.readdirSync(folder); let result = [] files.forEach((file) => { const filePath = path.join(folder, file); if (fs.statSync(filePath).isDirectory()) { result = result.concat(this.getAllUIPrefab(filePath)); } else if (this.isPrefab(filePath)) { result.push(filePath) } }); return result; }, getAllPrefabInfo(rootPath, allPrefabs) { let result = []; allPrefabs.forEach((file) => { let prefabName = path.basename(file, path.extname(file)); let prefabPath = file.replace(rootPath, "").replace(".prefab", "").substring(1) result.push({ "name":prefabName, "path":prefabPath, }) }); result.sort(function (a, b) { return a.name.localeCompare(b.name); //降序排序 }); return result; }, writeInfoFile(allPrefabInfos) { let result = ""; let enumTemplate = "ENUM_TEMPLATE"; let pathTemplate = "PATH_TEMPLATE"; result = "// 当前文件为手动导出文件.请勿修改.在预制体编辑界面点击右键导出即可.\n window.JMC.UIEnum = {\n" + enumTemplate + "\n}\nwindow.JMC.UIPath = {\n" + pathTemplate + "\n}"; let enumStr = ""; for (let index = 0; index < allPrefabInfos.length; index++) { enumStr = enumStr + "\t\t" + allPrefabInfos[index].name + ":" + index + (index == allPrefabInfos.length - 1 ? "" : ",") + "\n"; } let pathStr = ""; for (let index = 0; index < allPrefabInfos.length; index++) { let endStr = index == allPrefabInfos.length - 1 ? "" : ","; pathStr = pathStr + "\t\t[window.JMC.UIEnum." + allPrefabInfos[index].name + "] : \"" + allPrefabInfos[index].path.replace("\\", "/") + "\"" + endStr + "\n"; } result = result.replace(enumTemplate, enumStr); result = result.replace(pathTemplate, pathStr); let rootPath = Editor.url('db://assets/script/app/view/UIInfo.js', 'utf8') fs.writeFileSync(rootPath, result) //保存后刷新资源 Editor.assetdb.refresh('db://assets/script/app/view/UIInfo.js', function (err, results) { }); }, };