LuaTable转C#的列表List和字典Dictionaty
- 介绍
- lua中创建表测试
- lua中list表
- 表转成List
- 表转成Dictionary
- 键值对表
- 表转成Dictionary
- 多类型键值对表
- 表转成Dictionary
- 总结
介绍
之前基本都是从C#中的List或者Dictionary转成luaTable,很少会把LuaTable转成C#的List或者Dictionary,前几天正好做需求的时候要将lua里面的数据转成C#中的集合,C#List或者Dictionary转成luaTable的文章之前写过可以直接参考。
lua中创建表测试
lua中list表
注意luatable的数据是从1开始的 不是从0开始,这里默认的Key是1,2,3,4,5,6,7,8,9
--lua中的表
this.testList = {1,2,3,4,5,6,7,8,9}
--C#中
//获取lua表
LuaTable table = LuaManager.lua.GetTable("UIMainCityCtrl.testList");
//lua表的长度打印
Debug.LogError("table.Length = " + table.Length);
//转换成数组打印
object[] tabArray = table.ToArray();
for (int i = 0; i < tabArray.Length; i++)
{
Debug.LogError("tabArray[" + i + "] = " + tabArray[i]);
}
//使用luaTable直接打印
for (int i = 0; i < table.Length; i++)
{
Debug.LogError("table[" + (i + 1).ToString() + "] = " + table[(i + 1)]);
}
//上面的这两种打印的结果都是一样的。如果使用的是
打印如下所示:
表转成List
这两个方法本质上其实是一样的,LuaArrayTable可以理解成是对object[]的一层封装
方法一
object[] obj = table.ToArray();
List<object> arr1 = new List<object>(obj);
#region 打印一
var iter = arr1.GetEnumerator();
while (iter.MoveNext())
{
Debug.LogError("iter.Current = " + iter.Current);
}
#endregion
#region 打印二
arr1.ForEach((item) => {
Debug.LogError("item = " + item);
});
#endregion
方法二
这里只是转换方法稍微变了一下,这里不多解释了,只是封装了一下
//LuaArrayTable luaArray = table.ToArrayTable(); 另一种转换方法
LuaArrayTable luaArray = new LuaArrayTable(table);
//LuaArrayTable的打印如下
var iter = luaArray.GetEnumerator();
while (iter.MoveNext())
{
Debug.LogError("iter = " + iter.Current);
}
List<object> arr2 = new List<object>(luaArray);
//简化下来可以这样
List<object> arr2 = luaArray.ToList();
注意一点LuaArrayTable其实就是对List的一层封装,所以Key只能是Int
表转成Dictionary
//直接转成Object,object类型字典
LuaDictTable<object, object> luaArray = table.ToDictTable<object, object>();
//Dictionary<object, object> dic = luaArray.ToDictionary(k => k.Key, v => v.Value);
Dictionary<object, object> dic = luaArray.ToDictionary();
//转成Int,Object类型的字典
LuaDictTable<int, object> luaArray = table.ToDictTable<int, object>();
//Dictionary<int, object> dic = luaArray.ToDictionary(k => k.Key, v => v.Value);
Dictionary<int, object> dic = luaArray.ToDictionary();
//转成int,int类型的字典
LuaDictTable<int, int> luaArray = table.ToDictTable<int, int>();
//Dictionary<int, int> dic = luaArray.ToDictionary(k => k.Key, v => v.Value);
Dictionary<int, int> dic = luaArray.ToDictionary();
//转成int,string类型的字典
LuaDictTable<int, string> luaArray = table.ToDictTable<int, string>();
//Dictionary<int, string> dic = luaArray.ToDictionary(k => k.Key, v => v.Value);
Dictionary<int, string> dic = luaArray.ToDictionary();
//打印代码
var itor = dic.GetEnumerator();
while (itor.MoveNext())
{
Debug.LogError("itor.Current.Key = " + itor.Current.Key + " ,itor.Current.Value = " + itor.Current.Value);
}
itor.Dispose();
打印如下
键值对表
键值对的这种表必然是无法转成list,既然是键值对只能转成字典Dictionary
--lua中
--table表现的dictionary
this.testDic = {
["1"] = 50,
["a"] = 80,
["3"] = 120,
["b"] = 130
}
//C#中
//获取lua表的另一种获取方法
//LuaTable table = LuaManager.lua.GetTable("UIMainCityCtrl.testDic");
//lua表的长度打印
Debug.LogError("table.Length = " + table.Length);
LuaDictTable dic = table.ToDictTable();
//同上面的这个转换一样
//LuaDictTable<string, object> dic = table.ToDictTable<string,object>();
//转成value为int的键值对字典
//LuaDictTable<string, int> dic = table.ToDictTable<string, int>();
var itor = dic.GetEnumerator();
while (itor.MoveNext())
{
Debug.LogError("itor.Current.Key = " + itor.Current.Key + " ,itor.Current.Value = " + itor.Current.Value);
}
itor.Dispose();
打印如下所示:
这里有一点是键值对方式的LuaTable是没有长度的,长度为0
表转成Dictionary
转换成字典的方式
//string,object类型字典
LuaDictTable dic = table.ToDictTable();
Dictionary<string, object> unitydic = dic.ToDictionary(k=>k.Key.ToString(),v => v.Value);
//LuaDictTable<string, object> dic = table.ToDictTable<string, object>();
//Dictionary<string, object> unitydic = dic.ToDictionary();
//string int类型字典
LuaDictTable<string, int> dic = table.ToDictTable<string, int>();
Dictionary<string, int> unitydic = dic.ToDictionary();
//打印如下
var itor = unitydic.GetEnumerator();
while (itor.MoveNext())
{
Debug.LogError("itor.Current.Key = " + itor.Current.Key + " ,itor.Current.Value = " + itor.Current.Value);
}
itor.Dispose();
多类型键值对表
表转成Dictionary
键值对的这种表必然是无法转成list,既然是键值对只能转成字典Dictionary
--lua中
--table表现的dictionary
this.testDic = {
["1"] = 1,
[true] = 1,
[false] = true,
["123"] = false,
[5] = "heiheihei"
}
//C#中
//获取lua表的另一种获取方法
//LuaTable table = LuaManager.lua.GetTable("UIMainCityCtrl.testDic");
LuaDictTable<object,object> dic = table.ToDictTable<object, object>();
Dictionary<object, object> unitydic = dic.ToDictionary();
var itor = unitydic.GetEnumerator();
while (itor.MoveNext())
{
Debug.LogError("itor.Current.Key = " + itor.Current.Key + " ,itor.Current.Value = " + itor.Current.Value);
}
itor.Dispose();
总结
lua中的LuaTable、LuaArrayTable、LuaDictTable<K,V>这几个其实可以跟Unity中List和Dictionary相互转换。
后面会说一下距离怎么在C#中增删改查LuaTable、LuaArrayTable、LuaDictTable中的数据