ScriptEngine
The ScriptEngine
component is the core of OneJS. It manages interop between C# and JS (via Jint) and provides the DOM implementations needed by Preact.
INTEROP
Settings under INTEROP are generally about what features from C#/.Net you want to expose to Javascript. Whenever you start having trouble accessing .Net stuff from JS, here is the place you should check first.
- Objects: Maps an Unity Object to a js module name (any string that you choose). Objects declared here will be accessible from Javascript via
require("objname")
. The Objects list accepts any UnityEngine.Object, not just MonoBehaviours. To pick a specific MonoBehaviour component, you can right-click on the Inspector Tab of the selected GameObject and pick Properties. A standalone window will pop up for you to drag the specifc MonoBehavior from. - Assemblies: List of Assembly names you want to access from Javascript. (i.e.
"UnityEngine.CoreModule"
and"Unity.Mathematics"
) - Namespaces: You can map C# namespaces to JS module here. (i.e.
"UnityEngine.UIElements" => "UnityEngine/UIElements"
) - Extensions: List of Extension names you want to access from Javascript. (i.e.
"UnityEngine.UIElements.PointerCaptureHelper"
) - Static Classes: Map C# static classes to JS module. (i.e.
"Unity.Mathematics.math" => "math"
)
SECURITY
ScriptEngine
provides the following security settings for you to set in the Inspector.
- Catch .Net Exceptions
- Allow Reflection
- Allow
GetType()
- Memory Limit
- Timeout
- Recursion Depth
These are some of the security settings exposed directly from Jint. To set more granular security measures such as Member Accessor & TypeResolver, you can do so during the OnPostInit
event (refer to the event API below).
STYLING
You can add additional USS Style Sheets here and also tweak screen breakpoints.
MISC
Here, you can set the OneJS WorkingDir paths for Editor and runtime. The Bundler will use this info for automatic bundling and extraction. The only thing you need to make sure is that the 2 paths are not the same.
Also under MISC, you can set what scripts you want to load before and after every engine reload.
ScriptEngine APIs
ScriptEngine
exposes some public APIs for you to use from code.
Properties
public string WorkingDir; // The OneJS Working Directory
public Engine JintEngine; // Internal Jint Engine
public Document Document; // Document object (`document` in js)
public Dom DocumentBody; // document.body
public DateTime StartTime // Engine Start time. Reset on every reload.
Events
public event Action OnPostInit; // Happens after every ScriptEngine reload
public event Action OnReload; // Happens when ScriptEngine is just about to reload
Methods
public void RunScript(string scriptPath); // Run a script as is
public void ReloadAndRunScript(string scriptPath); // Reloads the ScriptEngine and then run a script
public void RegisterReloadHandler(Action handler); // Sub to the OnReload event and will automatically unsub during reload. Useful for the Live Reload workflow
public void AddRuntimeObject(string module, object obj); // For adding and maintaining runtime interop objects
public void AddRuntimeNamespace(string name, string module); // For adding and maintaining runtime namespace mappings
public void AddRuntimeStaticClass(string name, string module); // For adding and maintaining runtime statics mapping