LuaFramework案例_02

4.接收,改变Lua的值

public class Studio_AccessingLuaVariables : MonoBehaviour
{
    string script = @"
print('obj---'..obj)
var2Read=42
varTable={1,2,2,6}
varTable.default=1
varTable.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;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值