配置步骤
1 环境
1.1 vscode 安装emmy_lua
1.2 安装对应的lua版本
1.3 安装java8并配置环境
1.4 emmy_lua的github上下载emmy_lua的64位版本,解压放到工程目录client\Tools\EmmyLua\
结构如下
client
——Assets
——Tools\EmmyLua
——EasyHook.dll
——emmy_core.dll
——emmy_hook.dll
2 程序启动时执行下面的代码
local function split(line, sep, maxsplit)
if string.len(line) == 0 then
return {}
end
sep = sep or " "
maxsplit = maxsplit or 0
local retval = {}
local pos = 1
local step = 0
while true do
local from, to = string.find(line, sep, pos, true)
step = step + 1
if (maxsplit ~= 0 and step > maxsplit) or not from then
local item = string.sub(line, pos)
table.insert(retval, item)
break
else
local item = string.sub(line, pos, from - 1)
table.insert(retval, item)
pos = to + 1
end
end
return retval
end
--连接EmmyLua
local function connectEmmyLua()
local func = function()
local assets = CS.UnityEngine.Application.dataPath
local assetDict = split(assets, "/")
local path = ''
for i = 1, #assetDict-1 do
path = path .. assetDict[i] .. '/'
end
package.cpath = package.cpath .. ';' .. path .. 'Tools/EmmyLua/emmy_core.dll'
local dbg = require('emmy_core')
dbg.tcpConnect('localhost', 9966)
end
local handle = function(error)
print('IDE没有开启调试', error)
end
xpcall(func, handle)
end
3 配置debug使用的launch.json
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "emmylua_new",
"request": "launch",
"name": "EmmyLua New Debug",
"host": "localhost",
"port": 9966,
"ext": [
".lua",
".lua.txt",
".lua.bytes",
".txt"
],
"ideConnectDebugger": false
},
]
}
4 如果是xlua,修改自定义的LuaLoader
这里有个坑,比如lua中require("game.xxx.XX")
在自定义loader中,public byte[] CustomLoader(ref string luaPath)
luaPath则为game.xxx.XX
如果不对这个luaPath做什么,emmylua的断点无法生效,也不会报错,就什么也不发生,简单查阅emmylua插件的源码,没找到关联到lua源码的代码
后来使用breakHere方法,强制进到一个lua断点,但是报错"无法加载源",看不到源码。
local dbg = require('emmy_core')
dbg.tcpConnect('localhost', 8866)
dbg.breakHere()
我猜是不是xlua那边传给emmylua的路径不对呢?
如果想让emmylua断点后可以找到文件需要重新赋值这个luaPath为真实的文件路径
luaPath = GetRealPath(luaPath)
解决问题
5 vscode启动debug
6 设置断点后运行游戏
参考资料
VSCode使用EmmyLua调试Lua代码_HKW_hankangwen的博客-优快云博客_emmylua vscode