xlua - 使用vscode+emmylua调试

安装vscode和emmylua插件看这边:

vscode+emmylua搭建lua开发环境

 

Attack方式调试

1) 这种调试方式作者已经不推荐使用,该调试器也不再更新以及修复bug,那这边为啥还要介绍呢?

  个人使用下来感觉更快更稳定些,也没遇到创建协程coroutine调试器失效,调试过程中改代码断点错位的情况。

2) vscode的用户配置文件settings.json加上

{
    "files.associations": {
        "*.lua.txt": "lua",
        "*.txt": "lua"
    },

    //......
}

  如果lua源码文件是.lua.txt这种结尾的,不加这个会出现调试器找不到源文件的问题

3) vscode的项目调试配置文件.vscode/launch.json加上

{
    // 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_attach",
            "request": "attach",
            "name": "Attach by process id",
            "pid": 0,
            "processName": "",
            "captureLog": false
        },

        //......
    ]
}

就可以看到并使用这个调试条目

 

新的调试方式

1) vscode的项目调试配置文件.vscode/launch.json加上

{
    "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
        }
    ]
}

就会出现下面的调试条目

 

2) 下面的工具类用于获取emmylua插件安装路径

DevUtil.cs

using System;
using System.IO;
using XLua;

[LuaCallCSharp]
public static class DevUtil
{
    public static string FindJetBrainsInstallPath(string ideNamePrefix)
    {
        string userProfilePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
        var jetBrainsDir = Path.Combine(userProfilePath, "AppData/Roaming/JetBrains/");
        if (Directory.Exists(jetBrainsDir))
        {
            var dirPaths = Directory.GetDirectories(jetBrainsDir);
            if (dirPaths != null && dirPaths.Length > 0)
            {
                int startIndex = jetBrainsDir.Length;
                int maxFindIndex = startIndex + 3;
                foreach (var dirPath in dirPaths)
                {
                    int findIndex = dirPath.IndexOf(ideNamePrefix, startIndex, StringComparison.OrdinalIgnoreCase);
                    //Debug.Log($"find:{findIndex}, start:{startIndex}");
                    if (findIndex >= 0 && findIndex <= maxFindIndex)
                    {
                        return dirPath;
                    }
                }
            }
        }

        return "";
    }

    public static string FindVsCodeExtensionsPath(string extensionNamePrefix)
    {
        string userProfilePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
        var vsCodeExtensionsDir = Path.Combine(userProfilePath, ".vscode/extensions/");
        if (Directory.Exists(vsCodeExtensionsDir))
        {
            var dirPaths = Directory.GetDirectories(vsCodeExtensionsDir);
            if (null != dirPaths && dirPaths.Length > 0)
            {
                int startIndex = vsCodeExtensionsDir.Length;
                int maxFindIndex = startIndex + 3;
                foreach (var dirPath in dirPaths)
                {
                    int findIndex = dirPath.IndexOf(extensionNamePrefix, startIndex, StringComparison.OrdinalIgnoreCase);
                    //Debug.Log($"find:{findIndex}, start:{startIndex}");
                    if (findIndex >= 0 && findIndex <= maxFindIndex)
                    {
                        return dirPath;
                    }
                }
            }
        }

        return "";
    }

    public static string BuildVsCodeEmmyCoreDllPath(string dirPath)
    {
        var path = "";
#if UNITY_EDITOR_WIN
            path = Path.Combine(dirPath, "debugger/emmy/windows/x64/?.dll");
#elif UNITY_EDITOR_OSX
            path = Path.Combine(dirPath, "debugger/emmy/mac/arm64/?.dll");
#endif
        return path;
    }
    
}

 

在lua的入口文件中加上emmycore的搜索路径

比如:Main.lua

if CS.UnityEngine.Application.isEditor then
    local emmyLuaPath = CS.DevUtil.FindVsCodeExtensionsPath("tangzx.emmylua-")
    --print("emmylua path:"..emmyLuaPath)
    if emmyLuaPath and "" ~= emmyLuaPath then
        local dllPath = CS.DevUtil.BuildVsCodeEmmyCoreDllPath(emmyLuaPath)
        package._EmmyDbgDllPath = dllPath
        --print("emmylua dllPath:"..dllPath)
        package.cpath = package.cpath..";"..dllPath
    end
end

local dbg = require("emmy_core")
dbg.tcpConnect("localhost", 9966) --debugger连到ide的方式

-- ......

 

posted @ 2025-07-09 22:46  yanghui01  阅读(324)  评论(0)    收藏  举报