void Start() { if (scriptType == ScriptType.Lua) { FileInfo file = new FileInfo(scriptPath); FileStream stream = file.OpenRead(); byte[] text = new byte[file.Length]; stream.Read(text, 0, (int)file.Length); LuaState lua = new LuaState(); lua.Start(); lua.DoString(System.Text.Encoding.Default.GetString(text)); string tableName = Path.GetFileNameWithoutExtension(scriptPath); LuaTable luaTable = lua.GetTable(tableName); if (luaTable != null) { LuaDictTable dict = luaTable.ToDictTable(); var iter = dict.GetEnumerator(); while (iter.MoveNext()) { if (iter.Current.Value is LuaFunction) { _functions.Add(iter.Current.Key as string); } else if ((iter.Current.Value as string) != null) { _properties.Add(iter.Current.Key as string); } } } } else { } }
private void InitConfig <T>(LuaTable luaTable) where T : IConfigItem, new() { string typeName = typeof(T).Name; if (_allConfigDict.ContainsKey(typeName)) { return; } try { bool hasRecord = false; Dictionary <int, T> dict = new Dictionary <int, T>(); LuaDictTable table = luaTable.ToDictTable(); luaTable.Dispose(); var iter2 = table.GetEnumerator(); while (iter2 != null) { var one = iter2.Current.Key; LuaTable content = iter2.Current.Value as LuaTable; if (content != null) { T configItem = new T(); configItem.SetKey(int.Parse(one.ToString())); configItem.CreateByLuaTable(content); if (dict.ContainsKey(configItem.GetKey())) { UnityEngine.Debug.LogError(string.Format("[{0}][{1}]配置表key重复:{2}", typeof(T), one, configItem.GetKey())); } else { hasRecord = true; dict.Add(configItem.GetKey(), configItem); } } //临时解决读表结束不退出循环 if ((one == null) && hasRecord) { break; } else { iter2.MoveNext(); } } _allConfigDict.Add(typeName, dict); table.Dispose(); } catch (Exception e) { UnityEngine.Debug.LogError(e); } }
/// <summary> /// 遍历lua表 /// <param name="table">需要遍历的表</param> /// <param name="ac">对表数据的操作委托</param> /// </summary> public static void dealTable(LuaTable table, Action <object, object> ac) { LuaDictTable dict = table.ToDictTable(); table.Dispose(); var iter2 = dict.GetEnumerator(); while (iter2.MoveNext()) { ac(iter2.Current.Key, iter2.Current.Value); } iter2.Dispose(); dict.Dispose(); }
void Start() { #if UNITY_5 || UNITY_2017 Application.logMessageReceived += ShowTips; #else Application.RegisterLogCallback(ShowTips); #endif new LuaResLoader(); lua = new LuaState(); lua.Start(); DelegateFactory.Init(); lua.DoString(script); //Get the function object luaFunc = lua.GetFunction("test.luaFunc"); if (luaFunc != null) { int num = luaFunc.Invoke <int, int>(123456); Debugger.Log("generic call return: {0}", num); num = CallFunc(); Debugger.Log("expansion call return: {0}", num); Func <int, int> Func = luaFunc.ToDelegate <Func <int, int> >(); num = Func(123456); Debugger.Log("Delegate call return: {0}", num); num = lua.Invoke <int, int>("test.luaFunc", 123456, true); Debugger.Log("luastate call return: {0}", num); } LuaTable luaTable = lua.GetTable("test"); luaTable.Call <int>("luaFunc", 1000); LuaDictTable dict = luaTable.ToDictTable(); var iter2 = dict.GetEnumerator(); while (iter2.MoveNext()) { Debugger.Log("map item, k,v is {0}:{1}", iter2.Current.Key, iter2.Current.Value); } iter2.Dispose(); dict.Dispose(); luaTable.Dispose(); lua.CheckTop(); }
// Start is called before the first frame update void Start() { LuaManager.GetInstance().Init(); LuaManager.GetInstance().LuaState.Require("Main"); //学习如何调用Lua中的List 和 Dic //List //toLua中的 C#得到Lua中的表 只有一个套路 通过LuaTable获取 LuaTable luaTable = LuaManager.GetInstance().LuaState.GetTable("testList"); //1,2.对应的是位置 不是Key Debug.Log("luTable:" + luaTable[1]); Debug.Log("luTable:" + luaTable[2]); Debug.Log("luTable:" + luaTable[3]); Debug.Log("luTable:" + luaTable[4]); Debug.Log("luTable:" + luaTable[5]); //遍历 //先转成数组 object[] objs = luaTable.ToArray(); foreach (var i in objs) { Debug.Log("遍历打印:" + i); } //是引用拷贝 luaTable[1] = 999; LuaTable luaTable2 = LuaManager.GetInstance().LuaState.GetTable("testList"); Debug.Log("测试引用:" + luaTable2[1]); LuaTable luaTable1 = LuaManager.GetInstance().LuaState.GetTable("testList2"); //1,2.对应的是位置 不是Key Debug.Log("luaTable1:" + luaTable1[1]); Debug.Log("luaTable1:" + luaTable1[2]); Debug.Log("luaTable1:" + luaTable1[3]); Debug.Log("luaTable1:" + luaTable1[4]); Debug.Log("luaTable1:" + luaTable1[5]); objs = luaTable1.ToArray(); foreach (var i in objs) { Debug.Log("遍历打印:" + i); } //Dictionary相关 LuaTable dic = LuaManager.GetInstance().LuaState.GetTable("testDic"); Debug.Log(dic["1"]); Debug.Log(dic["2"]); Debug.Log(dic["3"]); Debug.Log(dic["4"]); LuaDictTable <string, int> luaDic = dic.ToDictTable <string, int>(); Debug.Log("luaDic:" + luaDic["1"]); Debug.Log("luaDic:" + luaDic["2"]); Debug.Log("luaDic:" + luaDic["3"]); Debug.Log("luaDic:" + luaDic["4"]); //dictionary也是引用拷贝 dic["1"] = 8527; LuaTable luaTable3 = LuaManager.GetInstance().LuaState.GetTable("testDic"); Debug.Log("luaTable3引用拷贝测试:" + dic["1"]); //如果想通过中括号得到值 只支持string和int 其它类型的键值无法获取 LuaTable dic2 = LuaManager.GetInstance().LuaState.GetTable("testDic2"); //Debug.Log(dic2[true]); LuaDictTable <object, object> luaDic2 = dic2.ToDictTable <object, object>(); Debug.Log("luaDic2:" + luaDic2["1"]); Debug.Log("luaDic2:" + luaDic2[true]); Debug.Log("luaDic2:" + luaDic2["4"]); Debug.Log("luaDic2:" + luaDic2[3]); //dic使用迭代器遍历 IEnumerator <LuaDictEntry <object, object> > dicIE = luaDic2.GetEnumerator(); while (dicIE.MoveNext()) { Debug.Log(dicIE.Current.Key + "_" + dicIE.Current.Value); } List <int> tes = null; List <int> .Enumerator tefd = tes.GetEnumerator(); }