LuaFramework案例_02

来源:互联网 发布:java用sleep内存泄露 编辑:程序博客网 时间:2024/06/07 08:01

4.接收,改变Lua的值

public class Studio_AccessingLuaVariables : MonoBehaviour{    string script = @"print('obj---'..obj)var2Read=42varTable={1,2,2,6}varTable.default=1varTable.map={}varTable.map.name='map'meta={name='meta'}setmetatable(varTable,meta)function testFunc(strs)    print('test----'..strs);end";    void Start()    {        Application.logMessageReceived += ShowTips;        LuaState lua = new LuaState();        lua.Start();        lua["obj"] = 5;//使obj有值        lua.DoString(script);        Debugger.Log("lua 元素 {0}", lua["var2Read"]);        Debugger.Log("lua 表元素 {0}", lua["varTable.default"]);        LuaFunction func = lua["testFunc"] as LuaFunction;        func.Call(123);        func.Dispose();        LuaTable table = lua.GetTable("varTable");        Debugger.Log("defalu值为{0}  map.name值为{1}", table["default"], table["map.name"]);        table["map.name"] = "新值New,改变";        Debugger.Log("第二次--defalu值为{0}  map.name值为{1}--改变后", table["default"], table["map.name"]);        table.AddTable("newMap");        LuaTable table1 = (LuaTable)table["newMap"];        table1["name"] = "table1";        Debugger.Log("varTable.newMap 名字={0}", table1["name"]);        table1.Dispose();        table1 = table.GetMetaTable();        if (table1 != null)            Debugger.Log("table metatable name {0}", table1["name"]);//元表        object[] list = table.ToArray();        for (int c = 0; c < list.Length; c++)        {            Debugger.Log("vartable[{0}]==={1}", c, list[c]);        }        //LuaTable newMap = lua.GetTable("newMap") as LuaTable;        //Debugger.Log("newMap['name']==={0}", newMap["name"]);        table.Dispose();        lua.CheckTop();        lua.Dispose();    }    string tips = null;    void ShowTips(string condition, string stackTrace, LogType type)    {        tips += condition;        tips += "\r\n";    }    void OnGUI()    {        GUI.Label(new Rect(10, 10, 200, 200), tips);    }    void OnApplicationQuit()    {        Application.logMessageReceived -= ShowTips;    }}
原创粉丝点击