/// <summary> /// Register some commonly used Unity classes and objects for Lua interop. /// To register more class objects externally to this class, register them in the Awake method of any /// monobehavior in your scene. /// </summary> protected virtual void InitFungusModule() { if (fungusModule == FungusModuleOptions.NoFungusModule) { return; } MoonSharp.Interpreter.Script interpreter = luaEnvironment.Interpreter; // Require the Fungus module and assign it to the global 'fungus' Table fungusTable = null; MoonSharp.Interpreter.DynValue value = interpreter.RequireModule("fungus"); if (value != null && value.Type == DataType.Function) { fungusTable = value.Function.Call().Table; } if (fungusTable == null) { UnityEngine.Debug.LogError("Failed to create Fungus table"); return; } interpreter.Globals["fungus"] = fungusTable; // Static classes fungusTable["time"] = UserData.CreateStatic(typeof(Time)); fungusTable["playerprefs"] = UserData.CreateStatic(typeof(PlayerPrefs)); fungusTable["prefs"] = UserData.CreateStatic(typeof(FungusPrefs)); fungusTable["factory"] = UserData.CreateStatic(typeof(PODTypeFactory)); // Lua Environment and Lua Utils components fungusTable["luaenvironment"] = luaEnvironment; fungusTable["luautils"] = this; // Provide access to the Unity Test Tools (if available). Type testType = Type.GetType("IntegrationTest"); if (testType != null) { UserData.RegisterType(testType); fungusTable["test"] = UserData.CreateStatic(testType); } // Populate the string table by parsing the string table JSON files stringTable = new Table(interpreter); fungusTable["stringtable"] = stringTable; foreach (TextAsset stringFile in stringTables) { if (stringFile.text == "") { continue; } JSONObject stringsJSON = new JSONObject(stringFile.text); if (stringsJSON == null || stringsJSON.type != JSONObject.Type.OBJECT) { UnityEngine.Debug.LogError("String table JSON format is not correct " + stringFile.name); continue; } foreach (string stringKey in stringsJSON.keys) { if (stringKey == "") { UnityEngine.Debug.LogError("String table JSON format is not correct " + stringFile.name); continue; } Table entriesTable = new Table(interpreter); stringTable[stringKey] = entriesTable; JSONObject entries = stringsJSON.GetField(stringKey); if (entries.type != JSONObject.Type.OBJECT) { UnityEngine.Debug.LogError("String table JSON format is not correct " + stringFile.name); continue; } foreach (string language in entries.keys) { string translation = entries.GetField(language).str; entriesTable[language] = translation; } } } stringSubstituter = new StringSubstituter(); if (fungusModule == FungusModuleOptions.UseGlobalVariables) { // Copy all items from the Fungus table to global variables foreach (TablePair p in fungusTable.Pairs) { if (interpreter.Globals.Keys.Contains(p.Key)) { UnityEngine.Debug.LogError("Lua globals already contains a variable " + p.Key); } else { interpreter.Globals[p.Key] = p.Value; } } interpreter.Globals["fungus"] = DynValue.Nil; // Note: We can't remove the fungus table itself because of dependencies between functions } }