lua使用小技巧

记录一些lua的通用方法、小技巧,会持续更新。

1. 在回调函数中使用self

-- 回调函数传参
function handler(obj,method)
    return function(...)
        return method(obj,...)
    end
end

使用方式:self.CellButton.onClick:AddListener(handler(self,self.OnClick_Cell))

2. 输出打印table表

function table2String(tb)
    if type(tb) ~= 'table' then
        return ''
    end

    local strs = {
    
    '{\n'}
    local stack = {
    
    }

    _table2String(tb, 0, strs, stack)

    table.insert(strs, "}")
    return table.concat(strs)
end

local function _table2String(t, depth, strs, stack)
    table.insert(stack, t)
    depth = depth + 1
    local indent = ""
    for i=1, depth do
        indent = indent .. "    "
    end
    for k, v in pairs(t) do
        local key = tostring(k)
        local typeV = type(v)
        if typeV == "table" then
            if key ~= "__valuePrototype" then
                local contains = nil
                for k1, v1 in ipairs(stack) do
                    if v1 == v then
                        contains = true
                        break
                    end
                end

                if contains then
                    table.insert(strs, indent.._formatKey(key).." = {检测到循环引用!},\n")
                else
                    table.insert(strs, indent.._formatKey(key).." = {\n")
                    _table2String(v, depth, strs, stack)
                    table.insert(strs, indent.."},\n")
                end
            end
        elseif typeV == "string" then
            table.insert(strs, string.format("%s%s = \"%s\",\n", indent, _formatKey(key), tostring(v)))
        else
            table.insert(strs, string.format("%s%s = %s,\n", indent, _formatKey(key), tostring(v)))
        end
    end
    table.remove(stack)
end

local function _formatKey(key)
    local t = type(key)
    if t == "number" then
        return "["..key.."]"
    elseif t == "string" then
        local n = tonumber(key)
        if n then
            return "["..key.."]"
        end
    end
    return key
end

3. 分割字符串

function SplitStr(str, reps)
    local r = {
    
    }
    if str == nil then return nil end
    string.gsub(str, "[^"..reps.."]+", function(w) table.insert(r, w) end)
    return r
end

4. clone

function clone(object)
    local lookup_table = {
    
    }
    local function _copy(object)
        if type(object) ~= "table" then
            return object
        elseif lookup_table[object] then
            return lookup_table[object]
        end
        local newObject = {
    
    }
        lookup_table[object] = newObject
        for key, value in pairs(object) do
            newObject[_copy(key)] = _copy(value)
        end
        return setmetatable(newObject, getmetatable(object))
    end
    return _copy(object)
end

5. lua热重载,用于界面刷新

function Reload( moduleName )
    if isEditor() then
        if package.loaded[moduleName] == nil then
            print("not require")
        end
        package.loaded[moduleName] = nil
        require(moduleName)
    end
end

6. lua中使用类似于string.join函数

table.concat(Table,分隔符,起始下标,结束下标)

猜你喜欢

转载自blog.csdn.net/weixin_56130753/article/details/126492730
今日推荐