调试

调试文件、launch 与连接

把正确的运行时文件放进工程,端口对齐;先开编辑器调试,再开游戏。

调试文件从哪里来

插件安装目录下的 luadebug 文件夹内含运行时文件。更新扩展后请重新拷贝,并确认运行时实际加载的就是这份文件(Unity 尤其要避免仍在跑旧 AssetBundle)。

设置项 luadebug.automaticDownloadingDebugFile 已废弃:插件不再自动弹窗下载调试文件。

连接顺序

  1. 编辑器启动调试会话(监听 launch 里的 port)。
  2. 游戏 / 客户端启动并执行 StartDebug(host, port)。
  3. 端口一致;真机时 host 为电脑局域网 IP。

如何添加 launch 配置

  1. 打开「运行和调试」面板。
  2. 没有 launch.json:点「创建 launch.json」→ 选 LuaDebug。
  3. 已有 launch.json:点「添加配置…」→ 选 LuaDebug: … 片段。
  4. 按引擎改字段;端口必须与 StartDebug 一致。
同时装多个 Lua 调试插件时,务必只保留 LuaDebug,否则 type=lua 会被其它扩展抢走。
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Unity-xlua",
      "type": "lua",
      "request": "attach",
      "runtimeType": "Unity",
      "localRoot": "${workspaceFolder}",
      "port": 7003,
      "fileExtNames": [".lua", ".txt", ".lua.txt", ".bytes"],
      "printType": 1
    }
  ]
}

launch 里常改的字段

字段说明
name调试下拉框显示的名字
type固定 lua(界面显示为 LuaDebug)
requestlaunch 由编辑器拉起程序;attach 等待进程连上
runtimeTypeCocos2 / Cocos3 / Unity / OpenResty / LuaTest 等
localRoot本地脚本根,用于堆栈路径映射;填错会导致断点错文件 / 乱跳
port调试端口,默认 7003,须与 StartDebug 一致
exePathlaunch 模式下可执行文件路径(模拟器 / player)
commandLine传给模拟器的参数(Cocos 常用)
mainFile部分配置下的启动脚本名
fileExtNames引擎里 Lua 可能用的后缀
printType1 控制台+系统;2 仅控制台;3 仅系统

各引擎完整示例见 框架对接

LuaTest:直接调试某个 Lua 文件

launch 中选择 LuaTest 可在不启动完整游戏的情况下调试当前 / 指定 Lua 文件。

{
  "name": "LuaTest",
  "type": "lua",
  "request": "launch",
  "runtimeType": "LuaTest",
  "mainFile": "${fileBasenameNoExtension}",
  "localRoot": "${fileDirname}",
  "curFileExtname": "${fileExtname}",
  "fileExtNames": [".lua", ".txt", ".lua.txt", ".bytes"],
  "port": 7003,
  "printType": 1
}

macOS 若出现 Permission denied,检查扩展目录与临时脚本执行权限,或在「隐私与安全性」中允许相关程序。

一个工作区多个游戏目录(乱跳文件)

多个相似目录时,调试器可能按 package.path / chunkname 猜错文件。为每个游戏写独立配置,并精确设置 localRoot

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "调试 src1",
      "type": "lua",
      "request": "attach",
      "runtimeType": "Cocos2",
      "localRoot": "${workspaceFolder}/src1",
      "port": 7003,
      "fileExtNames": [".lua", ".txt", ".lua.txt", ".bytes"],
      "printType": 1
    },
    {
      "name": "调试 src2",
      "type": "lua",
      "request": "attach",
      "runtimeType": "Cocos2",
      "localRoot": "${workspaceFolder}/src2",
      "port": 7003,
      "fileExtNames": [".lua", ".txt", ".lua.txt", ".bytes"],
      "printType": 1
    }
  ]
}

调试面板里选择对应配置再 F5。非 Cocos 项目同样只需关注 localRoot