[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,這時就會命中斷點了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM