Пример #1
0
    private void Start()
    {
        luaState = new LuaState();
        luaState.Start();
        string fullPath = Path.Combine(Application.dataPath, "My/04_AccessingLuaVariables");

        luaState.AddSearchPath(fullPath);

        luaState["Objs2Spawn"] = 5;

        luaState.Require("AccessingLuaVariables");

        Debugger.Log("Read var from lua:{0}", luaState["var2Read"]);
        Debugger.Log("Read table var from lua:{0}", luaState["varTable.default"]);

        LuaFunction func = luaState["TestFunc"] as LuaFunction;

        func.Call();
        func.Dispose();

        LuaTable table = luaState.GetTable("varTable");

        Debugger.Log("Read varTable from lua, default : {0} name:{1}", table["default"], table["name"]);

        table["map.name"] = "new";
        Debugger.Log("Modify varTable name:{0}", table["map.name"]);
        LuaFunction printName = luaState.GetFunction("PrintName");

        printName.Call();

        table.AddTable("newmap");
        LuaTable table1 = (LuaTable)table["newmap"];

        table1["name"] = "table1";
        Debugger.Log("varTable.newmap name:{0}", table1["name"]);
        table1.Dispose();

        table1 = table.GetMetaTable();

        if (table1 != null)
        {
            Debugger.Log("varTable metatable name:{0}", table1["name"]);
        }

        object[] list = table.ToArray();

        print("==============================================");
        for (int i = 0; i < list.Length; i++)
        {
            Debugger.Log("varTable[{0}], is {1}", i, list[i]);
        }

        table.Dispose();
    }
Пример #2
0
    void Start()
    {
        LuaState lua = new LuaState();

        lua.Start();
        lua["Objs2Spawn"] = 5;
        lua.DoString(script);

        //通过LuaState访问
        Debugger.Log("Read var from lua: {0}", lua["var2read"]);
        Debugger.Log("Read table var from lua: {0}", lua["varTable.default"]);

        LuaFunction func = lua["TestFunc"] as LuaFunction;

        func.Call();
        func.Dispose();

        LuaTable tmpTable = lua.GetTable("varTable2");

        Debugger.Log("-----------------");
        Debugger.Log((tmpTable["a"]).GetType());

        //cache成LuaTable进行访问
        LuaTable table = lua.GetTable("varTable");

        Debugger.Log("Read varTable from lua, default: {0} name: {1}", table["default"], table["map.name"]);
        table["map.name"] = "new";
        Debugger.Log("Modify varTable name: {0}", table["map.name"]);

        table.AddTable("newmap");
        LuaTable table1 = (LuaTable)table["newmap"];

        table1["name"] = "table1";
        Debugger.Log("varTable.newmap name: {0}", table1["name"]);
        table1.Dispose();

        table1 = table.GetMetaTable();

        if (table1 != null)
        {
            Debugger.Log("varTable metatable name: {0}", table1["name"]);
        }

        object[] list = table.ToArray();

        for (int i = 0; i < list.Length; i++)
        {
            Debugger.Log("varTable[{0}], is {1}", i, list[i]);
        }

        table.Dispose();
        lua.CheckTop();
        lua.Dispose();
    }
    void Start()
    {
#if UNITY_5 || UNITY_2017 || UNITY_2018
        Application.logMessageReceived += ShowTips;
#else
        Application.RegisterLogCallback(ShowTips);
#endif
        new LuaResLoader();
        LuaState lua = new LuaState();
        lua.Start();
        lua["Objs2Spawn"] = 5;
        lua.DoString(script);

        //通过LuaState访问
        Debugger.Log("Read var from lua: {0}", lua["var2read"]);
        Debugger.Log("Read table var from lua: {0}", lua["varTable.default"]);       //LuaState 拆串式table
        Debugger.Log("Read table map name from lua: {0}", lua["varTable.map.name"]); //LuaState 拆串式table

        LuaFunction func = lua["TestFunc"] as LuaFunction;
        func.Call();
        func.Dispose();

        //cache成LuaTable进行访问
        LuaTable table = lua.GetTable("varTable");
        Debugger.Log("Read varTable from lua, default: {0} name: {1}", table["default"], table["map.name"]);
        table["map.name"] = "new";  //table 字符串只能是key
        Debugger.Log("Modify varTable name: {0}", table["map.name"]);

        table.AddTable("newmap");
        LuaTable table1 = (LuaTable)table["newmap"];
        table1["name"] = "table1";
        Debugger.Log("varTable.newmap name: {0}", table1["name"]);
        table1.Dispose();

        table1 = table.GetMetaTable();

        if (table1 != null)
        {
            Debugger.Log("varTable metatable name: {0}", table1["name"]);
        }

        object[] list = table.ToArray();

        for (int i = 0; i < list.Length; i++)
        {
            Debugger.Log("varTable[{0}], is {1}", i, list[i]);
        }

        table.Dispose();
        lua.CheckTop();
        lua.Dispose();
    }
Пример #4
0
    public void SetCommonObjectToLua(LuaTable table)
    {
        var tableTmp = table;

        if (!string.IsNullOrEmpty(group))
        {
            tableTmp = (LuaTable)table[group];
            if (tableTmp == null)
            {
                table.AddTable(group);
                tableTmp = (LuaTable)table[group];
            }
        }

        if (commonObjects != null)
        {
            foreach (var item in commonObjects)
            {
                tableTmp[item.Key] = item.Value;
            }
        }
    }
Пример #5
0
    private void Start()
    {
        LuaState luaState = new LuaState();

        luaState.Start();
        luaState["Objs2Spawn"] = 5;
        luaState.DoString(this.script, "LuaState.cs");
        Debugger.Log("Read var from lua: {0}", luaState["var2read"]);
        Debugger.Log("Read table var from lua: {0}", luaState["varTable.default"]);
        LuaFunction luaFunction = luaState["TestFunc"] as LuaFunction;

        luaFunction.Call();
        luaFunction.Dispose();
        LuaTable table = luaState.GetTable("varTable", true);

        Debugger.Log("Read varTable from lua, default: {0} name: {1}", table["default"], table["map.name"]);
        table["map.name"] = "new";
        Debugger.Log("Modify varTable name: {0}", table["map.name"]);
        table.AddTable("newmap");
        LuaTable luaTable = (LuaTable)table["newmap"];

        luaTable["name"] = "table1";
        Debugger.Log("varTable.newmap name: {0}", luaTable["name"]);
        luaTable.Dispose();
        luaTable = table.GetMetaTable();
        if (luaTable != null)
        {
            Debugger.Log("varTable metatable name: {0}", luaTable["name"]);
        }
        object[] array = table.ToArray();
        for (int i = 0; i < array.Length; i++)
        {
            Debugger.Log("varTable[{0}], is {1}", i, array[i]);
        }
        table.Dispose();
        luaState.CheckTop();
        luaState.Dispose();
    }
Пример #6
0
    public void SetCacheToLua(LuaTable table)
    {
        var tableTmp = table;

        if (!string.IsNullOrEmpty(group))
        {
            tableTmp = (LuaTable)table[group];
            if (tableTmp == null)
            {
                table.AddTable(group);
                tableTmp = (LuaTable)table[group];
            }
        }

        if (gameObjectList != null)
        {
            foreach (var item in gameObjectList)
            {
                tableTmp[item.name] = item.value;
            }
        }

        if (transformList != null)
        {
            foreach (var item in transformList)
            {
                tableTmp[item.name] = item.value;
            }
        }

        if (rectTransformList != null)
        {
            foreach (var item in rectTransformList)
            {
                tableTmp[item.name] = item.value;
            }
        }

        if (imageList != null)
        {
            foreach (var item in imageList)
            {
                tableTmp[item.name] = item.value;
            }
        }

        if (inputFieldList != null)
        {
            foreach (var item in inputFieldList)
            {
                tableTmp[item.name] = item.value;
            }
        }

        if (textList != null)
        {
            foreach (var item in textList)
            {
                tableTmp[item.name] = item.value;
            }
        }

        if (toggleList != null)
        {
            foreach (var item in toggleList)
            {
                tableTmp[item.name] = item.value;
            }
        }

        if (buttonList != null)
        {
            foreach (var item in buttonList)
            {
                tableTmp[item.name] = item.value;
            }
        }

        if (sliderList != null)
        {
            foreach (var item in sliderList)
            {
                tableTmp[item.name] = item.value;
            }
        }

        if (toggleGroupList != null)
        {
            foreach (var item in toggleGroupList)
            {
                tableTmp[item.name] = item.value;
            }
        }

        if (scrollRectList != null)
        {
            foreach (var item in scrollRectList)
            {
                tableTmp[item.name] = item.value;
            }
        }

        if (scrollbarList != null)
        {
            foreach (var item in scrollbarList)
            {
                tableTmp[item.name] = item.value;
            }
        }

        if (dropdownList != null)
        {
            foreach (var item in dropdownList)
            {
                tableTmp[item.name] = item.value;
            }
        }

        if (rawImageList != null)
        {
            foreach (var item in rawImageList)
            {
                tableTmp[item.name] = item.value;
            }
        }

        if (audioSourceList != null)
        {
            foreach (var item in audioSourceList)
            {
                tableTmp[item.name] = item.value;
            }
        }

        if (quickGridList != null)
        {
            foreach (var item in quickGridList)
            {
                tableTmp[item.name] = item.value;
            }
        }

        if (quickGridList != null)
        {
            foreach (var item in quickGridList)
            {
                tableTmp[item.name] = item.value;
            }
        }

        if (tweenButtonScaleList != null)
        {
            foreach (var item in tweenButtonScaleList)
            {
                tableTmp[item.name] = item.value;
            }
        }

        if (simpleScrollViewList != null)
        {
            foreach (var item in simpleScrollViewList)
            {
                tableTmp[item.name] = item.value;
            }
        }

        if (animationList != null)
        {
            foreach (var item in animationList)
            {
                tableTmp[item.name] = item.value;
            }
        }

        if (animatorList != null)
        {
            foreach (var item in animatorList)
            {
                tableTmp[item.name] = item.value;
            }
        }

        if (spriteAtlasList != null)
        {
            foreach (var item in spriteAtlasList)
            {
                tableTmp[item.name] = item.value;
            }
        }

        if (spriteHolderList != null)
        {
            foreach (var item in spriteHolderList)
            {
                tableTmp[item.name] = item.value;
            }
        }

        if (uiStateSwitcherList != null)
        {
            foreach (var item in uiStateSwitcherList)
            {
                tableTmp[item.name] = item.value;
            }
        }

        if (textWrapList != null)
        {
            foreach (var item in textWrapList)
            {
                tableTmp[item.name] = item.value;
            }
        }

        if (uiImageAnimList != null)
        {
            foreach (var item in uiImageAnimList)
            {
                tableTmp[item.name] = item.value;
            }
        }

        if (canvasList != null)
        {
            foreach (var item in canvasList)
            {
                tableTmp[item.name] = item.value;
            }
        }

        if (particleSystemList != null)
        {
            foreach (var item in particleSystemList)
            {
                tableTmp[item.name] = item.value;
            }
        }

        if (particleSystemRendererList != null)
        {
            foreach (var item in particleSystemRendererList)
            {
                tableTmp[item.name] = item.value;
            }
        }

        if (cacheList != null)
        {
            foreach (var item in cacheList)
            {
                tableTmp[item.name] = item.value;
            }
        }
    }
    void Start()
    {
#if UNITY_5 || UNITY_2017 || UNITY_2018
        Application.logMessageReceived += ShowTips;
#else
        Application.RegisterLogCallback(ShowTips);
#endif
        new LuaResLoader();
        LuaState lua = new LuaState();
        lua.Start();
        //--------用C#给lua变量赋值---------
        //使用luaState定义一个变量并赋值
        lua["Objs2Spawn"] = 5;//"Objs2Spawn = 5"
        //此时执行代码是在定义变量并赋值的基础之上来执行
        lua.DoString(script);

        //通过LuaState访问
        //通过C#去访问lua中变量
        Debugger.Log("Read var from lua: {0}", lua["var2read"]);
        Debugger.Log("Read table var from lua: {0}", lua["varTable.default"]);  //LuaState 拆串式table
        //用luafunction对象获取lua代码中的函数
        LuaFunction func = lua["TestFunc"] as LuaFunction;
        //调用函数
        func.Call();
        func.Dispose();

        //cache成LuaTable进行访问
        //获取lua代码中表格
        LuaTable table = lua.GetTable("varTable");
        //在访问表格字段的值时,只能访问单个字段,不能访问字段的字段
        Debugger.Log("Read varTable from lua, default: {0} name: {1}", table["default"], table["map.name"]);
        //但是可以给字段的字段赋值
        table["map.name"] = "new";  //table 字符串只能是key
        //赋值之后就可以访问了
        Debugger.Log("Modify varTable name: {0}", table["map.name"]);

        table.AddTable("newmap");//--varTable.newmap={}
        //获取到表格中的该字段,该字段是一个表格
        LuaTable table1 = (LuaTable)table["newmap"];
        //给newmap表格设置的一个字段name:table1
        table1["name"] = "table1";
        Debugger.Log("varTable.newmap name: {0}", table1["name"]);
        //table1释放了
        table1.Dispose();
        //获取表格的元表格,此时table1是元表格
        table1 = table.GetMetaTable();

        if (table1 != null)
        {
            Debugger.Log("varTable metatable name: {0}", table1["name"]);
        }
        //将表格转换为C#中的数组
        object[] list = table.ToArray();

        for (int i = 0; i < list.Length; i++)
        {
            Debugger.Log("varTable[{0}], is {1}", i, list[i]);
        }

        table.Dispose();
        lua.CheckTop();
        lua.Dispose();
    }