zoukankan      html  css  js  c++  java
  • lua常用方法收集

    1. xlua之将c#集合转换成table

    -- 将c#的list转换成table
    local function ConvertCSListToTable(list)
        local t = {};
        for i = 0, list.Count - 1 do
            table.insert(t, list[i]);
        end
        return t;
    end
    
    -- 将c#的数组转换成table
    local function ConvertCSArrayToTable(array)
        local t = {};
        for i = 0, array.Length - 1 do
            table.insert(t, array[i]);
        end
        return t;
    end
    
    -- 将c#的字典转换成table
    local function ConvertCSDicToTable(dic)
        local t = {};
        local etor = dic:GetEnumerator();
    
        while (etor:MoveNext())
        do
            local current = etor.Current;
            local k = current.Key;
            local v = current.Value;
            table.insert(t, {k, v});
        end
        
        return t;
    end

    2. 分割字符串

    -- 分割字符串
    function this.Split(input, delimiter)
        input = tostring(input);
        delimiter = tostring(delimiter);
    
        if (delimiter == "") then 
            return false;
        end
    
        local pos,arr = 0, {};
        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

    3. 判断 unity3d 中 GameObject 是否为 null

    详见:https://blog.csdn.net/qq_34907362/article/details/80482493

    function IsNil(uobj) 
        return uobj == nil or uobj:Equals(nil) 
    end 

    4. xlua之与c#枚举器的关系

    小鱼人(692540236) 13:20:32
    xlua中我如果想跟c#中一样去启动新协程,等新协程结束再执行后面的逻辑,怎么弄啊?
    Home_to_the_mold(383894728) 13:45:33
    -- HotFix.UIRankMainTest.lua
    -- 模拟Lua侧的异步回调
    local function lua_async_test(seconds, coroutine_break)
        print('lua_async_test '..seconds..' seconds!')
        -- TODO:这里还是用Unity的协程相关API模拟异步,有需要的话再考虑在Lua侧实现一个独立的协程系统
        yield_return(CS.UnityEngine.WaitForSeconds(seconds))
        coroutine_break(true, seconds)
    end

    -- lua侧新建协程:本质上是在Lua侧建立协程,然后用异步回调驱动,
    local corotineTest = function(self, seconds)
        print('NewCoroutine: lua corotineTest', self)
        
        local s = os.time()
        print('coroutine start1 : ', s)
        -- 使用Unity的协程相关API:实际上也是CS侧协程结束时调用回调,驱动Lua侧协程继续往下跑
        -- 注意:这里会在CS.CorotineRunner新建一个协程用来等待3秒,这个协程是和self没有任何关系的
        yield_return(CS.UnityEngine.WaitForSeconds(seconds))
        print('coroutine end1 : ', os.time())
        print('This message1 appears after '..os.time() - s..' seconds in lua!')
        
        local s = os.time()
        print('coroutine start2 : ', s)
        -- 使用异步回调转同步调用模拟yield return
        -- 这里使用cs侧的函数也是可以的,规则一致:最后一个参数必须是一个回调,回调被调用时表示异步操作结束
        -- 注意:
        --    1、如果使用cs侧函数,必须将最后一个参数的回调(cs侧定义为委托)导出到[CSharpCallLua]
        --    2、用cs侧函数时,返回值也同样通过回调(cs侧定义为委托)参数传回
        local boolRetValue, secondsRetValue = util.async_to_sync(lua_async_test)(seconds)
        print('coroutine end2 : ', os.time())
        print('This message2 appears after '..os.time() - s..' seconds in lua!')
        -- 返回值测试
        print('boolRetValue:', boolRetValue, 'secondsRetValue:', secondsRetValue)
    end

    -- 协程热更示例
    xlua.hotfix(CS.UIRankMain, 'Open', function(self, param, pathData)
        print('HOTFIX:Open ', self)
        -- 省略其它代码
        -- 方式一:新建Lua协程,优点:可新增协程;缺点:使用起来麻烦
        print('----------async call----------')
        util.coroutine_call(corotineTest)(self, 4)--相当于CS的StartCorotine,启动一个协程并立即返回
        print('----------async call end----------')
        
        -- 方式二:沿用CS协程,优点:使用方便,可直接热更协程代码逻辑,缺点:不可以新增协程
        self:StartCoroutine(self:TestCorotine(3))
    end)

    -- cs侧协程热更
    xlua.hotfix(CS.UIRankMain, 'TestCorotine', function(self, seconds)
        print('HOTFIX:TestCorotine ', self, seconds)
        --注意:这里定义的匿名函数是无参的,全部参数以闭包方式传入
        return util.cs_generator(function()
            local s = os.time()
            print('coroutine start3 : ', s)
            --注意:这里直接使用coroutine.yield,跑在self这个MonoBehaviour脚本中
            coroutine.yield(CS.UnityEngine.WaitForSeconds(seconds))
            print('coroutine end3 : ', os.time())
            print('This message3 appears after '..os.time() - s..' seconds in lua!')
        end)
    end)

    转载请注明出处:http://www.cnblogs.com/jietian331/p/8118230.html

  • 相关阅读:
    Java实现构造无向图的欧拉回路( The Necklace)
    Java实现构造无向图的欧拉回路( The Necklace)
    Java实现构造无向图的欧拉回路( The Necklace)
    Java实现构造无向图的欧拉回路( The Necklace)
    Java实现构造无向图的欧拉回路( The Necklace)
    x64内联汇编调用API(需intel编译器,vc不支持x64内联汇编)
    屏蔽按CapsLock键切换到大写时,编辑框自动弹出的提示(UnregisterClass(TOOLTIPS_CLASS)后,重新设置WndProc并注意返回值)
    非常简单的利用CreateProcess注入DLL的方法
    Ring3下无驱动移除winlogon.exe进程ctrl+alt+del,win+u,win+l三个系统热键,非屏蔽热键(子类化SAS 窗口)
    设置windows2008系统缓存大小限制,解决服务器运行久了因物理内存耗尽出僵死(提升权限后,使用SetSystemFileCacheSize API函数,并将此做成了一个Service)
  • 原文地址:https://www.cnblogs.com/jietian331/p/8118230.html
Copyright © 2011-2022 走看看