lua-调试技巧


title: lua-调试技巧
categories: Lua
tags: [lua, 调试, 技巧]
date: 2022-07-14 16:31:27
comments: false
mathjax: true
toc: true

lua-调试技巧


前篇

游戏 lua 脚本调试技巧, 达到修改逻辑脚本后能实时生效执行, 提高开发效率


pc 端 editor 模式

  1. 新建一个测试代码脚本

    gDebugCustom = gDebugCustom or {
          
          }
    
    function gDebugCustom.Test01()
    	gLog("--- Test01")    
    end
    
  2. editor 增加解析这个脚本, 显示出 按钮

    image-20220715161734442

    1. 点击按钮去执行对应的方法

      • csharp

        GameMgr.CallLuaFunction("Debug_ExecCustomLua", luaFile, funcName);
        
      • lua

        -- 动态执行 自定义 lua 文件调试
        function Debug_ExecCustomLua(luaFile, funcName)
            local ok = xpcall(function()
                gAssert(Utils.IsFileExist(luaFile), "--- 找不到lua 文件, path: {0}", luaFile)
                dofile(luaFile)
                gAssert(gDebugCustom, "--- 找不到全局 table: gDebugCustom")
                gAssert(type(gDebugCustom[funcName]) == "function", "--- 找不到 gDebugCustom.{0} 方法", funcName)
        
                gDebugCustom[funcName]()
            end, __G__TRACKBACK__)
            if ok then
                gLog("<color=#00ff00ff>--- gDebugCustom.{0} success!</color>", funcName)
            else
                gLog("<color=yellow>--- gDebugCustom.{0} execute fail!</color>", funcName)
            end
        end
        
        
        
        
        

移动端

下载 lua

  1. 在 [pc 端 editor 模式](#pc 端 editor 模式) 测试脚本上增加一个全局方法 gDebugWebFn, 用来 web 执行

    
    gDebugCustom = gDebugCustom or {
          
          }
    
    function gDebugCustom.Test01()
        gLog("--- Test01")
    end
    
     -- 增加的全局方法
    gDebugWebFn = function()
        gDebugCustom.Test01()
    end
    
    
  2. 游戏调试 ui 按钮上去执行一个下载测试脚本, 并执行 gDebugWebFn

    local function testDynamicLua()
        local url = "http://192.168.1.200:59090/lua/custom_unittest.lua"
        gTool.SafeDoString(url, function(isStop, msg)
            gLog("<color=#ffff00>--- gTool.SafeDoString, isStop: {0}</color>, msg: {1}", isStop, msg)
            gAssert(type(gDebugWebFn) == "function", "--- 找不到全局调试方法 gDebugWebFn")
            gDebugWebFn()
        end)
    end
    

进阶
  1. 动态执行的脚本, 再次动态下载其他逻辑脚本, 这样会重写 lua 方法, 达到实时刷新脚本逻辑

    -- 避免出现在 unity editor中
    gDebugCustom.requireExt = function(luaPath, callback)
        local luaDirUrl = "http://192.168.1.200:59090/require"
        local newPath = string.gsub(luaPath, "%.", "/")
        local relaPath = string.formatExt("{0}.lua", newPath)
        local fullUrl = string.formatExt("{0}/{1}", luaDirUrl, relaPath)
        -- gLog("--- fullUrl: {0}", fullUrl)
    
        local cbWrap = function(isOk, res)
            gAssert(isOk, "--- url require fail, luaPath: {0}, fullUrl: {1}", luaPath, fullUrl)
            gLog("<color=#00ff00>--- url require success</color>, luaPath: {0}, fullUrl: {1}", luaPath, fullUrl)
            if callback then callback(res) end
        end
    
        gBestHttpMgr:GetData(fullUrl, function(isSucc, data)
            if isSucc and not IsNull(data) then
                local text = Utils.BytesToUTF8(data)
                local ok, res = xpcall(loadstring(text), __G__TRACKBACK__)
                if not ok then
                    cbWrap(false, "do remote string error")
                else
                    -- 移动端非 ab 模式, 写入本地
                    if gLogicTool.IsMobile() and not Const.LuaBundleMode then
                        local fullPath = gTool.PathJoin(Application.persistentDataPath, Const.kResluaDir, relaPath)
                        Utils.WriteFileUTF8(fullPath, text)
                    end
    
                    cbWrap(true, res)
                end
            else
                cbWrap(false, "Network error")
            end
        end)
    end
    
    function gDebugCustom.Dynamic()
        local function testGm()
            gDebugCustom.requireExt("logic.common.gm", function()
                local input = "show me the log"
                local isOk = gGmMgr:Do(input)
                gLog("--- gm ok: {0}", isOk)
            end)
        end
    	
        -- 测试
        testGm()
    end
    
    
    
    

Android 端拷贝 lua

明文 lua 的方式, 可以直接将项目内的 lua 脚本用 adb 命令拷到包体目录下, 然后重启游戏 or 重启 lua 虚拟机

  • adb 命令

    $ adb push E:/its/rummy_itc-v4/z_tempSave/lua /sdcard/Android/data/com.aaa.bbb/files
    

猜你喜欢

转载自blog.csdn.net/yangxuan0261/article/details/125946307