文档

Cloud Code

调用 Cloud Code API

Invoke scripts by making HTTP requests to the Cloud Code REST API.
阅读时间2 分钟最后更新于 10 个月前

调用 Cloud Code API 来运行脚本。请参阅 Cloud Code API 文档了解更多信息。

身份验证

请参阅身份验证,使用服务帐户或无状态令牌以玩家或受信任客户端的身份进行身份验证。请在请求标头中使用收到的令牌作为 HTTP 身份验证的持有者令牌:
Authorization: Bearer <BEARER_TOKEN>

运行脚本请求

通过向以下地址发送 POST 请求来调用 Cloud Code API:
https://cloud-code.services.api.unity.com/v1/projects/<PROJECT_ID>/scripts/<SCRIPT_NAME>
定义路径参数:
  • projectId
    - 项目的 ID。
  • scriptName
    - 脚本的名称。
一个简单的 cURL 命令如下所示:
curl --request POST 'https://cloud-code.services.api.unity.com/v1/projects/<PROJECT_ID>/scripts/<SCRIPT_NAME>' \--header 'Content-Type: application/json' \--header 'Authorization: Bearer <BEARER_TOKEN>'

请求有效负载

您可以在脚本中定义参数。这些参数会传入到请求正文中。如果脚本没有参数,则可以传入空的
params
对象。
{ "params": { "numericParam": 123, "stringParam": "abcdef", "booleanParam": true, "jsonParam": { "key": "value" } }}
一个完整的 cURL 命令如下所示:
curl --request POST 'https://cloud-code.services.api.unity.com/v1/projects/<PROJECT_ID>/scripts/<SCRIPT_NAME>' \--header 'Content-Type: application/json' \--header 'Authorization: Bearer <BEARER_TOKEN>' \--data-raw '{"params": { "numericParam": 123, "stringParam": "abcdef", "booleanParam": true, "jsonParam": { "key": "value" }}}'

示例输出

以下是返回
playerInfo
对象的脚本的示例输出。
{ "output": { "playerInfo": { "playerID": "TestUser", "userName": "TestUserName", "displayName": "Name", "email": "test@test.com", "country": null, "deviceType": null, "platform": "IOS", "dateCreated": "string" } }}