"use strict"; let fs = require("fs"); let path = require("path"); // 默認路徑 let defaultPolygonPath="assets/"; module.exports={ load() { this.init(); }, unload() { Editor.log("卸載執行"); }, /** 初始化 */ init() { this.createDirectory(); }, /** 創建初始文件夾 */ createDirectory() { if(fs.existsSync(path.join(Editor.Project.path, defaultPolygonPath))) { Editor.log("resources exists!"); }else { // 插件加載後在項目根目錄自動創建指定文件夾 fs.mkdirSync(path.join(Editor.Project.path, defaultPolygonPath)); Editor.success("resources created!"); } }, /** 創建文件 */ createPolygonFile(filePath, node) { let polygonPath = filePath + ".txt"; if(fs.existsSync(path.join(Editor.Project.path, polygonPath))) { // 覆蓋源文件 this.coverTargetFile(polygonPath, node); }else { // 新建文件 this.createNewFile(polygonPath, node); } }, /** 覆蓋源文件 */ coverTargetFile(filePath, node) { Editor.Scene.callSceneScript("output-component", "get-scene-info", {node: node}, function (err, json) { Editor.assetdb.saveExists( "db://" + filePath, json, function ( err, meta ) { if(err) { Editor.log("覆盖文件失败!!!"); return; } Editor.log("覆盖文件成功!!!"); }); }); }, /** 創建一個新的文件 */ createNewFile(filePath, node) { Editor.Scene.callSceneScript("output-component", "get-scene-info", {node: node}, function (err, json) { Editor.assetdb.create( "db://" + filePath, json, function ( err, results ) { if(err) { Editor.log("創建文件失敗!!!"); return; } Editor.log("創建文件成功!!!"); }); }); }, /** 刪除文件 */ deletePolygonFile(filePath) { let polygonPath=filePath+".txt"; if(fs.existsSync(path.join(Editor.Project.path, polygonPath))) { Editor.assetdb.delete(["db://" + polygonPath], function (err, results) { results.forEach(function (result) { if(err) { Editor.log("刪除文件失敗!!!"); return; } Editor.log("刪除文件成功!!!"); }); }); }else { Editor.log("沒有可刪除的文件!!!"); } }, messages: { /** 打開面板 */ "open-panel"() { Editor.Panel.open("output-component"); }, /** 保存按鈕點擊 */ "save-click"(event, ...args) { const self = this; self.createPolygonFile(args[0], args[1]); }, /** 設置路徑 */ "set-path"(event,...args) { if(args[0] && args != "") { defaultPolygonPath = args[0]; this.createDirectory(); } }, /** 面板加載完成 */ "panel-load-finish"(evnet,...args) { Editor.Ipc.sendToPanel("output-component","setDefaultPath", defaultPolygonPath); }, /** 刪除按鈕點擊 */ "delete-click"(event,...args) { this.deletePolygonFile(args[0]); } }, }