Overview of MoonSharp for Unity
mastersrc/Unity directory serves as a container for all Unity-related MoonSharp assets. Currently, it primarily functions as a testbed project used to run the MoonSharp test suite within the Unity environment.repository·master·Indexed 23 days ago
https://github.com/moonsharp-devs/moonsharpA complete Lua 5.2 compatible interpreter written entirely in C# for .NET, Mono, Xamarin, and Unity3D. It features high-performance interop with CLR objects, AOT compatibility for iOS and Android, and support for coroutines and async methods. The library includes a VS Code debugger (moonsharp-debug), a REPL/CLI environment with a command system for script execution and type registration, and tools for bytecode compilation and hardwiring Lua dump tables into C# or VB code.
src/Unity directory serves as a container for all Unity-related MoonSharp assets. Currently, it primarily functions as a testbed project used to run the MoonSharp test suite within the Unity environment.MoonSharp is a complete Lua solution written in C# for .NET, Mono, Xamarin, and Unity3D. It is approximately 99% compatible with Lua 5.2 (excluding weak tables).
Key capabilities include:
debug and string modules), coroutines (invokable as C# iterators), and async method support.If you are developing the extension, you can build and package it using npm with the following commands:
npm install
npm run compile
npm run package:vsixThe MoonSharp VSCode Debugger extension allows you to debug MoonSharp scripts running inside other applications.
Requirements:
Supported Features:
self inspection.Limitations:
To use the debugger, you must create a launch.json file in your VSCode project. You must specify the debugServer port immediately after the version field. This port must match the port the host application is using to expose the debugger.
{
"version": "0.2.0",
"debugServer" : 41912,
"configurations": [
{
"name": "MoonSharp Attach",
"type": "moonsharp-debug",
"request": "attach",
"HELP": "Please set 'debugServer':41912 (or whatever port you ar connecting to) right after the 'version' field in this json."
}
]
}You can install MoonSharp directly into your Unity project by adding it to your Packages/manifest.json file.
Add the following entry to your dependencies object:
"org.moonsharp.moonsharp": "https://github.com/moonsharp-devs/moonsharp.git?path=/interpreter#upm/v3.0"
To pin to a major version (e.g., version 3) instead of a specific minor version, use the branch upm/v3.
The VSCode debugger is a separate package. Add it to manifest.json using:
"org.moonsharp.debugger.vscode": "https://github.com/moonsharp-devs/moonsharp.git?path=/debugger/vscode#upm/v3.0"
Beta branches follow the naming convention upm/beta/v3.0.
To create a local tarball of the MoonSharp Unity Package, run the following commands from the repository root:
npm pack to generate the .tgz file.Note: Replace 3.0.0-local with your desired version string.
tools/upm/stage-local-package.sh 3.0.0-local
cd .upm-staging/org.moonsharp.moonsharp
npm packTo execute Lua code within a C# application, use the Script.RunString method. The result is returned as a DynValue, which can then be converted to native C# types (such as double, int, or string).
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;
}!). This allows you to interact with the environment without exiting the interpreter loop.The CommandManager class is responsible for managing the lifecycle and execution of commands within the MoonSharp REPL or CLI environment. It uses an internal registry of objects implementing the ICommand interface.
To use the command system, you must first call Initialize() to scan the executing assembly and register all available non-abstract classes that implement ICommand. Once initialized, you can execute command strings via Execute or retrieve available commands using GetCommands or Find.