private void Awake()
        {
            // XLuaEnvironment.AddLoader(load);
            XLuaEnv.onDispose += LuaDispose;
            scriptEnv          = XLuaEnv.GetTable(luaScript, "LuaTestScript");
            //scriptEnv = XLuaEnv.NewTable();

            //// 为每个脚本设置一个独立的环境,可一定程度上防止脚本间全局变量、函数冲突
            //LuaTable meta = XLuaEnv.NewTable();
            //meta.Set("__index", XLuaEnv.GlobalTable);
            //scriptEnv.SetMetaTable(meta);
            //meta.Dispose();

            scriptEnv.Set("this", this);
            foreach (var injection in injections)
            {
                scriptEnv.Set(injection.Key, injection.value);
            }

            //XLuaEnv.DoString(luaScript.text, "LuaTestScript", scriptEnv);

            luaAwake     = scriptEnv.Get <Action>("Awake");
            luaOnEnable  = scriptEnv.Get <Action>("OnEnable");
            luaStart     = scriptEnv.Get <Action>("Start");
            luaUpdate    = scriptEnv.Get <Action>("Update");
            luaOnDisable = scriptEnv.Get <Action>("OnDisable");
            luaOnDestroy = scriptEnv.Get <Action>("OnDestroy");

            var test1 = scriptEnv.Get <DoSth2>("test");
            var test2 = scriptEnv.Get <LuaFunction>("test2");

            test1.Invoke(test2, 1, 2);
            //object[] objs = test2.Call( 1, 2);


            //if (luaAwake != null)
            //{
            //    luaAwake();
            //    luaAwake = null;
            //}
        }
