magicblock-labs / Jint-Unity

MIT License
5 stars 0 forks source link

Jint Unity

Jint Unity provides an easy way to install Jint trough the package manager and execute javascript in Unity

➕ Installation

Usage

Example 1

var square = new Engine()
    .SetValue("x", 3) // define a new variable
    .Evaluate("x * x") // evaluate a statement
    .ToObject();
Debug.Log(square);

Example 2

Engine js = new Engine();
js.SetValue("Console",typeof(Debug));
js.Execute("function Compute() { const a = 3; const res = a * a; Console.Log('Js computation: ' + res); }");
js.Invoke("Compute");

Example 3

Loading a script:

var source = new StreamReader(Application.dataPath + "/Resources/" + "main.js");
string script = source.ReadToEnd();
source.Close();
js.Execute(script);

Dependencies

Script with dependencies can be bundled using webpack or rollup, see this tutorial for reference.