C#与lua交互
2016-12-01 00:00
435 查看
摘要: 利用MoonSharp实现c#与lua脚本的交互
发现一个c#与lua脚本交互的工具MoonSharp
http://www.moonsharp.org/
直接下载release版本,添加MoonSharp.Interpreter.dll的引用之后,即可使用。示例代码如下:
MoonSharp还包含了Unity3D的包,可以直接导入unity3d使用,非常方便。
发现一个c#与lua脚本交互的工具MoonSharp
http://www.moonsharp.org/
直接下载release版本,添加MoonSharp.Interpreter.dll的引用之后,即可使用。示例代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using MoonSharp.Interpreter; namespace LuaOperation { class Program { static void Main(string[] args) { Console.WriteLine("result1 = {0}", CallbackTest1()); Console.WriteLine("result2 = {0}", CallbackTest2()); Console.WriteLine("result3 = {0}", EnumerableTest()); Console.WriteLine("result4 = {0}", TableTest1()); Console.WriteLine("result5 = {0}", TableTest2()); Console.WriteLine("result6 = {0}", TableTestReverse()); Console.WriteLine("result7 = {0}", TableTestReverseSafer()); Console.WriteLine("result8 = {0}", TableTestReverseWithTable()); Console.ReadKey(); } private static double CallbackTest1() { string scriptCode = @" -- defines a factorial function function fact (n) if (n == 0) then return 1 else return n * fact(n - 1); end end"; Script script = new Script(); script.DoString(scriptCode); DynValue res = script.Call(script.Globals["fact"], 4); return res.Number; } private static int Mul(int a, int b) { return a * b; } private static double CallbackTest2() { string scriptCode = @" -- defines a factorial function function fact (n) if (n == 0) then return 1 else return Mul(n, fact(n - 1)); end end"; Script script = new Script(); script.Globals["Mul"] = (Func<int, int, int>)Mul; script.DoString(scriptCode); DynValue res = script.Call(script.Globals["fact"], 4); return res.Number; } private static IEnumerable<int> GetNumbers() { for (int i = 1; i <= 10; i++) yield return i; } private static double EnumerableTest() { string scriptCode = @" total = 0; for i in getNumbers() do total = total + i; end return total; "; Script script = new Script(); script.Globals["getNumbers"] = (Func<IEnumerable<int>>)GetNumbers; DynValue res = script.DoString(scriptCode); return res.Number; } private static List<int> GetNumberList() { List<int> lst = new List<int>(); for (int i = 1; i <= 10; i++) lst.Add(i); return lst; } private static double TableTest1() { string scriptCode = @" total = 0; tbl = getNumbers() for _, i in ipairs(tbl) do total = total + i; end return total; "; Script script = new Script(); script.Globals["getNumbers"] = (Func<List<int>>)GetNumberList; DynValue res = script.DoString(scriptCode); return res.Number; } private static Table GetNumberTable(Script script) { Table tbl = new Table(script); for (int i = 1; i <= 10; i++) tbl[i] = i; return tbl; } private static double TableTest2() { string scriptCode = @" total = 0; tbl = getNumbers() for _, i in ipairs(tbl) do total = total + i; end return total; "; Script script = new Script(); script.Globals["getNumbers"] = (Func<Script, Table>)(GetNumberTable); DynValue res = script.DoString(scriptCode); return res.Number; } public static double TableTestReverse() { string scriptCode = @" return dosum { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } "; Script script = new Script(); script.Globals["dosum"] = (Func<List<int>, int>)(l => l.Sum()); DynValue res = script.DoString(scriptCode); return res.Number; } public static double TableTestReverseSafer() { string scriptCode = @" return dosum { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } "; Script script = new Script(); script.Globals["dosum"] = (Func<List<object>, int>)(l => l.OfType<int>().Sum()); DynValue res = script.DoString(scriptCode); return res.Number; } static double Sum(Table t) { var nums = from v in t.Values where v.Type == DataType.Number select v.Number; return nums.Sum(); } private static double TableTestReverseWithTable() { string scriptCode = @" return dosum { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } "; Script script = new Script(); script.Globals["dosum"] = (Func<Table, double>)Sum; DynValue res = script.DoString(scriptCode); return res.Number; } } }
MoonSharp还包含了Unity3D的包,可以直接导入unity3d使用,非常方便。
相关文章推荐
- Lua学习之路_C#与Lua的交互001
- BOLT.NET中的关于数据库链接和使用的LUA与C#语言的交互设计
- 在C#环境中与Lua进行交互(一)
- Unity C#与Xlua的交互 C#调用Lua CSharpCallLua
- Lua_ uLua_Lua和C#变量交互_018
- c# lua 简单交互
- Lua_ uLua_Lua和C#变量交互_018
- lua 与c#交互原理
- Lua与C#交互
- Tolua使用笔记二:lua与C#的函数,变量交互方法
- UNITY之Lua与C#交互
- Unity3d ulua c#与lua交互+wrap文件理解
- 用好Lua+Unity,让性能飞起来——Lua与C#交互篇
- c#和 lua 简单交互
- c# lua 简单交互
- 用好Lua+Unity,让性能飞起来——Lua与C#交互篇
- 用好Lua+Unity,让性能飞起来——Lua与C#交互篇
- Tolua使用笔记二:lua与C#的函数,变量交互方法
- 在C#环境中与Lua进行交互(二)
- Unity3d ulua c#与lua交互+wrap文件理解