(补充:2016年11月3日 ,不要使用本文的方法,经测试会导致内存泄露。。原因是NewLuaTable方法会导致引用计数无法清除,无法正确的被lua虚拟机垃圾回收)
注:本文中使用的环境为Unity+lua
在使用lua的时候,我们经常需要调用遍历调用C#中的泛型结构(诸如List、IEnumberable、Dictionary)。在LUA中最通用的遍历方法是针对table的调用,所以我们可以将C#中的数据结构转换为lua table,然后作为参数传进lua中。
lua代码为
function test(k)
for i,v in pairs(k) do
Debug.Log(string.format("%d %s",i,v))
end
end
我们传进来k为lua table,那么如何生成它的呢?
List<string> t = new List<string>(){
"test1", "test2"};
LuaManager.Call ("test", t.toLuaTable());
Dictionary<string,string> s = new Dictionary<string, string