Exemplo n.º 1
0
    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);
        }
    }
Exemplo n.º 2
0
    /// <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();
    }
Exemplo n.º 3
0
    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();
    }