scene-walker.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. let fs = require('fs');
  2. let path = require('path');
  3. module.exports = {
  4. 'exportInfo': function () {
  5. cc.log("export info");
  6. let rootPath = Editor.url('db://assets/resources/edt_prefab', 'utf8')
  7. let allPrefabs = this.getAllUIPrefab(rootPath);
  8. let allPrefabInfos = this.getAllPrefabInfo(rootPath, allPrefabs);
  9. this.writeInfoFile(allPrefabInfos);
  10. // allPrefabInfos.forEach((info) => {
  11. // // cc.log(info.name, info.path);
  12. // });
  13. },
  14. isPrefab(file) {
  15. return /\.(prefab)$/.test(file);
  16. },
  17. getAllUIPrefab(folder){
  18. const files = fs.readdirSync(folder);
  19. let result = []
  20. files.forEach((file) => {
  21. const filePath = path.join(folder, file);
  22. if (fs.statSync(filePath).isDirectory()) {
  23. result = result.concat(this.getAllUIPrefab(filePath));
  24. } else if (this.isPrefab(filePath)) {
  25. result.push(filePath)
  26. }
  27. });
  28. return result;
  29. },
  30. getAllPrefabInfo(rootPath, allPrefabs) {
  31. let result = [];
  32. allPrefabs.forEach((file) => {
  33. let prefabName = path.basename(file, path.extname(file));
  34. let prefabPath = file.replace(rootPath, "").replace(".prefab", "").substring(1)
  35. result.push({
  36. "name":prefabName,
  37. "path":prefabPath,
  38. })
  39. });
  40. result.sort(function (a, b) {
  41. return a.name.localeCompare(b.name); //降序排序
  42. });
  43. return result;
  44. },
  45. writeInfoFile(allPrefabInfos) {
  46. let result = "";
  47. let enumTemplate = "ENUM_TEMPLATE";
  48. let pathTemplate = "PATH_TEMPLATE";
  49. result = "// 当前文件为手动导出文件.请勿修改.在预制体编辑界面点击右键导出即可.\n window.JMC.UIEnum = {\n" + enumTemplate + "\n}\nwindow.JMC.UIPath = {\n" + pathTemplate + "\n}";
  50. let enumStr = "";
  51. for (let index = 0; index < allPrefabInfos.length; index++) {
  52. enumStr = enumStr + "\t\t" + allPrefabInfos[index].name + ":" + index + (index == allPrefabInfos.length - 1 ? "" : ",") + "\n";
  53. }
  54. let pathStr = "";
  55. for (let index = 0; index < allPrefabInfos.length; index++) {
  56. let endStr = index == allPrefabInfos.length - 1 ? "" : ",";
  57. pathStr = pathStr + "\t\t[window.JMC.UIEnum." + allPrefabInfos[index].name + "] : \"" + allPrefabInfos[index].path.replace("\\", "/") + "\"" + endStr + "\n";
  58. }
  59. result = result.replace(enumTemplate, enumStr);
  60. result = result.replace(pathTemplate, pathStr);
  61. let rootPath = Editor.url('db://assets/script/app/view/UIInfo.js', 'utf8')
  62. fs.writeFileSync(rootPath, result)
  63. //保存后刷新资源
  64. Editor.assetdb.refresh('db://assets/script/app/view/UIInfo.js', function (err, results) {
  65. });
  66. },
  67. };