编辑模式下运行lua文件

作者:追风剑情 发布于:2018-4-10 17:43 分类:Lua

示例:导出lua语言包到一个csv文件


[MenuItem ("Tool/ExportLuaLanguage CSV")]
   static void ExportLuaLanguageCSV()
   {
        //加载lua文件
        string luaLangFile = Application.dataPath + @"\Lua\helper\CErrorCode.lua";
        StreamReader reader = File.OpenText(luaLangFile);
        string script = reader.ReadToEnd();
        reader.Close();

        //执行lua文件
        LuaEnv evn = new LuaEnv();
        LuaFunction fun = evn.LoadString(script);
        fun.Call();//必须要执行一下

        //获取想导出的table
        LuaTable CErrorCode = evn.Global.Get<string, LuaTable>("CErrorCode");
        
        StringBuilder sb = new StringBuilder();
        //这里根据自己定义的lua对象格式来遍历
        CErrorCode.ForEach<string, LuaTable>((k1, v1)=>{
            if(v1 == null)
                return;
            v1.ForEach<string, string>((k2, v2)=>{
                v2 = v2.Replace("\n", "\\\\n");
                sb.AppendFormat("{0}.{1}\t{2}\n", k1, k2, v2);
            });
        });
        evn.Dispose();

        //保存为.csv文件
        string savefile = EditorUtility.SaveFilePanel("保存", Application.dataPath, "CErrorCode", "csv");
        StreamWriter writer = File.CreateText(savefile);
        writer.Write(sb.ToString());
        writer.Flush();
        writer.Close();
        Debug.Log(savefile);
   }

标签: xLua

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号