Ylisar 发表于 2022-4-20 14:27

ToLua 入门05_AccessingLuaVariables

Tolua中的变量怎么去使用和赋值,在案例场景ToLua/Examples/04_AccessingLuaVariables中可以去学习;本章我们就分析下具体使用方式,首先先看一下提供的示范代码。
using UnityEngine;using System.Collections.Generic;using LuaInterface;publicclassAccessingLuaVariables:MonoBehaviour{privatestring script =@"
            print('Objs2Spawn is: '..Objs2Spawn)
            var2read = 42
            varTable = {1,2,3,4,5}
            varTable.default = 1
            varTable.map = {}
            varTable.map.name = 'map'
            
            meta = {name = 'meta'}
            setmetatable(varTable, meta)
            
            function TestFunc(strs)
                print('get func by variable')
            end
      ";void Start (){#if UNITY_5 || UNITY_2017 || UNITY_2018
      Application.logMessageReceived += ShowTips;#else
      Application.RegisterLogCallback(ShowTips);#endifnewLuaResLoader();LuaState lua =newLuaState();
      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 拆串式tableLuaFunction 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);}

      table.Dispose();                        
      lua.CheckTop();
      lua.Dispose();}privatevoidOnApplicationQuit(){#if UNITY_5 || UNITY_2017 || UNITY_2018
      Application.logMessageReceived -= ShowTips;#else
      Application.RegisterLogCallback(null);#endif}string tips =null;voidShowTips(string msg,string stackTrace,LogType type){
      tips += msg;
      tips +="\r\n";}voidOnGUI(){
      GUI.Label(newRect(Screen.width /2-300, Screen.height /2-200,600,400), tips);}}首先第一步初始化lua脚本:
    new LuaResLoader();
    LuaState lua = new LuaState();
    lua.Start();
    lua["Objs2Spawn"] = 5; //对脚本中Objs2Spawn变量进行赋值。
    lua.DoString(script);在初始化时可以直接获取变量对lua脚本中变量赋值。
第二步访问变量:
    //通过LuaState访问
    Debugger.Log("Read var from lua: {0}", lua["var2read"]);
    Debugger.Log("Read table var from lua: {0}", lua["varTable.default"]);//LuaState 拆串式table第三步调用脚本方法
    LuaFunction func = lua["TestFunc"] as LuaFunction;
    func.Call();
    func.Dispose();第四步读取的内容转为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";//varTable.map.name 变量改变
    //改变后打印varTable.map.name 变量
    Debugger.Log("Modify varTable name: {0}", table["map.name"]);

    //table添加新的luaTable
    table.AddTable("newmap");
    LuaTable table1 = (LuaTable)table["newmap"];
    //新的luaTable 表加入字段(变量)
    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);
    }
页: [1]
查看完整版本: ToLua 入门05_AccessingLuaVariables