ToLua Example 17 TestInherit

13 篇文章 0 订阅

using UnityEngine;

using System.Collections;

using LuaInterface;

 

public class TestInherit : MonoBehaviour

{

    private string script =

    @"  LuaTransform =

        {                         

        }                                                  

 

        function LuaTransform.Extend(u)        

            local t = {}                       

            local _position = u.position     

            tolua.setpeer(u, t)    

 

            t.__index = t

            local get = tolua.initget(t)    --初始化访问器

            local set = tolua.initset(t)  

 

            local _base = u.base      --base是在setpeer产生的,具体怎么用? u.base应该代表的就是为了u所创建的那个表    

 

            --重写同名属性获取       

            get.position = function(self)       --获取LuaTransform._position                       

                return _position               

            end           

 

            --重写同名属性设置

            set.position = function(self, v)                --同时设置   LuaTransform._position和用户的 _base.position                                           

                if _position ~= v then        

                    _position = v                   

                    _base.position = v                                                                                  

                end

            end

 

            --重写同名函数

            function t:Translate(...)           

                print('child Translate')        --先干点事儿

                _base:Translate(...)          --还是执行用户的 _base:Translate(...)        

            end   

                          

            return u

        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

            end

            print('LuaTransform get set cost', os.clock() - t)

 

            transform:Translate(1,1,1)                                                                    

                       

            local child = transform:Find('child')

            print('child is: ', tostring(child))

           

            if child.parent == transform then        --相当于go.transform == transform   

                print('LuaTransform compare to userdata transform is ok')

            end

 

            transform.xyz = 123

            transform.xyz = 456

            print('extern field xyz is: '.. transform.xyz)

        end

        ";

 

    LuaState lua = null;

 

    void Start ()

    {

#if UNITY_5 || UNITY_2017 || UNITY_2018

        Application.logMessageReceived += ShowTips;

#else

        Application.RegisterLogCallback(ShowTips);

#endif  

        new LuaResLoader();

        lua = new LuaState();       

        lua.Start();

        LuaBinder.Bind(lua);

        lua.DoString(script, "TestInherit.cs");

 

        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);       

        lua.Call("Test", transform, true);       

        lua.Dispose();

        lua = null;       

    }

 

    string tips;

 

    void ShowTips(string msg, string stackTrace, LogType type)

    {

        tips += msg;

        tips += "\r\n";

    }

 

    void OnDestroy()

    {

#if UNITY_5 || UNITY_2017 || UNITY_2018

        Application.logMessageReceived -= ShowTips;

#else

        Application.RegisterLogCallback(null);

#endif

    }

 

    void OnGUI()

    {

        GUI.Label(new Rect(Screen.width / 2 - 300, Screen.height / 2 - 300, 600, 600), tips);

    }

}

 

 

Setpeer详解:

https://www.cnblogs.com/xsxjin/p/6854584.html

代码在runtimetolua.c

TODO:这个还得看看

 

点号,冒号,self

https://blog.csdn.net/wangbin_jxust/article/details/12170233

 

执行结果

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值