About LUA+Unity development _toLua [2]

This article talks about the toLua framework LuaFramework on github. The two versions correspond to UGUI and NGUI respectively. This framework is based on a new generation of toLua#. The performance of toLua# is stronger than that of uLua and earlier toLua, and it comes with an assetbundle mechanism, which is convenient for users to develop.

Framework address: https://github.com/jarjin/LuaFramework_UGUI

In the process of using it, you should look at the official documents: http://doc.ulua.org/default.asp

 

Generally speaking, it is a bit winding, and it is easy to get lost due to the unclear structure when it is first used. But the design idea is still good. In the use of the framework, Main.lua is used as a global entry instead of a single game entry. My approach is to write the Controller and View, and the Model layer with native callbacks needs to be implemented in C# and then directly bridged. Of course, the Model that does this cannot be extended! Therefore, it can be realized by bridging each entity of C# with MonoBehavior class to LUA in a "fixed form".

For example, in the following situation, what can you do?

1.LUA instance two cubes with rigid body and collision

2. The C# layer implements a collision detection, gets the callback OnCollisionEnter, and passes the collision object (GameObject) to LUA.

3. After getting this GameObject in LUA, how to determine which Cube in 1 was touched?

 

 

my method:

1. A more prudent method, create a unique name for the Cube, and then judge by the name after taking it out

2. The most direct method! ! Comparing GameObject directly, after continuous testing, it is completely OK.

Why is method 2 feasible? I think it's because the actual address of the GameObject is still in C#, so the result obtained through LUA's == (compare object address and value) is still what we want.

 

In addition, the framework provides us with the Event mechanism, which is a broadcast mechanism, and all the callback functions of the global AddEventListener event will be called. So what if you only want to monitor a certain lua class? You can try to add the following code to an object from a metatable instance:

function object:addEventListener(eventName, listener, target)
    print(eventName)
    eventName = string.upper(eventName)
    if object.listeners_[eventName] == nil then
        object.listeners_[eventName] = {}
    end

    local ttarget = type(target)
    if ttarget == "table" or ttarget == "userdata" then
        listener = handler(target, listener)
        -- tag = ""
    end

    object.listenerHandleIndex_ = object.listenerHandleIndex_ + 1
    local handle = string.format("HANDLE_%d", object.listenerHandleIndex_)
    object.listeners_[eventName][handle] = listener
    return handle
end

function object:dispatchEvent(event)
    event.name = string.upper(event.name)
    local eventName = event.name
    if object.listeners_[eventName] == nil then return end
    event.target = object
    for handle, listener in pairs(object.listeners_[eventName]) do
        local ret = listener(event, a)
        if ret == false then
            break
        elseif ret == "__REMOVE__" then
            object.listeners_[eventName][handle] = nil
        end
    end
end

In this way, we can specify a caller object to listen for an event, which is regarded as the use of special needs.

 

Finally, combined with the overall toLua and Lua Framework framework, it is the best choice for rapid development of U3D projects with LUA as the main language! Let's talk about XLUA later.

Guess you like

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