Example #1
0
 void OnDestroy()
 {
     f1   = null;
     f2   = null;
     f3   = null;
     f4   = null;
     f5   = null;
     farr = null;
     flua = null;
     ie   = null;
     add  = null;
     luaenv.Dispose();
 }
Example #2
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.Hotfix.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.Hotfix.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);
        }