[一个简单的游戏框架]Lua相关

Mar 8, 2018


[https://github.com/HushengStudent/myGameFramework]

目前游戏开发中,普遍使用Unity+Lua的形式,为了热更新的目的,所以Lua在游戏开发中基本属于必不可少了,本文旨在简单介绍在Unity开发中,Lua的常见简单使用:

1、Lua”类”的实现

Lua中实现”类”,主要是通过元表实现的,我们简单参考cocos2d-x lua-bulding源码中,”类”的实现:

Class.lua:

function class(classname, ...)

    local cls = {__cname = classname}

    local supers = {...}  
    for _, super in ipairs(supers) do  
        local superType = type(super)  
        assert(superType == "nil" or superType == "table" or superType == "function",  
        string.format("class() - create class \"%s\" with invalid super class type \"%s\"",  
        classname, superType))  
  
        if superType == "function" then  
            assert(cls.__create == nil,  
            string.format("class() - create class \"%s\" with more than one creating function",  
            classname));  
            -- if super is function, set it to __create  
            cls.__create = super  
        elseif superType == "table" then  
            if super[".isclass"] then  
                -- super is native class  
                assert(cls.__create == nil,  
                string.format("class() - create class \"%s\" with more than one creating function or native class",  
                classname));  
                cls.__create = function() return super:create() end  
            else  
                -- super is pure lua class  
                cls.__supers = cls.__supers or {}  
                cls.__supers[#cls.__supers + 1] = super  
                if not cls.super then  
                    -- set first super pure lua class as class.super  
                    cls.super = super  
                end  
            end  
        else  
            error(string.format("class() - create class \"%s\" with invalid super type",  
            classname), 0)  
        end  
    end  
  
    cls.__index = cls  
    if not cls.__supers or #cls.__supers == 1 then  
        setmetatable(cls, {__index = cls.super})  
    else  
        setmetatable(cls, {__index = function(_, key)  
            local supers = cls.__supers  
            for i = 1, #supers do  
                local super = supers[i]  
                if super[key] then return super[key] end  
            end  
        end})  
    end  
  
    if not cls.ctor then  
        -- add default constructor  
        cls.ctor = function() end  
    end  
    cls.new = function(...)  
        local instance  
        if cls.__create then  
            instance = cls.__create(...)  
        else  
            instance = {}  
        end  
        setmetatableindex(instance, cls)  
        instance.class = cls  
        instance:ctor(...)  
        return instance  
    end  
    cls.create = function(_, ...)  
        return cls.new(...)  
    end  
  
    return cls  
end  
  
local setmetatableindex_  
setmetatableindex_ = function(t, index)  
    if type(t) == "userdata" then  
        local peer = tolua.getpeer(t)  
        if not peer then  
            peer = {}  
            tolua.setpeer(t, peer)  
        end  
        setmetatableindex_(peer, index)  
    else  
        local mt = getmetatable(t)  
        if not mt then mt = {} end  
        if not mt.__index then  
            mt.__index = index  
            setmetatable(t, mt)  
        elseif mt.__index ~= index then  
            setmetatableindex_(mt, index)  
        end  
    end  
end  
setmetatableindex = setmetatableindex_  

lua类的使用,比如我们建一个类GameManager:


GameManager = class("GameManager")  
  
function GameManager:ctor() end  
  
function GameManager:GetManagerName()  
    log("GameManager")  
end  

2、Lua常用工具方法

以下常用Lua工具方法,也是参考cocos2d-x lua-bulding源码:


---table's number  
function table.nums(t)  
    local count = 0  
    for k, v in pairs(t) do  
        count = count + 1  
    end  
    return count  
end  
  
---table's keys  
function table.keys(hashtable)  
    local keys = {}  
    for k, v in pairs(hashtable) do  
        keys[#keys + 1] = k  
    end  
    return keys  
end  
  
---table's values  
function table.values(hashtable)  
    local values = {}  
    for k, v in pairs(hashtable) do  
        values[#values + 1] = v  
    end  
    return values  
end  
  
---  
function table.merge(dest, src)  
    for k, v in pairs(src) do  
        dest[k] = v  
    end  
end  
  
---  
function table.walk(t, fn)  
    for k,v in pairs(t) do  
        fn(v, k)  
    end  
end  
  
---string split  
function string.split(input, delimiter)  
    input = tostring(input)  
    delimiter = tostring(delimiter)  
    if (delimiter=='') then return false end  
    local pos,arr = 0, {}  
    -- for each divider found  
    for st,sp in function() return string.find(input, delimiter, pos, true) end do  
        table.insert(arr, string.sub(input, pos, st - 1))  
        pos = sp + 1  
    end  
    table.insert(arr, string.sub(input, pos))  
    return arr  
end  
  
---table dump  
function table.dump(t)  
    for k, v in pairs(t) do  
        logGreen("--->>>key:"..tostring(k).." --->>>value:"..tostring(v))  
    end  
end  

3、Lua其他相关

C#中ulong传递到Lua使用string;

lua面向对象编程中,属性使用点号,方法使用冒号;

Lua网络,事件系统,定时器等将在介绍tolua时介绍;

Lua注意两种表的遍历方式;

Lua GC;