本篇文章为大家展示了如何在C#项目中集成一个Lua脚本,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
这个dll是一个很轻量级的库,100kb左右,引用这个库可以通过Nuget包管理器来引用,当前引用的版本是1.5.7.0,我们看看引用之后的添加了哪些DLL。
这个里面lua54.dll有x86和x64两个类型的版本,这个在使用的时候需要注意因为我们生成设置选择的是Any CPU所以这里会有两个版本的dll,这里使用的时候需要注意。
下面通过一个控制台应用程序来看看这个脚本到底该怎么使用,这里包括直接创建表达式,注册方法并使用lua调用C#函数以及直接导入C#的库然后再调用里面内部的方法这三个方面进行描述。
我们来直接看控制台程序中的代码
class Program
{
static void Main(string[] args)
{
using (var state = new Lua())
{
//Evaluating simple expressions:
//Lua can return multiple values, for this reason DoString return a array of objects
var res0 = state.DoString("return 10 + 3*(5 + 2)")[0];
Console.WriteLine($"Output result0:{res0}");
//Passing raw values to the state:
double val = 12.0;
state["x"] = val; // Create a global value 'x'
var res1 = (double)state.DoString("return 10 + x*(5 + 2)")[0];
Console.WriteLine($"Output result1:{res1}");
//Retrieving global values:
state.DoString("y = 10 + x*(5 + 2)");
double y = (double)state["y"]; // Retrieve the value of y
Console.WriteLine($"Y result:{y}");
Console.ReadKey();
}
}}
注意事项:
首先来看这个注释:Lua can return multiple values, for this reason DoString return a array of objects,就是直接调用DoString方法的时候返回的结果是一个object[]类型,所以这里需要取结果的时候要取用第一个
这里我们通过直接在DoString中定义好function,然后通过Call方法进行调用,我们再来看下面的示例及返回结果
class Program
{
static void Main(string[] args)
{
using (var state = new Lua())
{
//Retrieving Lua functions:
state.DoString(@"function ScriptFunc (val1, val2)
if val1 > val2 then
return val1 + 1
else
return val2 - 1
end
end
");
var scriptFunc = state["ScriptFunc"] as LuaFunction;
var funcRes = scriptFunc.Call(3, 5).First();
Console.WriteLine($"Func result:{funcRes}");
Console.ReadKey();
}
}
}
同样的我们也来看看最终执行的结果
下面的例子包含了几种不同的参数类型及返回类型用来演示调用的完整过程。
using System;
using System.Linq;
using NLua;
namespace NLuaConsoleApp
{
class Program
{
static void Main(string[] args)
{
using (var state = new Lua())
{
////---------------------------------------------------lua调用c#函数
TestClass obj = new TestClass();
// 注册CLR对象方法到Lua,供Lua调用 typeof(TestClass).GetMethod("TestPrint")
state.RegisterFunction("TestPrint", obj, obj.GetType().GetMethod("TestPrint"));
// 注册CLR对象方法到Lua,供Lua调用 typeof(TestClass).GetMethod("AnotherFunc")
state.RegisterFunction("AnotherFunc", obj, obj.GetType().GetMethod("AnotherFunc"));
// 注册CLR静态方法到Lua,供Lua调用
state.RegisterFunction("TestStaticPrint", null, typeof(TestClass).GetMethod("TestStaticPrint"));
state.DoString("TestPrint(10,20)");
state.DoString("AnotherFunc('10','20')");
state.DoString("TestStaticPrint()");
Console.ReadKey();
}
}
class TestClass
{
public int TestPrint(int num,int num2)
{
var result = num + num2;
Console.WriteLine("Print result:" + result);
return result;
}
public void AnotherFunc(string val1, string val2)
{
Console.WriteLine($"MyTest,Param1:{val1},Param2:{val2}");
}
public static void TestStaticPrint()
{
Console.WriteLine("TestStaticPrint");
}
}
}
}
同样的我们来看整个测试的返回完整结果。
这个是按照官方的例子进行模拟的,但是在调用的时候总是报错,现贴出具体的示例供后面排查使用
class Program
{
static void Main(string[] args)
{
using (var state = new Lua())
{
state.LoadCLRPackage();
state.DoString(@" import ('System.Web') ");
state.DoString(@" client = WebClient() ");
state.DoString(@"local res = client:DownloadString('http://nlua.org')");
Console.ReadKey();
}
}
}
这个就是说通过调用C#程序集并引入命名空间,然后调用其内部的WebClient方法,这个在调试的时候一直都是报错,这个暂时记录供以后进行排查错误
上述内容就是如何在C#项目中集成一个Lua脚本,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。