título: categorias de habilidades de depuração de lua
:
tags de lua: [lua, depuração, habilidades]
data: 14/07/2022 16:31:27
comentários: false
mathjax: true
toc: true
dicas de depuração lua
Prequela
Habilidades de depuração de script de jogo lua, para obter execução em tempo real após modificar scripts lógicos e melhorar a eficiência do desenvolvimento
Modo de editor lateral do PC
-
Criar um novo script de código de teste
gDebugCustom = gDebugCustom or { } function gDebugCustom.Test01() gLog("--- Test01") end
-
O editor adiciona análise a este script e exibe o botão
-
Clique no botão para executar o método correspondente
-
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
-
-
terminal móvel
baixar lua
-
Adicione um método global ao script de teste [modo do editor pc](#modo do editor pc)
gDebugWebFn
para execução na webgDebugCustom = gDebugCustom or { } function gDebugCustom.Test01() gLog("--- Test01") end -- 增加的全局方法 gDebugWebFn = function() gDebugCustom.Test01() end
-
Vá para o botão de interface do usuário de depuração do jogo para executar um script de teste de download e execute
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
Avançado
-
Execute scripts dinamicamente e baixe dinamicamente outros scripts lógicos novamente, o que irá reescrever o método lua para alcançar a lógica do script de atualização em tempo real
-- 避免出现在 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
Lua de cópia do lado do Android
Na forma de lua de texto simples, você pode copiar diretamente o script lua no projeto para o diretório do corpo do pacote com o comando adb e, em seguida, reiniciar o jogo ou reiniciar a máquina virtual lua
-
comando adb
$ adb push E:/its/rummy_itc-v4/z_tempSave/lua /sdcard/Android/data/com.aaa.bbb/files