feat(mcp-bridge): 添加 MCP 桥接插件实现 Cocos Creator 与外部工具集成 - 新增 main.js 实现 MCP 服务器,提供 HTTP 接口供外部工具调用 - 实现 5 个核心工具接口:获取选中节点、修改节点名称、保存场景、 获取场景层级结构、更新节点变换属性 - 添加 panel 面板用于测试 MCP 功能,包含节点 ID 获取和名称修改功能 - 实现场景脚本 scene-script.js 处理节点属性修改和层级数据导出 - 配置 package.json 定义插件入口文件和菜单项 - 支持跨域请求便于调试,返回符合 MCP 规范的工具定义格式 ```
92 lines
2.5 KiB
JavaScript
92 lines
2.5 KiB
JavaScript
"use strict";
|
||
|
||
module.exports = {
|
||
"set-property": function (event, args) {
|
||
const { id, path, value } = args;
|
||
|
||
// 1. 获取节点
|
||
let node = cc.engine.getInstanceById(id);
|
||
|
||
if (node) {
|
||
// 2. 修改属性
|
||
if (path === "name") {
|
||
node.name = value;
|
||
} else {
|
||
node[path] = value;
|
||
}
|
||
|
||
// 3. 【解决报错的关键】告诉编辑器场景变脏了(需要保存)
|
||
// 在场景进程中,我们发送 IPC 给主进程
|
||
Editor.Ipc.sendToMain("scene:dirty");
|
||
|
||
// 4. 【额外补丁】通知层级管理器(Hierarchy)同步更新节点名称
|
||
// 否则你修改了名字,层级管理器可能还是显示旧名字
|
||
Editor.Ipc.sendToAll("scene:node-changed", {
|
||
uuid: id,
|
||
});
|
||
|
||
if (event.reply) {
|
||
event.reply(null, `Node ${id} updated to ${value}`);
|
||
}
|
||
} else {
|
||
if (event.reply) {
|
||
event.reply(new Error("Scene Script: Node not found " + id));
|
||
}
|
||
}
|
||
},
|
||
"get-hierarchy": function (event) {
|
||
const scene = cc.director.getScene();
|
||
|
||
function dumpNodes(node) {
|
||
// 【优化】跳过编辑器内部的私有节点,减少数据量
|
||
if (node.name.startsWith("Editor Scene") || node.name === "gizmoRoot") {
|
||
return null;
|
||
}
|
||
|
||
let nodeData = {
|
||
name: node.name,
|
||
uuid: node.uuid,
|
||
active: node.active,
|
||
position: { x: Math.round(node.x), y: Math.round(node.y) },
|
||
scale: { x: node.scaleX, y: node.scaleY },
|
||
size: { width: node.width, height: node.height },
|
||
// 记录组件类型,让 AI 知道这是个什么节点
|
||
components: node._components.map((c) => c.__typename),
|
||
children: [],
|
||
};
|
||
|
||
for (let i = 0; i < node.childrenCount; i++) {
|
||
let childData = dumpNodes(node.children[i]);
|
||
if (childData) nodeData.children.push(childData);
|
||
}
|
||
return nodeData;
|
||
}
|
||
|
||
const hierarchy = dumpNodes(scene);
|
||
if (event.reply) event.reply(null, hierarchy);
|
||
},
|
||
|
||
"update-node-transform": function (event, args) {
|
||
const { id, x, y, scaleX, scaleY, color } = args;
|
||
let node = cc.engine.getInstanceById(id);
|
||
|
||
if (node) {
|
||
if (x !== undefined) node.x = x;
|
||
if (y !== undefined) node.y = y;
|
||
if (scaleX !== undefined) node.scaleX = scaleX;
|
||
if (scaleY !== undefined) node.scaleY = scaleY;
|
||
if (color) {
|
||
// color 格式如 "#FF0000"
|
||
node.color = new cc.Color().fromHEX(color);
|
||
}
|
||
|
||
Editor.Ipc.sendToMain("scene:dirty");
|
||
Editor.Ipc.sendToAll("scene:node-changed", { uuid: id });
|
||
|
||
if (event.reply) event.reply(null, "Transform updated");
|
||
} else {
|
||
if (event.reply) event.reply(new Error("Node not found"));
|
||
}
|
||
},
|
||
};
|