tolua#的Examples-17_Inherit

    private string script =
    @"  LuaTransform =
        {            
             posCount = -1,
            _position = Vector3.zero,
        }        
        
        local get = tolua.initget(LuaTransform)
        local set = tolua.initset(LuaTransform)              
                
        --重写同名函数
        function LuaTransform:Translate(...)            
            print('child Translate')
            self.base:Translate(...)                  
        end        

        function LuaTransform:Init(u)
            self._position = u.position                    
        end

        LuaTransform.__index = LuaTransform            

        function LuaTransform.Extend(u)        
            local t = {}            
            setmetatable(t, LuaTransform)  
            t:Init(u)                    
            tolua.setpeer(u, t)                                
            return u
        end
        
        --重写同名属性获取        
        get.position = function(self)                              
            return self._position
        end

        --重写同名属性设置
        set.position = function(self, v)                
            if self._position ~= v then                                  
                self._position = v                
                self.base.position = v                                            
            end
        end
        
        --既保证支持继承函数,又支持go.transform == transform 这样的比较
        function Test(node)        
            local v = Vector3.one          
            local transform = LuaTransform.Extend(node)                                                    

            local t = os.clock()            
            for i=1, 200000 do
                transform.position = transform.position
                --local v = transform.position:Clone()                
                --v.x = v.x - 1                
                --transform.position = v
            end
            print('LuaTransform get set cost', os.clock() - t)

            transform:Translate(1,1,1)                                                                    
                        
            local child = transform:FindChild('child')
            print('child is: ', tostring(child))
            
            if child.parent == transform then            
                print('LuaTransform compare to userdata transform is ok')
            end

            transform.xyz = 123
            transform.xyz = 456
            print('extern field xyz is: '.. transform.xyz)
        end
        ";

    void Start ()
    {
        lua = new LuaState();        
        lua.Start();
        LuaBinder.Bind(lua);
        lua.DoString(script);

        float time = Time.realtimeSinceStartup;

        for (int i = 0; i < 200000; i++)
        {
            Vector3 v = transform.position;            
            transform.position = v;
        }

        time = Time.realtimeSinceStartup - time;
        Debugger.Log("c# Transform get set cost time: " + time);

        LuaFunction func = lua.GetFunction("Test");
        func.BeginPCall();
        func.Push(transform);
        func.PCall();
        func.EndPCall();

        lua.CheckTop();
        lua.Dispose();
        lua = null;
    }

这个例子演示的是Lua中如何集成unity中的Transform类——LuaTransform:从此我们可以在lua代码中重写同名函数+重写同名属性获取+重写同名属性设置。这又是tolua#新特性之一。因此升级tolua真的物超所值。

上一篇: tolua#的Examples-16_Int64
下一篇: tolua#的Examples-18_Bundle
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 0 | 查看次数: -
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