Skip to content

Start a JavaScript script with Node.js from a Unity project.

Notifications You must be signed in to change notification settings

2-REC/unity-nodejs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

Unity project allowing to execute a JavaScript script via a Node.js instance.
It is based on the NodejsUnity project from hecomi.

Usage

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
    • [Scripts]
      • CommunicationController.cs
      • NodeJs.cs
  • [StreamingAssets]
    • [.node]
      • LICENSE.txt
      • node.exe
    • [.script]
      • example_script.js

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.

Node.js

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.

JS Script

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).

Parameters/Configuration

Node.js

  • 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).

JS Script

  • 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.

Communication

  • 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)

Example

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.

Resources/Links

  • 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.

About

Start a JavaScript script with Node.js from a Unity project.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published