神临的uLua学习(二)

上面写到到了函数的使用,这里继续
10.LuaCoroutines(这里是协程的记录)

private string script = @"                                   
            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')
                local i = 0
                for i = 0, 10, 1 do
                    print(fib(i))                    
                    coroutine.wait(1)
                end
                print('Coroutine ended')
            end

            function myFunc()--协程开始函数
                coroutine.start(CoFunc)
            end
        ";

    private LuaScriptMgr lua = null;

void Awake () 
    {
        lua  = new LuaScriptMgr();
        lua.Start();
        lua.DoString(script);        
        LuaFunction f = lua.GetLuaFunction("myFunc");
        f.Call();
        f.Release();//略
    }

    // Update is called once per frame
void Update () 
    {        
        lua.Update();
    }

void LateUpdate()
    {
        lua.LateUpate();
    }

void FixedUpdate()
    {
        lua.FixedUpdate();
    }//更新触发

11.LuaWWW(关于uLua的WWW使用方式)

LuaScriptMgr lua;

    string script = @"      
        local WWW = UnityEngine.WWW

        function testFunc()
            local www = WWW('http://bbs.ulua.org/readme.txt');
            coroutine.www(www);
            print(www.text);    
        end

        coroutine.start(testFunc)
    ";//这里认真看.无需解释

    // Use this for initialization
    void Start () {
        lua = new LuaScriptMgr();
        lua.Start();
        lua.DoString(script);
    }

    void Update() {
        lua.Update();
    }

    void LateUpdate() {
        lua.LateUpate();
    }

    void FixedUpdate() {
        lua.FixedUpdate();
    }

12.LuaArray(一个关于Array的使用)

 private string script = @"                                   
            function TestArray(objs)                
                local len = objs.Length

                for i = 0, len - 1 do
                    print(objs[i])
                end
                return 1, '123', true
            end
        ";

    string[] objs = { "aaa", "bbb", "ccc" };

 void Start()
    {
        LuaScriptMgr lua = new LuaScriptMgr();
        lua.Start();
        lua.DoString(script);
        LuaFunction f = lua.GetLuaFunction("TestArray");
        //转换一下类型,避免可变参数拆成多个参数传递
        object[] rts = f.Call((object)objs);
        f.Release();

        for (int i = 0; i < objs.Length; i++)
        {
            Debug.Log(rts[i].ToString());
        }
    }//不多做解释,并不困难

13.LuaProtoBuffer(关于发送信息的),目前不是太会用这个,这里就先放在这里了,等以后再补充.

 private string script = @"      
        function decoder()  
            local msg = person_pb.Person()
            msg:ParseFromString(TestProtol.data)
            print('id:'..msg.id..' name:'..msg.name..' email:'..msg.email)
        end

        function encoder()                           
            local msg = person_pb.Person()
            msg.id = 100
            msg.name = 'foo'
            msg.email = 'bar'
            local pb_data = msg:SerializeToString()
            TestProtol.data = pb_data--调用TestProtol类
        end
        ";

    //实际应用如Socket.Send(LuaStringBuffer lsb)函数(功能发送lsb.buffer) , 在lua中调用Socket.Send(pb_data)
    //读取协议 Socket.PeekMsgPacket() {return MsgPacket}; lua 中,取协议字节流 MsgPacket.data
void Start()
    {
        LuaScriptMgr mgr = new LuaScriptMgr();
        mgr.Start();
        TestProtolWrap.Register(mgr.GetL());//调用TestProtolWrap类
        mgr.DoFile("3rd/pblua/person_pb.lua");
        mgr.DoString(script);

        LuaFunction func = mgr.GetLuaFunction("encoder");
        func.Call();
        func.Release();

        func = mgr.GetLuaFunction("decoder");
        func.Call();
        func.Release();
    }
public static class TestProtol
{
    public static LuaStringBuffer data; 
}
public class TestProtolWrap
{
    public static void Register(IntPtr L)
    {
        LuaMethod[] regs = new LuaMethod[]
        {
            new LuaMethod("New", _CreateTestProtol),
            new LuaMethod("GetClassType", GetClassType),
        };

        LuaField[] fields = new LuaField[]
        {
            new LuaField("data", get_data, set_data),
        };

        LuaScriptMgr.RegisterLib(L, "TestProtol", typeof(TestProtol), regs, fields, null);
    }

    [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
    static int _CreateTestProtol(IntPtr L)
    {
        LuaDLL.luaL_error(L, "TestProtol class does not have a constructor function");
        return 0;
    }

    static Type classType = typeof(TestProtol);

    [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
    static int GetClassType(IntPtr L)
    {
        LuaScriptMgr.Push(L, classType);
        return 1;
    }

    [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
    static int get_data(IntPtr L)
    {
        LuaScriptMgr.Push(L, TestProtol.data);
        return 1;
    }

    [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
    static int set_data(IntPtr L)
    {
        TestProtol.data = LuaScriptMgr.GetStringBuffer(L, 3);
        return 0;
    }
}

神临就先整理到这里了更加具体的关于委托,重写的方法,明天再添加,谢谢.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值