[Unity热更新]VSCode使用EmmyLua调试lua代码

参考链接:

https://blog.csdn.net/qq_34035956/article/details/109255357

https://www.cnblogs.com/zhizihua/p/12857245.html

https://www.showdoc.com.cn/luaide/713892723028836

0.环境

jdk、jre(EmmyLua插件需要)

vscode、xlua

查看jdk是否安装成功:

1.设置

修改launch.json,其中的ideConnectDebugger,ide指的是vscode,Debugger指的是unity,这里设置为false即表示用unity来连接vscode

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: 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"
            ],
            "ideConnectDebugger": false
        }
    ]
}

将这个dll复制粘贴到工程的Assets同级目录下

2.代码

TestEmmyLua.lua

local str = "start"
for i = 1, 3 do
    str = "hello:" .. i
end
str = "end"

c#

using System.IO;
using UnityEngine;
using XLua;

public class TestEmmyLua : MonoBehaviour
{
    private LuaEnv luaenv;

    void Start()
    {
        luaenv = new LuaEnv();
        string currentDirectory = Directory.GetCurrentDirectory();
        if (File.Exists(currentDirectory + "/emmy_core.dll"))
        {
            string str = @"xpcall(function() local dbg = require('emmy_core') dbg.tcpConnect('localhost', 9966) end, function() print('IDE没有开启调试') end)";
            luaenv.DoString(str);
        }
        luaenv.AddLoader(CustomLoader);
        luaenv.DoString("require('TestEmmyLua')");
    }

    private byte[] CustomLoader(ref string filePath)
    {
        //print(filePath);
        filePath = Application.dataPath + "/LuaScript/" + filePath + ".lua";
        //print(filePath);
        byte[] bytes = File.ReadAllBytes(filePath);
        return bytes;
    }
}

注意一下,CustomLoader方法的参数,需要修改为该文件的路径

3.运行

在vscode中设置好断点,点击左上角的运行按钮,此时下方会提示等待连接

运行unity,这时就会命中断点了

原文地址:https://www.cnblogs.com/lyh916/p/15366611.html