一.运行工程
本例展示如何用C#访问Lua变量,运行表现很简单,打印几行文本

二.代码分析
2.1代码展示
cs
using UnityEngine;
using System.Collections.Generic;
using LuaInterface;
public class AccessingLuaVariables : MonoBehaviour
{
private string 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);
#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
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();
}
private void OnApplicationQuit()
{
#if UNITY_5 || UNITY_2017 || UNITY_2018
Application.logMessageReceived -= ShowTips;
#else
Application.RegisterLogCallback(null);
#endif
}
string tips = null;
void ShowTips(string msg, string stackTrace, LogType type)
{
tips += msg;
tips += "\r\n";
}
void OnGUI()
{
GUI.Label(new Rect(Screen.width / 2 - 300, Screen.height / 2 - 200, 600, 400), tips);
}
}
2.2 ToLua 部分API用法
下面给出README(LuaFramework\ToLua\Examples\README.md)文档中的解释
* luaState["Objs2Spawn"] LuaState通过重载this操作符,访问lua _G表中的变量Objs2Spawn <br>
* LuaState.GetTable 从lua中获取一个lua table, 可以串式访问比如lua.GetTable("varTable.map.name") 等于 varTable->map->name<br>
* LuaTable 支持this操作符,但此this不支持串式访问。比如table["map.name"] "map.name" 只是一个key,不是table->map->name <br>
* LuaTable.GetMetaTable() 可以获取当前table的metatable <br>
* LuaTable.ToArray() 获取数组表中的所有对象存入到object[]表中 <br>
* LuaTable.AddTable(name) 在当前的table表中添加一个名字为name的表 <br>
* LuaTable.GetTable(key) 获取t[key]值到c#, 类似于 lua_gettable <br>
* LuaTable.SetTable(key, value) 等价于t[k] = v的操作, 类似于lua_settable <br>
* LuaTable.RawGet(key) 获取t[key]值到c#, 类似于 lua_rawget <br>
* LuaTable.RawSet(key, value) 等价于t[k] = v的操作, 类似于lua_rawset <br>
三.C#访问Lua变量小结
3.1 访问非table变量
Lua
num1 = 15
cs
Debug.Log(lua["num1"]);
3.2 用key访问table
Lua
table1 = {value1 = 2025}
cs
Debug.Log(lua["table1.value1"]);
3.3 访问嵌套table
Lua
table1 = {table2 = {value2 = 100}}
cs
LuaTable table2 = lua.GetTable("table1.table2");
Debug.Log(table2["value2"]);
3.4 遍历数组
Lua
table1 = {1,2,3,4,5}
cs
LuaTable table1 = lua.GetTable("table1");
object[] list = table1.ToArray();
for (int i = 0; i < list.Length; i++)
{
Debugger.Log("table1[{0}], is {1}", i, list[i]);
}