Unity project allowing to execute a JavaScript script via a Node.js instance.
It is based on the NodejsUnity project from hecomi.
To use this project, download the repository, and copy/import the "NodeJs" and "StreamingAssets" directories in the Unity project's "Assets" directory. The entire content of the 2 directories is not required.
The project is structured as follow:
- [NodeJs]
- [Editor]
- NodeJsEditor.cs
- [Examples]
- [Prefabs]
- NodeJs
- [Scenes]
- ExampleScene.unity
- [Scripts]
- CommunicationControllerImpl.cs
- NodeJsController.cs
- [Prefabs]
- [Scripts]
- CommunicationController.cs
- NodeJs.cs
- [Editor]
- [StreamingAssets]
- [.node]
- LICENSE.txt
- node.exe
- [.script]
- example_script.js
- [.node]
The core of the project is the "NodeJs" script located in the "NodeJs/Scripts" directory. It handles the initialisation and execution of the process.
The script allows to configure the different parameters and provides methods to initialise, start and stop Node.js script's execution (with respectively "Init", "Run" and "Stop").
An Editor script is provided for a more convenient configuration via the Unity Editor. The "NodeJsEditor" script is located in the "NodeJs/Editor" directory.
A Node.js executable is required by the application, it can either be included in the build generated by Untiy or else needs to be installed on the end user's machine (see "Node.js" in the "Parameters/Configuration" section below). By default, an executable is provided in the "StreamingAssets/.node" directory.
Additionally, the application will require a JavaScript script and its dependencies (Node.js modules and other used scripts or components). All will need to be in a subdirectory of the "StreamingAssets" directory (see "JS Script" in the "Parameters/Configuration" section below).
-
Use Embedded Node.js
If the setting is checked, the application will use the Node.js executable provided with the project, that will be embedded in the generated built.
If the setting is not checked, the application will look for the Node.js executable installed in the system (which will have to be present on the end user's machine and defined in the PATH environment variable). -
Use Default Path
This setting is only enabled if "Use Embedded Node.js" is checked.
If checked, the application will look for the Node.js executable in the default directory ".node" (located in "StreamingAssets"). The Node.js executable provided in the directory will be embedded in the generated built (as well as the entire directory).
If the setting is not checked, a different path can be specified. -
Node.js relative path
This setting is only enabled if "Use Default Path" is checked.
It allows to specify a different path for the Node.js executable than the default path. The provided path must be a subdirectory of the "StreamingAssets" directory.
The Node.js executable provided in the directory will be embedded in the generated built (as well as the entire directory).
-
Use Default Script Path
If the setting is checked, the application will look for the JavaScript script to execute in the default directory ".script" (located in "StreamingAssets"). The specified directory will be embedded in the generated built.
If the setting is not checked, a different path can be specified. -
Relative Path
This setting is only enabled if "Use Default Script Path" is checked.
It allows to specify a different path for the JavaScript script than the default path. The provided path must be a subdirectory of the "StreamingAssets" directory.
The specified directory will be embedded in the generated built. -
Name
The name of the JavaScript script to execute.
It must be located in the specified directory (either the default directory, or the one specified as "Relative Path"). -
Arguments
The arguments to provide to the JavaScript script.
-
Use Communication
This setting allows to provide an object in order to exchange messages with the application. -
Communication Controller
This setting is only enabled if "Use Communication" is checked.
It allows to provide an implementation of the CommunicationController class in order to exchange messages with the application.
(Currently only output messages are handled: the output and error streams from the Node.js application)
An example can be found in the "NodeJs/Examples" directory, in the provided "ExampleScene" scene.
It uses a NodeJsController component to control the execution of the process.
The example executes a basic sample JS script "example_script".
It also provides a CommunicationController implementation to allow displaying output and error messages from the Node.js running instance.
- NodejsUnity project from hecomi: The original project from which this is based on.
- Node.js: JavaScript run-time environment to execute JavaScript code outside of a browser.