unity+lua游戏开发遇到卡死bug解决

本文介绍了一种使用lua中的debug.sethook功能来监听并记录函数调用情况的方法,通过指定事件触发点,将调用位置信息写入日志,便于追踪代码执行过程。
摘要由CSDN通过智能技术生成

核心思路是用debug.sethook监听lua脚本中函数调用

local file_dir = Application.dataPath .. "/logs/"
local file_path = file_dir .. os.date("%Y-%m-%d.txt", os.time())
local function writelog(str_log)
	trycatch(function()
			local sw
			if not System.IO.Directory.Exists(file_dir) then
				System.IO.Directory.CreateDirectory(file_dir)
			end
			if not System.IO.File.Exists(file_path) then
				sw = System.IO.File.CreateText(file_path)
			else
				sw = System.IO.File.AppendText(file_path)
			end
			sw:WriteLine(CJson_ToString(str_log))
			sw:Flush()
			sw:Close()
		end,
		function()
			warn("lua vision low")
		end)
end

debug.sethook(
	function(event, line)
		writelog(debug.getinfo(2).short_src .. ":" .. line)
	end
	, "l")

把上面代码随便贴如到lua脚本.运行工程,可查看log日志.会发现循环调用的位置.

好用啊!!! 记录下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值