文档

​
​

Development

User Acquisition

Monetization

工业

Cloud Code

Open Unity Dashboard

Cloud Code

LiveOps
​
​
Cloud Code
  • Overview
  • Get started
  • Server authority
  • Cloud Code C# modules
  • Cloud Code JavaScript scripts
    • Overview
    • Get started
    • Concepts
      • Authentication
      • Script structure
      • Script types
      • Token support
      • Error handling
      • JavaScript files
    • Tutorials
    • Reference
  • Logging
  • Use-case samples
  • Privacy and consent
  1. Cloud Code

Cloud Code JavaScript 文件

Manage your Cloud Code scripts from the Unity Editor using script files and the Deployment package.
阅读时间3 分钟
最后更新于 5 个月前

使用 Cloud Code JavaScript 文件从 Unity 编辑器管理您的脚本。您可以使用 Deployment 包将这些脚本部署到 Unity 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 标准。
警告
重要:在设置参数之前,必须分配
module.exports
属性。

示例

/* * -------- 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(部署)。对于 2022 之前的 Unity 版本,请选择 Window(窗口)> Deployment(部署)。Deployment(部署)窗口会显示您的本地 Cloud Code 脚本文件。
  2. 选择您所选的 Cloud Code 脚本文件,然后选择 Deploy Selected(部署所选项)。
您现在可以在 Unity Dashboard 中编辑您的 Cloud Code 脚本。
如需有关使用 Deployment(部署)窗口的更多信息,请参阅 Deployment 包。

Copyright © 2026 Unity Technologies
法律信息隐私政策CookiesDocumentation Terms of Use请勿出售或分享我的个人信息您的隐私选择(Cookie 设置)

“Unity”、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属公司在美国和其他地方的商标或注册商标(此处查看更多信息)。其他名称或品牌是其各自所有者的商标。

为方便起见,一些页面是机器翻译的,可能包含不准确的内容。如有信息不一致的情况,以英文版本为准。

  • 在本页上
    • 先决条件

    • 创建 Cloud Code 脚本文件

    • 文件格式

      • 示例

    • 部署文件


报告此页面的问题