Integrate with Unity Editor
Author and modify Cloud Code scripts directly within the Unity Editor using the Cloud Code Authoring module.
Read time 6 minutesLast updated a day ago
The Cloud Code Authoring module (installed with the Cloud Code package) allows you to optionally author and modify scripts directly within the Unity Editor. You can then upload scripts from the Unity Editor to the Unity Dashboard with the Deployment package.
Scripts existing in the Unity Editor allow users to treat their source control as the single source of truth (instead of the version in the cloud), simplifying actions such as rollbacks, bisection, and other common operations. For example, the Cloud Code Authoring module facilitates tasks like keeping client C# scripts in sync with Cloud Code scripts.
Prerequisites
To use Cloud Code in the Unity Editor, you must first install the Cloud Code SDK and link your Unity Gaming Services project to the Unity Editor.Link project
Link your Unity Gaming Services project with the Unity Editor. You can find your UGS project ID in the Unity Dashboard.- In Unity Editor, select Edit > Project Settings > Services.
-
Link your project.
-
If your project doesn't have a Unity project ID:
- Select Create a Unity Project ID > Organizations, then select an organization from the dropdown menu.
- Select Create project ID.
-
If you have an existing Unity project ID:
- Select Use an existing Unity project ID.
- Select an organization and a project from the dropdown menus.
- Select Link project ID.
-
If your project doesn't have a Unity project ID:
UnityEditor.CloudProjectSettings.projectIdInstall required packages
To create Cloud Code scripts within the Editor, you must install the following packages:- Deployment
- Cloud Code (2.1.1 or greater)
To install these packages and add them to your list of available packages:
- From the Unity Editor’s Package Manager window, select + (add) > Add package by name….
- Enter .
com.unity.services.deployment - Select Add.
- Repeat these steps for .
com.unity.services.cloudcode
Authoring within Unity Editor
The Cloud Code Authoring module allows you to create, edit, and deploy Cloud Code scripts directly within the Unity Editor.Create a script
Follow these steps to create a Cloud Code script using the Cloud Code Authoring module:- In the Unity Editor, right-click in the Project window, then select Create > Services > Cloud Code Js Script.
- Name the script as you would a C# script. Cloud Code scripts use their file name as the identifier when uploading to the service.
- Press Enter.
The new script is now visible in the Project window, and in the Deployment window, accessible by selecting Window > Deployment.
Edit a script
There are two methods to edit an existing Cloud Code Authoring script:- In the Project tab, double-click the existing script.
- In the Deployment window, find the existing script then, from the right-click menu, select Open.
- In the Unity Editor, select Edit > Preferences… > Cloud Code.
- In the Javascript Editor section, choose the application path for your preferred JS editor.
- Select Apply.
- : File path of the asset
$(File) - : Project directory
$(ProjectPath) - : Solution path
$(SolutionPath) - : Editor executable path (chosen within the Unity Editor by selecting Edit > Preferences… > External Tools)
$(EditorExePath)
-
JetBrains Rider (on Windows):
- Application:
cmd.exe - Args:
/C "$(EditorExePath) $(ProjectPath) $(File)"
- Application:
-
Microsoft Visual Studio Code:
- Application:
Code.exe - Args:
$(ProjectPath) $(File)"
- Application:
Deploy a script
You can deploy a script through the Deployment window. Check the Deployment package manual for more information.Modify script parameters
You can access and modify Cloud Code scripts parameters in the Unity Editor.Access parameters in the Editor’s Inspector window
You can modify a Cloud Code script’s parameters by selecting the script in the Unity Editor’s Project window. The script’s parameters open in the Editor’s Inspector window.
The possible parameter types are:
- String
- Boolean
- Numeric
- JSON
- Any
Declare parameters in the script body
For a more seamless experience in your Cloud Code scripts, you can declare your parameters directly in the script by exporting theparams
For example:
JavaScript
Alternatively, if you'd like to specify that a parameter is required, you may specify an object containing both themodule.exports.params = { echo: "Boolean" };
typerequiredBy default, parameters are not required. Both formats can be combined as desired: JavaScriptmodule.exports = async ({ params, context, logger }) => { return { value: params["aParam"], };};module.exports.params = { aParam: { type: "String", required: true } };
If you are using in-script parameters, they override previously defined inspector parameters. In-script parameters offer a simpler alternative to declaring parameters, but the Unity Dashboard does not currently parse in-script parameters if you try to update them without using the Deployment package.module.exports = async ({ params, context, logger }) => { var value = params["echo"] ? params["aParam"] : "default"; return { value: value, };};module.exports.params = { echo: "Boolean", aParam: { type: "String", required: true },};
JS bundles
Your JS scripts can import local scripts or external modules with the
importrequirebundling
JavaScript
The following example demonstrates what bundling could look like in a sample project. lib.jsmodule.exports.bundling = true;
scriptToBundle.jsmodule.exports.helloWorld = "Hello world!";
const lib = require("./lib");module.exports = async ({ params, context, logger }) => { return { value: lib.helloWorld, };};module.exports.bundling = true;
Deployment window
The Deployment window is a core feature of the Deployment package. It allows all services to have a single cohesive interface for deployment needs, and allows you to upload cloud assets to their respective cloud services. Check the Deployment package manual for more information.Cloud Code JavaScript projects in the Unity Editor
A Cloud Code JavaScript project in the Unity Editor sits at the root of the Unity project and has the following structure: ├──Assets│ └──
CloudCode│ └──
cloud_script.js├──
node_modules├──
package.json└──
package-lock.jsonpackage.jsonpackage.jsonpackage-lock.jsonNodeJS
To support more tooling for developers, developers must install NodeJS. NodeJS allows support for autocomplete and other expected features of a development environment.Autocomplete
The majority of autocomplete for JS is handled by the typings project. You can augment any JavaScript file by using a.d.ts.d.tsnode_modulespackage.jsonnode_modulesExternal tools
You can add external tools by running the appropriatenpm install ...ESLint
-
Run . This command adds ESLint to the project dependencies.
npm i -D eslint -
Create an file at the project root containing:\ JSON
.eslint.json{ "env": { "commonjs": true, "es2021": true }, "extends": "eslint:recommended", "parserOptions": { "ecmaVersion": 13 }, "rules": { }} - Configure your IDE to run ESLint. This is automatic for Visual Studio Code but might need additional settings depending on the IDE.
- ESLint should now be configured and working.
eslint:recommendedif (something == -0)Jest
Jest is a framework for JavaScript testing. The following is an example of how you can create unit tests for your Cloud Code scripts.-
Run . This command installs the Jest package.
npm install --save-dev jest -
Configure Jest to run with scripts.
.es10 -
Add the following code snippet to your :\ JSON
package.json"jest": { "moduleFileExtensions": ["es10", "js"], "testMatch": ["**/*.test.es10"]}
moduleFileExtensionsjses10testMatch.test.es10Unit testing example
The following JS unit testing example returns an object then verifies whether the result in the test script is the same. More advanced options are available with mocking.The script to be tested
Cloud Code expects the following signature: JavaScriptmodule.exports = async ({ params, context, logger }) => { return { value: "1" };};
The test script
JavaScriptconst test = require("./jest_01.es10");it("test", async () => { expect(await test({})).toMatchObject({ value: "1" });});
Running your tests
Run your tests in the command line with the following command:npm run test