"use strict"; let fs = require("fs"); let path = require("path"); module.exports = { /** * 獲取場景節點下的配置信息 * @param event event * @param data 這邊把panel的Node帶進來 */ "get-asset-info": function (event, request) { let self = this; let Canvas = cc.find("Canvas"); var response = null; var args = request.args; let node = cc.find(args[0]._name); if (!node) { node = this.getAllChildByUuid(Canvas, args[0]._nodeID); } // Editor.log(`node: ${node.name}`); this.setAllComponentByNode(node, args[1]._value, args[2]._value); response = "OK"; if (event.reply) { event.reply(null, response); } }, /** * 根據Node去尋找底下每個子節點有沒有符合的UUID * @param Node 需要搜尋的節點 * @param UUID 目標的UUID */ setAllComponentByNode(Node, MyID, TargetID) { let self = this; // Editor.log("Node: " + Node.name, "UUID: " + Node.uuid); let Sprite = Node.getComponent(cc.Sprite); let type = "cc.SpriteFrame"; if (!Sprite) { Sprite = Node.getComponent(cc.ParticleSystem); } let Label = Node.getComponent(cc.Label); let Animation = Node.getComponent(cc.Animation); // Editor.log("來源 Node: " + Node.name, "Sprite: " + (Sprite ? "true" : "false")); if (Sprite) { if (!Sprite.spriteFrame) { Editor.warn(`${Node.name} 沒有spriteFrame`); } else { let uuid = ""; try { uuid = Sprite.spriteFrame._uuid; } catch (error) { Editor.log(`${Node.name} 沒有uuid, Error: ${error}`); Editor.log(`${Node.name} ${JSON.stringify(Sprite.spriteFrame)}`); } let url = Editor.remote.assetdb.uuidToUrl(uuid); if (url.indexOf(MyID) === -1) { if (url.indexOf(TargetID) === -1) { Editor.log(`${Node.name} 檔案非${MyID}: ${url}`); } } else { // Editor.log("來源 uuid: " + uuid); // Editor.log("來源 url: " + url); // Editor.log(`MyID: ${MyID}, TargetID: ${TargetID}`); let reg = `/${MyID}/g`; let newurl = url.replace(eval(reg), TargetID); // Editor.log(`newurl: ${newurl}`); let newuuid = Editor.remote.assetdb.urlToUuid(newurl); if (!newuuid) { Editor.error(`${Node.name} 缺檔案: ${newurl}`); } else { // Editor.log(`newuuid: ${newuuid}`); let newfspath = Editor.remote.assetdb.uuidToFspath(newuuid); // Editor.log(`newfspath: ${newfspath}`); newfspath = newfspath.substring(0, newfspath.length - Sprite.spriteFrame.name.length - 1); // Editor.log(`newfspath: ${newfspath}`); // Editor.log(`目標 spriteFrame: ${newfspath} ${Sprite.spriteFrame.name}`); // Editor.log("uuid: " + uuid); // Editor.log("Sprite.uuid: " + Sprite.uuid); Editor.Ipc.sendToPanel('scene', 'scene:set-property', { id: Sprite.uuid, path: "spriteFrame",//要修改的属性 type: type, value: { uuid: newuuid }, isSubProp: false, }); } } } } else if (Label) { if (Label.useSystemFont) { // Editor.log(`${Node.name} useSystemFont`); } else { let Font = Label.font; if (Font.name.indexOf(MyID) !== -1) { Editor.warn(`${Node.name} Font.name: ${Font.name} 請手動替換字體`); } // Editor.log(`${Node.name} Font.name: ${Font.name} Font._fontDefDictionary: ${JSON.stringify(Font._fontDefDictionary)}`); // Editor.log(`${Node.name} Font.name: ${Font.name} Font.fntDataStr: ${Font.fntDataStr}`); // Editor.log(`${Node.name} Font: ${Object.values(Font)}`); // Editor.log(`${Node.name} Font._fontDefDictionary: ${Object.keys(Font._fontDefDictionary)}`); // Editor.log(`${Node.name} Font._fontDefDictionary: ${Object.values(Font._fontDefDictionary)}`); } } else if (Animation) { let defaultClip = Animation.defaultClip; if (defaultClip) { if (defaultClip.name.indexOf(MyID) !== -1) { Editor.warn(`${Node.name} Font.name: ${defaultClip.name} 請手動替換字體`); } } let Clips = Animation.getClips(); // Editor.log(`${Node.name} Clips.length: ${Clips.length}`); if (Clips.length > 0) { for (let i = 0; i < Clips.length; i++) { let Clip = Clips[i]; if (Clip.name.indexOf(MyID) !== -1) { Editor.warn(`${Node.name} Font.name: ${Clip.name} 請手動替換字體`); } // Editor.log(`${Node.name} Clip.name: ${Clip.name} Clip.curveData: ${Clip.curveData}`); // Editor.log(`${Node.name} Clip: ${Object.keys(Clip)}`); // Editor.log(`${Node.name} Clip: ${Object.values(Clip)}`); // Editor.log(`${Node.name} Clip.curveData.paths: ${Object.keys(Clip.curveData.paths)}`); // Editor.log(`${Node.name} Clip.curveData.paths: ${Object.values(Clip.curveData.paths)}`); } } } for (let i = 0; i < Node.childrenCount; i++) { this.setAllComponentByNode(Node.children[i], MyID, TargetID); } // Editor.log("Node: " + Node.children[1].childrenCount); }, /** * 根據Node去尋找底下每個子節點有沒有符合的UUID * @param Node 需要搜尋的節點 * @param UUID 目標的UUID */ getAllChildByUuid(Node, UUID) { for (let i = 0; i < Node.childrenCount; i++) { // Editor.log("Node: " + Node.name, "UUID: " + Node.uuid); if (Node.uuid == UUID) { // Editor.log("找到 Node: " + Node.name, "UUID: " + Node.uuid); return Node; } else { // Editor.log("Node: " + Node.children[i].name, "UUID: " + Node.children[i].uuid); if (Node.children[i].uuid == UUID) { return Node.children[i]; } } if (Node.children[i].childrenCount > 0) { // Editor.log(Node.children[i].name + " childrenCount > 0"); let getAllChildByUuid = this.getAllChildByUuid(Node.children[i], UUID); if (getAllChildByUuid) { return getAllChildByUuid; } } } // Editor.log("Node: " + Node.children[1].childrenCount); return null; }, };