예제 #2
0
        void Test()
        {
            XLuaEnv.DoString(script);
            CalcNew calc_new = XLuaEnv.gtable.GetInPath <CalcNew>("Calc.New");
            ICalc   calc     = calc_new(10, "hi", "john"); //constructor

            Debug.Log("sum(*10) =" + calc.Add(1, 2));
            calc.Mult = 100;
            Debug.Log("sum(*100)=" + calc.Add(1, 2));

            Debug.Log("list[0]=" + calc[0]);
            Debug.Log("list[1]=" + calc[1]);

            calc.PropertyChanged += Notify;
            calc[1] = "dddd";
            Debug.Log("list[1]=" + calc[1]);

            calc.PropertyChanged -= Notify;

            calc[1] = "eeee";
            Debug.Log("list[1]=" + calc[1]);
        }
        void OnGUI()
        {
            if (GUI.Button(new Rect(10, 10, 300, 80), "Hotfix"))
            {
                XLuaEnv.DoString(@"
                xlua.hotfix(CS.IFramework.Lua.HotfixTest, 'Update', function(self)
                    self.tick = self.tick + 1
                    if (self.tick % 50) == 0 then
                        print('<<<<<<<<Update in lua, tick = ' .. self.tick)
                    end
                end)
            ");
            }

            string   chHint = @"在运行该示例之前,请细致阅读xLua文档,并执行以下步骤:

1.宏定义:添加 HOTFIX_ENABLE 到 'Edit > Project Settings > Player > Other Settings > Scripting Define Symbols'。
(注意:各平台需要分别设置)

2.生成代码:执行 'XLua > Generate Code' 菜单,等待Unity编译完成。

3.注入:执行 'XLua > Hotfix Inject In Editor' 菜单。注入成功会打印 'hotfix inject finish!' 或者 'had injected!' 。";
            string   enHint = @"Read documents carefully before you run this example, then follow the steps below:

1. Define: Add 'HOTFIX_ENABLE' to 'Edit > Project Settings > Player > Other Settings > Scripting Define Symbols'.
(Note: Each platform needs to set this respectively)

2.Generate Code: Execute menu 'XLua > Generate Code', wait for Unity's compilation.


3.Inject: Execute menu 'XLua > Hotfix Inject In Editor'.There should be 'hotfix inject finish!' or 'had injected!' print in the Console if the Injection is successful.";
            GUIStyle style  = GUI.skin.textArea;

            style.normal.textColor = Color.red;
            style.fontSize         = 16;
            GUI.TextArea(new Rect(10, 100, 500, 290), chHint, style);
            GUI.TextArea(new Rect(10, 400, 500, 290), enHint, style);
        }
 // Use this for initialization
 void Start()
 {
     XLuaEnv.DoString("require 'coruntine_test'");
 }
예제 #5
0
        // Use this for initialization
        void Start()
        {
            XLuaEnv.onDispose += () =>
            {
                f1   = null;
                f2   = null;
                f3   = null;
                f4   = null;
                f5   = null;
                farr = null;
                flua = null;
                ie   = null;
                add  = null;
            };
            XLuaEnv.DoString(@"
                function id(...)
                    return ...
                end

                function add(a, b) return a + b end
            
                function array_exchange(arr)
                    arr[0], arr[1] = arr[1], arr[0]
                end

                local v3 = CS.UnityEngine.Vector3(7, 8, 9)
                local vt = CS.IFramework.Lua.MyStruct(5, 6)

                function lua_access_csharp()
                    monoBehaviour:FloatParamMethod(123) --primitive
                    monoBehaviour:Vector3ParamMethod(v3) --vector3
                    local rnd = math.random(1, 100)
                    local r = monoBehaviour:Vector3ParamMethod({x = 1, y = 2, z = rnd}) --vector3
                    assert(r.x == 1 and r.y == 2 and r.z == rnd)
                    monoBehaviour:StructParamMethod(vt) --custom struct
                    r = monoBehaviour:StructParamMethod({a = 1, b = rnd, e = {c = rnd}})
                    assert(r.b == rnd and r.e.c == rnd)
                    monoBehaviour:EnumParamMethod(CS.IFramework.Lua.MyEnum.E2) --enum
                    monoBehaviour:DecimalParamMethod(monoBehaviour.a5[0])
                    monoBehaviour.a1[0], monoBehaviour.a1[1] = monoBehaviour.a1[1], monoBehaviour.a1[0] -- field
                end

                exchanger = {
                    exchange = function(self, arr)
                        array_exchange(arr)
                    end
                }

                A = { B = { C = 789}}
                GDATA = 1234;
            ");

            XLuaEnv.gtable.Set("monoBehaviour", this);

            XLuaEnv.gtable.Get("id", out f1);
            XLuaEnv.gtable.Get("id", out f2);
            XLuaEnv.gtable.Get("id", out f3);
            XLuaEnv.gtable.Get("id", out f4);
            XLuaEnv.gtable.Get("id", out f5);

            XLuaEnv.gtable.Get("array_exchange", out farr);
            XLuaEnv.gtable.Get("lua_access_csharp", out flua);
            XLuaEnv.gtable.Get("exchanger", out ie);
            XLuaEnv.gtable.Get("add", out add);

            XLuaEnv.gtable.Set("g_int", 123);
            XLuaEnv.gtable.Set(123, 456);
            int i;

            XLuaEnv.gtable.Get("g_int", out i);
            Debug.Log("g_int:" + i);
            XLuaEnv.gtable.Get(123, out i);
            Debug.Log("123:" + i);
        }
        // Use this for initialization
        void Start()
        {
            HotfixCalc   calc         = new HotfixCalc();
            NoHotfixCalc ordinaryCalc = new NoHotfixCalc();

            int CALL_TIME = 100 * 1000 * 1000;
            var start     = System.DateTime.Now;

            for (int i = 0; i < CALL_TIME; i++)
            {
                calc.Add(2, 1);
            }
            var d1 = (System.DateTime.Now - start).TotalMilliseconds;

            Debug.Log("Hotfix using:" + d1);

            start = System.DateTime.Now;
            for (int i = 0; i < CALL_TIME; i++)
            {
                ordinaryCalc.Add(2, 1);
            }
            var d2 = (System.DateTime.Now - start).TotalMilliseconds;

            Debug.Log("No Hotfix using:" + d2);

            Debug.Log("drop:" + ((d1 - d2) / d1));

            Debug.Log("Before Fix: 2 + 1 = " + calc.Add(2, 1));
            Debug.Log("Before Fix: Vector3(2, 3, 4) + Vector3(1, 2, 3) = " + calc.Add(new Vector3(2, 3, 4), new Vector3(1, 2, 3)));
            XLuaEnv.DoString(@"
            xlua.hotfix(CS.IFramework.Lua.HotfixCalc, 'Add', function(self, a, b)
                return a + b
            end)
        ");
            Debug.Log("After Fix: 2 + 1 = " + calc.Add(2, 1));
            Debug.Log("After Fix: Vector3(2, 3, 4) + Vector3(1, 2, 3) = " + calc.Add(new Vector3(2, 3, 4), new Vector3(1, 2, 3)));

            double num;
            string str = "hehe";
            int    ret = calc.TestOut(100, out num, ref str);

            Debug.Log("ret = " + ret + ", num = " + num + ", str = " + str);

            XLuaEnv.DoString(@"
            xlua.hotfix(CS.IFramework.Lua.HotfixCalc, 'TestOut', function(self, a, c, go)
                    print('TestOut', self, a, c, go)
                    if go then error('test error') end
                    return a + 10, a + 20, 'right version'
                end)
        ");
            str = "hehe";
            ret = calc.TestOut(100, out num, ref str);
            Debug.Log("ret = " + ret + ", num = " + num + ", str = " + str);

            XLuaEnv.DoString(@"
            xlua.hotfix(CS.IFramework.Lua.HotfixCalc, {
                 Test1 = function(self)
                    print('Test1', self)
                    return 1
                 end;
                 Test2 = function(self, a, b)
                     print('Test1', self, a, b)
                     return a + 10, 1024, b
                 end;
                 Test3 = function(a)
                    print(a)
                    return 10
                 end;
                 Test4 = function(a)
                    print(a)
                 end;
                 Test5 = function(self, a, ...)
                    print('Test4', self, a, ...)
                 end
            })
        ");

            int    r1 = calc.Test1 <int>();
            double r2 = calc.Test1 <double>();

            Debug.Log("r1:" + r1 + ",r2:" + r2);

            string ss = "heihei";
            int    r3 = calc.Test2(r1, out r2, ref ss);

            Debug.Log("r1:" + r1 + ",r2:" + r2 + ",r3:" + r3 + ",ss:" + ss);

            r3 = HotfixCalc.Test3("test3");
            r3 = HotfixCalc.Test3(2);
            r3 = HotfixCalc.Test3(this);
            Debug.Log("r3:" + r3);
            HotfixCalc.Test4(this);
            HotfixCalc.Test4(2);
            calc.Test5(10, "a", "b", "c");
            calc.Test5(10, 1, 3, 5);

            Debug.Log("----------------------before------------------------");
            TestStateful();
            System.GC.Collect();
            System.GC.WaitForPendingFinalizers();
            XLuaEnv.DoString(@"
            local util = require 'xlua.util'
            xlua.hotfix(CS.IFramework.Lua.StatefullTest, {
                ['.ctor'] = function(csobj)
                    util.state(csobj, {evt = {}, start = 0, prop = 0})
                end;
                set_AProp = function(self, v)
                    print('set_AProp', v)
                    self.prop = v
                end;
                get_AProp = function(self)
                    return self.prop
                end;
                get_Item = function(self, k)
                    print('get_Item', k)
                    return 1024
                end;
                set_Item = function(self, k, v)
                    print('set_Item', k, v)
                end;
                add_AEvent = function(self, cb)
                    print('add_AEvent', cb)
                    table.insert(self.evt, cb)
                end;
                remove_AEvent = function(self, cb)
                   print('remove_AEvent', cb)
                   for i, v in ipairs(self.evt) do
                       if v == cb then
                           table.remove(self.evt, i)
                           break
                       end
                   end
                end;
                Start = function(self)
                    print('Start')
                    for _, cb in ipairs(self.evt) do
                        cb(self.start, 2)
                    end
                    self.start = self.start + 1
                end;
                StaticFunc = function(a, b, c)
                   print(a, b, c)
                end;
                GenericTest = function(self, a)
                   print(self, a)
                end;
                Finalize = function(self)
                   print('Finalize', self)
                end
           })
        ");
            Debug.Log("----------------------after------------------------");
            TestStateful();
            XLuaEnv.FullGc();
            System.GC.Collect();
            System.GC.WaitForPendingFinalizers();

            var genericObj = new GenericClass <double>(1.1);

            genericObj.Func1();
            Debug.Log(genericObj.Func2());
            XLuaEnv.DoString(@"
            xlua.hotfix(CS.IFramework.Lua.GenericClass(CS.System.Double), {
                ['.ctor'] = function(obj, a)
                    print('GenericClass<double>', obj, a)
                end;
                Func1 = function(obj)
                    print('GenericClass<double>.Func1', obj)
                end;
                Func2 = function(obj)
                    print('GenericClass<double>.Func2', obj)
                    return 1314
                end
            })
        ");
            genericObj = new GenericClass <double>(1.1);
            genericObj.Func1();
            Debug.Log(genericObj.Func2());

            InnerTypeTest itt = new InnerTypeTest();

            itt.Foo();
            XLuaEnv.DoString(@"
            xlua.hotfix(CS.IFramework.Lua.InnerTypeTest, 'Bar', function(obj)
                    print('lua Bar', obj)
                    return {x = 10, y = 20}
                end)
        ");
            itt.Foo();

            StructTest st = new StructTest(gameObject);

            Debug.Log("go=" + st.GetGo(123, "john"));
            XLuaEnv.DoString(@"
            xlua.hotfix(CS.IFramework.Lua.StructTest, 'GetGo', function(self, a, b)
                    print('GetGo', self, a, b)
                    return nil
                end)
        ");
            Debug.Log("go=" + st.GetGo(123, "john"));

            GenericStruct <int> gs = new GenericStruct <int>(1);

            Debug.Log("gs.GetA()=" + gs.GetA(123));
            XLuaEnv.DoString(@"
            xlua.hotfix(CS.IFramework.Lua.GenericStruct(CS.System.Int32), 'GetA', function(self, a)
                    print('GetA',self, a)
                    return 789
                end)
        ");
            Debug.Log("gs.GetA()=" + gs.GetA(123));

            try
            {
                calc.TestOut(100, out num, ref str, gameObject);
            }
            catch (LuaException e)
            {
                Debug.Log("throw in lua an catch in c# ok, e.Message:" + e.Message);
            }


            BaseTestBase <InnerTypeTest> bt = new BaseTest();

            bt.Foo(1);
            Debug.Log(bt);

            XLuaEnv.DoString(@"
            xlua.hotfix(CS.IFramework.Lua.BaseTest, 'Foo', function(self, p)
                    print('BaseTest', p)
                end)
            xlua.hotfix(CS.IFramework.Lua.BaseTest, 'ToString', function(self)
                    return '>>>' .. base(self):ToString()
                end)
        ");
            bt.Foo(2);
            Debug.Log(bt);
        }
 void Start()
 {
     XLuaEnv.DoString("require 'async_test'");
 }
예제 #8
0
 private void Awake()
 {
     XLuaEnv.DoString("CS.UnityEngine.Debug.Log('haha By Unity')");
     XLuaEnv.DoString("CS.IFramework.Log.L('haha By IFramework')");
 }