ドキュメント

サポート

Cloud Code

Cloud Code JavaScript ファイル

Manage your Cloud Code scripts from the Unity Editor using script files and the Deployment package.
読み終わるまでの所要時間 1 分最終更新 23日前

Cloud Code JavaScript ファイルを使用して、Unity エディターでスクリプトを管理します。これらのスクリプトは、Deployment パッケージ を使用して Unity Cloud Dashboard にデプロイできます。

前提条件

Cloud Code スクリプトを使用するには、Deployment パッケージ (
com.unity.services.deployment
) をインストールする必要があります。

Cloud Code スクリプトファイルの作成

Cloud Code スクリプトファイルを作成するには、以下を行います。
  1. Unity エディターで、Project (プロジェクト) ウィンドウを右クリックし、Create (作成) > Services (サービス) > Cloud Code Js Script (Cloud Code Js スクリプト) を選択します。

ファイル形式

ファイル形式は .js 標準に従います。

/* * -------- Example Cloud Code Script -------- * * Roll a 6-sided dice and get back the result * * -------------------------------------------- * Additional imports are limited to: * - lodash-4.17 * - axios-0.21 * - @unity-services/cloud-save-1.0 * - @unity-services/economy-2.0 * - @unity-services/remote-config-1.0 */ // Include the lodash module for convenient functions such as "random"const _ = require("lodash-4.17");const NUMBER_OF_SIDES = 6;/* * CommonJS wrapper for the script. It receives a single argument, which can be destructured into: * - params: Object containing the parameters provided to the script, accessible as object properties * - context: Object containing the projectId, environmentId, environmentName, playerId and accessToken properties. * - logger: Logging client for the script. Provides debug(), info(), warning() and error() log levels. */module.exports = async ({ params, context, logger }) => { // Log an info message with the parameters provided to the script and the invocation context logger.info("Script parameters: " + JSON.stringify(params)); logger.info("Authenticated within the following context: " + JSON.stringify(context)); const roll = rollDice(NUMBER_OF_SIDES); if (roll > NUMBER_OF_SIDES) { // Log an error message with information about the exception logger.error("The roll is greater than the number of sides: " + roll); // Return an error back to the client throw Error("Unable to roll dice!"); } // Return the JSON result to the client return { sides: NUMBER_OF_SIDES, roll: roll, };};// Functions can exist outside of the script wrapperfunction rollDice(sides) { return _.random(1, sides);}
その他のオーサリングオプションについては、エディターでのスクリプトの記述 を参照してください。

ファイルのデプロイ

Cloud Code スクリプトファイルを作成したら、選択した環境にデプロイできます。 ファイルをデプロイするには、以下を行います。
  1. Unity エディターで、Services (サービス) > Deployment (デプロイ) を選択します。Unity のバージョンが 2022 より前の場合は、Window (ウィンドウ) > Deployment (デプロイ) を選択します。Deployment (デプロイ) ウィンドウに、ローカルの Cloud Code スクリプトファイルが表示されます。
  2. 選択した Cloud Code スクリプトファイルを選択し、Deploy Selected (選択したアイテムのデプロイ) を選択します。
これで、Cloud Code スクリプトを Unity Cloud Dashboard で編集できます。 Deployment (デプロイ) ウィンドウの操作について詳しくは、Deployment パッケージ を参照してください。