public void CustomDestroy() { if (null == m_luaVM) { return; } m_luaVM.Dispose(); m_luaVM = null; Debug.Log("~LuaManager was destroyed!"); }
private void OnDestroy() { if (m_luaEnv != null) { m_luaEnv.DoString("__lua_runtime__.Cleanup()"); m_luaEnv.Dispose(); } m_luaEnv = null; if (Singleton == this) { Singleton = null; } }
/// <summary> /// 运行lua文件并获取结果 /// </summary> /// <param name="file"></param> /// <param name="args"></param> /// <returns></returns> public static string Run(string file, ArrayList args = null) { //文件不存在 if (!File.Exists(Tools.Global.ProfilePath + "user_script_send_convert/" + file)) { return(""); } if (luaRunner == null) { luaRunner = new XLua.LuaEnv(); luaRunner.Global.SetInPath("runType", "send");//一次性处理标志 Initial(luaRunner, "send"); } lock (luaRunner) { luaRunner.Global.SetInPath("file", file); try { if (args != null) { for (int i = 0; i < args.Count; i += 2) { luaRunner.Global.SetInPath((string)args[i], args[i + 1].ToString()); System.Diagnostics.Debug.WriteLine($"{(string)args[i]},{args[i + 1]}"); } } return(luaRunner.DoString("return require('core_script.once')()")[0].ToString()); } catch (Exception e) { luaRunner.Dispose(); luaRunner = null; throw new Exception(e.ToString()); } } }
// Start is called before the first frame update void Start() { XLua.LuaEnv luaenv = new XLua.LuaEnv(); luaenv.DoString("CS.UnityEngine.Debug.Log('hello world')"); luaenv.Dispose(); }
// Parse the command and try to execute it public static void ExecuteCommand(string command) { if (command == null) { return; } command = command.Trim(); if (command.Length == 0) { return; } if (command.Substring(0, 4).ToLower() == "lua:") { var env = new XLua.LuaEnv(); env.DoString(command.Substring(4)); env.Dispose(); return; } for (int i = 0, imax = m_commondActions.Count; i < imax; i++) { if (m_commondActions[i](command)) { return; } } // Parse the arguments commandArguments.Clear(); int endIndex = IndexOfChar(command, ' ', 0); commandArguments.Add(command.Substring(0, endIndex)); for (int i = endIndex + 1; i < command.Length; i++) { if (command[i] == ' ') { continue; } int delimiterIndex = IndexOfDelimiter(command[i]); if (delimiterIndex >= 0) { endIndex = IndexOfChar(command, inputDelimiters[delimiterIndex][1], i + 1); commandArguments.Add(command.Substring(i + 1, endIndex - i - 1)); } else { endIndex = IndexOfChar(command, ' ', i + 1); commandArguments.Add(command.Substring(i, endIndex - i)); } i = endIndex; } // Check if command exists ConsoleMethodInfo methodInfo; if (!methods.TryGetValue(commandArguments[0], out methodInfo)) { Debug.LogWarning("Can't find command: " + commandArguments[0]); } else if (!methodInfo.IsValid()) { Debug.LogWarning("Method no longer valid (instance dead): " + commandArguments[0]); } else { // Check if number of parameter match if (methodInfo.parameterTypes.Length != commandArguments.Count - 1) { Debug.LogWarning("Parameter count mismatch: " + methodInfo.parameterTypes.Length + " parameters are needed"); return; } Debug.Log("Executing command: " + commandArguments[0]); // Parse the parameters into objects object[] parameters = new object[methodInfo.parameterTypes.Length]; for (int i = 0; i < methodInfo.parameterTypes.Length; i++) { string argument = commandArguments[i + 1]; Type parameterType = methodInfo.parameterTypes[i]; ParseFunction parseFunction; if (!parseFunctions.TryGetValue(parameterType, out parseFunction)) { Debug.LogError("Unsupported parameter type: " + parameterType.Name); return; } object val; if (!parseFunction(argument, out val)) { Debug.LogError("Couldn't parse " + argument + " to " + parameterType.Name); return; } parameters[i] = val; } // Execute the method associated with the command object result = methodInfo.method.Invoke(methodInfo.instance, parameters); if (methodInfo.method.ReturnType != typeof(void)) { // Print the returned value to the console if (result == null || result.Equals(null)) { Debug.Log("Value returned: null"); } else { Debug.Log("Value returned: " + result.ToString()); } } } }