ToLua的Example示例学习笔记05_LuaCorouti

2020-05-11  本文已影响0人  凌枫望星月

展示了如何使用lua协同。

1」代码

操作代码如下:

        function fib(n)
            local a, b = 0, 1
            while n > 0 do
                a, b = b, a + b
                n = n - 1
            end

            return a
        end

        function CoFunc()
            print('Coroutine started')    
            for i = 1, 10, 1 do
                print(fib(i))                    
                coroutine.wait(0.1)                     
            end 
            print("current frameCount: "..Time.frameCount)
            coroutine.step()
            print("yield frameCount: "..Time.frameCount)

            local www = UnityEngine.WWW("http://www.baidu.com")
            coroutine.www(www)
            local s = tolua.tolstring(www.bytes)
            print(s:sub(1, 128))
            print('Coroutine ended')
        end

        function TestCortinue() 
            coroutine.start(CoFunc)
        end

        local coDelay = nil

        function Delay()
            local c = 1

            while true do
                coroutine.wait(1) 
                print("Count: "..c)
                c = c + 1
            end
        end

        function StartDelay()
            coDelay = coroutine.start(Delay)
        end

        function StopDelay()
            coroutine.stop(coDelay)
        end

c#代码如下:

        new LuaResLoader();
        lua  = new LuaState();
        lua.Start();
        LuaBinder.Bind(lua);
        DelegateFactory.Init();         
        looper = gameObject.AddComponent<LuaLooper>();
        looper.luaState = lua;

        lua.DoString(luaFile.text, "TestLuaCoroutine.lua");
        LuaFunction f = lua.GetFunction("TestCortinue");
        f.Call();
        f.Dispose();
        f = null;  

2」需要了解的部分


3」值得注意的方法

上一篇下一篇

猜你喜欢

热点阅读