Lua 断点调试
中文 | English
在仓库根目录运行以下命令,用本机 Unity MCP 回放本页示例。
python Docs/Manual/manual/tools/tutorials.py run lua-breakpoint-debugging
先 安装 VS Code 和 EmmyLua,用 VS Code 打开与目标运行实例匹配的 Lua 源文件。
- 从 Run and Debug 选择 EmmyLua Attach Debug,并选择你自己的 Unity Editor 或本地游戏进程,例如
PanzerWar-DE。附加调试需要目标提供 Lua 符号。 - 在目标代码的行号左侧点击设置断点。在游戏中触发脚本,例如 Hello World 的行为启动。
- 命中断点后,在 Variables 查看变量,点击 Step Over 单步执行。如果断点未绑定,检查运行中的脚本与工作区文件路径和内容是否一致。
回放在项目的 xLua 中检查行号和局部变量。VS Code 附加及发行版的符号可用性需要按目标版本实测。
点击展开 MCP 操作点击收起 MCP 操作
[
{
"tool": "invoke",
"arguments": {
"path": "static:ShanghaiWindy.Core.AssetBundleManager/LuaEnv/DoString",
"args": [
"local source = \"@manual-breakpoint-example.lua\"\nlocal handler = assert(load(\"local total = 8\\nlocal sum = total + 4\\nreturn sum\", source))\nlocal observed\nlocal thread = coroutine.create(function()\n debug.sethook(function(_, line)\n if line == 2 and debug.getinfo(2, \"S\").source == source then\n local name, value = debug.getlocal(2, 1)\n if name == \"total\" then observed = value end\n end\n end, \"l\")\n local result = handler()\n debug.sethook()\n assert(result == 12 and observed == 8)\nend)\nlocal ok, err = coroutine.resume(thread)\nassert(ok, err)\nreturn \"Lua line information verified\"\n",
"manual-breakpoint",
"null"
]
},
"expect": {
"contains": "Lua line information verified"
}
}
]