- 确保Unity MCP Server正在运行(菜单: Window > MCP Unity Server)
- 确保Node.js MCP Server已构建(在.MCPServer/McpServer目录运行
npm run build)
首先需要在Unity中实例化这个prefab才能测试:
# 加载prefab
curl -X POST http://localhost:8090/McpUnity/ \
-H "Content-Type: application/json" \
-d '{"command":"load_asset","parameters":{"path":"Assets/Game/ResourcesAB/UI2/G/Act99/Prefab/Act99_AFK_MyTips.prefab"}}'prefab的根节点GameObject名称是 Act99_AFK_MyTips:
curl -X POST http://localhost:8090/McpUnity/ \
-H "Content-Type: application/json" \
-d '{"command":"get_lua_params","parameters":{"name":"Act99_AFK_MyTips"}}'预期结果:
{
"success": true,
"gameObjectName": "Act99_AFK_MyTips",
"luaScriptPath": "Module.Activity99.View.Act99_AFK_MyTips",
"objectParams": [
{"key": "CloseBtn", "type": "Transform", "value": "CloseBtn"},
{"key": "PowerText", "type": "Transform", "value": "PowerText"},
{"key": "HpBar", "type": "Transform", "value": "HpBar"},
...
],
"valueParams": []
}curl -X POST http://localhost:8090/McpUnity/ \
-H "Content-Type: application/json" \
-d '{"command":"find_lua_behaviours","parameters":{}}'curl -X POST http://localhost:8090/McpUnity/ \
-H "Content-Type: application/json" \
-d '{"command":"find_lua_behaviours","parameters":{"luaScript":"Act99_AFK"}}'例如,修改CloseBtn的引用(假设场景中有一个新的按钮):
curl -X POST http://localhost:8090/McpUnity/ \
-H "Content-Type: application/json" \
-d '{"command":"set_lua_param","parameters":{"name":"Act99_AFK_MyTips","key":"CloseBtn","type":"object","value":"NewCloseButton"}}'curl -X POST http://localhost:8090/McpUnity/ \
-H "Content-Type: application/json" \
-d '{"command":"set_lua_param","parameters":{"name":"Act99_AFK_MyTips","key":"TestParam","type":"value","value":"{\"obj\":123}"}}'-
Prefab模式限制: LuaModule目前只能操作场景中的GameObject,不能直接操作Prefab资源。需要先实例化prefab到场景中。
-
对象引用设置: 设置object类型参数时,目标对象需要在场景中可找到(通过GameObject.Find)。
- 添加
get_prefab_lua_params命令,直接读取prefab资源中的LuaBehaviour配置 - 添加
set_prefab_lua_param命令,修改prefab资源中的Lua参数 - 支持通过fileID引用对象(用于prefab内部引用)