public void Init() { Log.i("Lua init begin"); if (!dirty) { return; } //==================== if (mL != null) { //释放以便重加载 Log.i("Lua Dispose"); mL.Dispose(); mL = null; } //==================== LUA_PATH = ResLoad.resPath + "/lua/"; mReadLuaFile = readLuaFile; if (!Directory.Exists(LUA_PATH)) { LUA_PATH = Application.streamingAssetsPath + "/Lua/"; //路径区分大小写 #if UNITY_ANDROID && !UNITY_EDITOR mReadLuaFile = readAssetLuaFile; #endif } //==================== #if USE_ULUA mL = new LuaState(); bindLuaClass(this); #elif USE_XLUA mL = new LuaEnv(); mL.AddLoader(LuaLoader); //引入第三方lua库 mL.AddBuildin("rapidjson", XLua.LuaDLL.Lua.LoadRapidJson); mL.AddBuildin("lpeg", XLua.LuaDLL.Lua.LoadLpeg); mL.AddBuildin("pb", XLua.LuaDLL.Lua.LoadLuaProfobuf); //mL.AddBuildin("ffi", XLua.LuaDLL.Lua.LoadFFI);IOS存在兼容问题 #endif LuaHelp.ExportToLua(); byte[] tags = mReadLuaFile(LUA_PATH + "main.lua", false); mEncode = tags [2] == 0x3d ? false : true; //==================== //设置Lua脚本根路径列表,并执行入口脚本main.lua mL.DoString("package.path = package.path .. ';' .. '" + LUA_PATH + "?.lua';require 'main';"); //==================== mNewLuaObject = (LuaFunction)mL["newLuaObject"]; mReloadLua = (LuaFunction)mL["reloadLua"]; mPushLuaEvent = (LuaFunction)mL["pushLuaEvent"]; mProcessLuaEvent = (LuaFunction)mL["processLuaEvent"]; mGameConfig = (LuaTable)mL["LGameConfig"]; ((LuaFunction)mL ["main"]).Call(); //==================== dirty = false; Log.i("Lua init end"); }