XLua framework construction - Unity's Update cycle

There is an Update function in the life cycle of unity, which is the cycle of the game, similar to FixedUpdate and LateUpdate, etc. After inheriting monobehaviour in c#, unity will call the corresponding function, according to our previous design "XLua Framework Construction - LuaBehaviour Design" , we can also get the corresponding update function and call it, but what about the non-lifecycle class of mgr class in lua?

Both c# and lua will have this problem. For non-monbehaviour classes, there is no corresponding life cycle function call. Think about the practice in c#, generally define a c# class, inherit monobehaviour, and then mark it as DontDestroyOnLoad, in Add some methods similar to addUpdateListener to the class for registration, put these registrations in the list, and traverse the corresponding registration function in the corresponding function.

Following this method in lua, we add the class we just wrote to the export list, create a new class in lua, register the corresponding update callback function with the c# class just now in the class constructor, and use the same principle to add AddUpdateListener , AddFixedUpdateListener, AddLateUpdateListener, and the corresponding Remove function, like the previous message distribution, you need to pass the self instance together when adding, and then traverse the list in the callback just registered and call.

The code is relatively simple, for reference only

GameUpdate = newClass("GameUpdate", nil)

local m_updateList = {};
local m_fixedUpdateList = {};
local m_lateUpdateList = {};

function GameUpdate.AddUpdate(ins, func)
    local add = {};
    add.ins = ins;
    add.func = func;
    table.insert(m_updateList, add);
end
function GameUpdate.RemoveUpdate( ins, func)
    for i, v in pairs(m_updateList) do
        if v.ins == ins and v.func == func then
            table.remove(m_updateList, i);
        end
    end
end

function GameUpdate.AddFixedUpdate(ins, func)
    local add = {};
    add.ins = ins;
    add.func = func;
    table.insert(m_fixedUpdateList, add);
end
function GameUpdate.RemoveFixedUpdate( ins, func)
    for i, v in pairs(m_fixedUpdateList) do
        if v.ins == ins and v.func == func then
            table.remove(m_fixedUpdateList, i);
        end
    end
end

function GameUpdate.AddLateUpdate(ins, func)
    local add = {};
    add.ins = ins;
    add.func = func;
    table.insert(m_lateUpdateList, add);
end
function GameUpdate.RemoveLateUpdate( ins, func)
    for i, v in pairs(m_lateUpdateList) do
        if v.ins == ins and v.func == func then
            table.remove(m_lateUpdateList, i);
        end
    end
end

function GameUpdate.Update()
    local deltaTime = CS.UnityEngine.Time.deltaTime;
    for i, v in pairs(m_updateList) do
        if v.ins ~= nil then
            v.func(v.ins,deltaTime);
        else
            v.func(deltaTime);
        end
    end
end

function GameUpdate.FixedUpdate()
    local fixedDeltaTime = CS.UnityEngine.Time.fixedDeltaTime;
    for i, v in pairs(m_fixedUpdateList) do
        if v.ins ~= nil then
            v.func(v.ins,fixedDeltaTime);
        else
            v.func(fixedDeltaTime);
        end
    end
end

function GameUpdate:LateUpdate()
    local deltaTime = CS.UnityEngine.Time.deltaTime;
    for i, v in pairs(m_lateUpdateList) do
        if v.ins ~= nil then
            v.func(v.ins,deltaTime);
        else
            v.func(deltaTime);
        end
    end
end

GameUpdate.Instance = GameUpdate.New();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324587566&siteId=291194637