(补充: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>();
s["321"]=

本文介绍了在Unity+lua环境下,如何将C#的泛型结构如List、IEnumerable和Dictionary转换成lua table以便在lua中遍历。文中提到的方法可能导致内存泄露问题,不建议使用,因为NewLuaTable方法可能引起引用计数无法清除,影响lua虚拟机的垃圾回收。
最低0.47元/天 解锁文章
848

被折叠的 条评论
为什么被折叠?



