MoonSharp 文档一
目录
1.Getting Started(入门手册)
步骤1:在 IDE 中引入 MoonSharp
步骤2:引入命名空间
步骤3:调用脚本
步骤4:运行代码
2.Keeping a Script around(保留一个脚本)
步骤1:复现前教程所有操作
步骤2:改为创建Script对象
步骤3:访问全局环境
步骤4:直接调用函数
3.DynValue revealed
步骤1:重新执行你在上一个教程中的所有操作
步骤2:将 fact 函数存入 DynValue
步骤3:将数字存入 DynValue
步骤 4:了解 DataType(s)
步骤 5:元组
4.Calling back C#(回调C#)
步骤 1:永远不要厌倦阶乘
步骤 2:自定义乘法函数
返回一系列数字
返回表格
接收一个表
5.Auto-conversions explained(自动转换说明)
自定义转换器
CLR 类型到 MoonSharp 类型的自动转换
MoonSharp 类型到 CLR 类型的标准自动转换
MoonSharp 类型到 CLR 类型的受限自动转换
1.Getting Started(入门手册)
您的第一个 MoonSharp 项目的快速指南
文档地址:MoonSharp
本教程将带你初步体验 MoonSharp 的简洁与强大。虽然 MoonSharp 有更优的使用方式,但这是最基础的一种。其他教程会深入探讨如何充分发挥 MoonSharp 的潜力。现在,让我们开始吧!
关于语言支持的说明
本网站大多数教程仅提供 C# 示例,但本页会为 VB.NET 用户提供一些指引。
MoonSharp 兼容所有 CLR 语言(C#、VB.NET、C++/CLI、F#、Boo 等),理论上也支持DLR语言(如 IronPython、IronRuby)。但由于维护多语言示例的工作量较大,后续教程仅提供 C# 示例(本入门页含 VB.NET)。
大多数教程的代码可在 GitHub 的示例项目中找到。
学习前提:需熟悉Lua和至少一门.NET语言(推荐 C#),否则可能难以理解示例。
步骤1:在 IDE 中引入 MoonSharp
根据使用的IDE(Visual Studio、MonoDevelop、SharpDevelop、Unity)选择以下方式:
Visual Studio(通过 NuGet)
1.在包管理器控制台输入:
PM> Install-Package MoonSharp
2.或右键“引用”->“管理NuGet包”->搜索“MoonSharp”并安装。
Xamarin Studio(通过NuGet)
菜单栏选择“项目”->“添加NuGet包”->搜索“MoonSharp”并安装。
其他IDE(手动添加)
将 MoonSharp 发行包中对应平台的 MoonSharp.Interpreter.dll 添加为项目依赖。
Unity
1.推荐方式:通过Asset Store安装(待审核后在此链接提供)。
2.手动方式:
将 interpreter/net35 目录下的 MoonSharp.Interpreter.dll 放入 Assets/Plugins。
若需支持 Windows Store/WP:
将 interpreter/portable-net40 下的 DLL放入 Assets/Plugins/WSA。
参考此指南配置。链接
3.IL2CPP项目:
在 Assets 目录下创建/编辑 link.xml,内容如下:
<linker>
<assembly fullname="MoonSharp.Interpreter">
<type fullname="MoonSharp.*" preserve="all" />
</assembly>
</linker>
步骤2:引入命名空间
在代码顶部添加:
C#
using MoonSharp.Interpreter;
VB.NET
Imports MoonSharp.Interpreter
步骤3:调用脚本
以下示例演示如何用 MoonSharp 计算阶乘:
C#
double MoonSharpFactorial()
{
string script = @"
-- defines a factorial function
function fact (n)
if (n == 0) then
return 1
else
return n*fact(n - 1)
end
end
return fact(5)";
DynValue res = Script.RunString(script);
return res.Number;
}
VB.NET
Function MoonSharpFactorial() As Double
' VB.NET is not very strong at embedded newlines...
Dim scriptCode As String = "-- defines a factorial function" & vbCrLf &
"function fact (n)" & vbCrLf & _
"if (n == 0) then" & vbCrLf & _
"return 1" & vbCrLf & _
"else" & vbCrLf & _
"return n*fact(n - 1)" & vbCrLf & _
"end" & vbCrLf & _
"end" & vbCrLf & _
"return fact(5)" & vbCrLf
Dim res As DynValue = Script.RunString(scriptCode)
Return res.Number
End Function
步骤4:运行代码
在代码中调用MoonSharpFactorial()即可执行脚本。现在,你可以继续探索其他教程了!
2.Keeping a Script around(保留一个脚本)
言语易逝,文字永存。
文档地址:MoonSharp
在之前的教程中,你首次接触了 MoonSharp:将脚本放入字符串中运行并获取输出。虽然偶尔有用,但大多数实际用例需要的互操作性需要 CLR 代码与 MoonSharp 更深度的集成。
步骤1:复现前教程所有操作
认真地说,虽然我们在这里学习的是(稍微)更高级的概念,但你最初的尝试几乎无需改动。这正是一个极好的起点。
步骤2:改为创建Script对象
首先要做的更改是创建一个脚本对象,而不是使用静态方法之一。这并非什么难事,但它为我们接下来的发展奠定了基础。
double MoonSharpFactorial()
{
string scriptCode = @"
-- defines a factorial function
function fact (n)
if (n == 0) then
return 1
else
return n*fact(n - 1)
end
end
return fact(5)";
Script script = new Script();
DynValue res = script.DoString(scriptCode);
return res.Number;
}
步骤3:访问全局环境
现在有了脚本对象,我们可以修改函数运行的全局环境。例如,改变阶乘函数的输入参数,让程序能指定计算目标数值的阶乘。
double MoonSharpFactorial()
{
string scriptCode = @"
-- defines a factorial function
function fact (n)
if (n == 0) then
return 1
else
return n*fact(n - 1)
end
end
return fact(mynumber)";
Script script = new Script();
script.Globals["mynumber"] = 7;
DynValue res = script.DoString(scriptCode);
return res.Number;
}
通过简单的 script.Globals 表引用语法,我们实现了向 MoonSharp 脚本注入数值。实际上不仅能传递数字,后续还将演示如何传递函数和对象,但现阶段我们暂限于数字、布尔值和字符串。
步骤4:直接调用函数
我们学习了如何让 MoonSharp 计算从外部选择的数字的阶乘。但是,以这种方式完成,感觉像是一个肮脏的黑客行为(尽管如此,这是一项重要的技术,我们将经常使用)。
下面是如何从C#调用Lua函数。
double MoonSharpFactorial2()
{
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;
}
我们来看看