ScriptEngine component is the core of OneJS. It manages interop between C# and JS (via Jint) and provides the DOM implementations needed by Preact. Below are the settings you can set on the component, divided into 3 categories: Interop, Assets, and Security.
- Namespaces: You can map C# namespaces to JS module here. (i.e.
"UnityEngine.UIElements" => "UnityEngine/UIElements")
- Static Classes: Map C# static classes to JS module. (i.e.
"Unity.Mathematics.math" => "math")
- Objects: Map a MonoBehaviour component to JS module. Note you can drag a Component to the slot, but you can probably only do so by locking the
ScriptEngineInspector and opening a new Inspector Tab for the target MonoBahaviour. (i.e.
"MaterialManager" => "matman")
The settings under Assets mostly contain templates or default files for OneJS to use, such as
settings.json. So You don't really need to touch any of that. The only thing you may want to add are the Style Sheets (USS).
ScriptEngine provides the following security settings for you to set in the Inspector.
- Allow Reflection
- Memory Limit
- 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).
ScriptEngine exposes some public APIs for you to use from code.
public Engine JintEngine; // Internal Jint Engine public Dom DocumentBody; // Dom for document.body
public event Action OnPostInit; // Happens after every ScriptEngine reload public event Action OnReload; // Happens when ScriptEngine is just about to reload
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