HTTP API
您可以使用原始 HTTP API 来管理云代码脚本。
使用 API
这两个 API 文档都包含有关身份验证、端点以及请求和响应格式的信息,以及示例。您可以 OpenAPI 格式下载它们,并使用它们来设置您自己的自动化。
要使用管理员 API,请使用服务帐户进行身份验证。
授权标头
要对请求进行身份验证,请使用基本身份验证。创建一个服务帐户,并对 <KEY_ID>:<SECRET_KEY>
进行 base64 编码,并将其用作 Authorization
标头的值。
--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>' \
部署脚本
您可以通过发送以下请求来创建脚本
curl 'https://services.api.unity.com/cloud-code/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/scripts' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>' \
--data '{
"name": "test-script",
"type": "API",
"code": "module.exports = () => {return \"Result from standalone API script\";}",
"language": "JS"
}'
要使游戏客户端可以使用该脚本,您必须发布它
curl --request POST 'https://services.api.unity.com/cloud-code/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/scripts/<SCRIPT_NAME>/publish' \
--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>'
每次发布时,脚本版本都会递增。成功的响应可能如下所示
{
"version": 1,
"datePublished": "2023-06-23T10:39:20Z"
}
您无法发布自上次发布以来没有任何更改的脚本。
更新脚本
您可以通过发送以下请求来更新脚本代码、参数或两者
curl --request PATCH 'https://services.api.unity.com/cloud-code/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/scripts/<SCRIPT_NAME>' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>' \
--data '{
"params": [
{
"name": "parameter",
"type": "STRING",
"required": false
}
],
"code": "module.exports = () => {return \"Updated result from standalone API script\";}"
}'
要使游戏客户端可以使用更新后的脚本版本,您必须再次发布它,从而创建版本 2。
{
"version": 2,
"datePublished": "2023-06-23T10:40:00Z"
}
回滚脚本
如果您发布了一个脚本,并且对新版本不满意,则可以通过发送带有指定版本号的请求回滚到以前的版本
curl --request POST 'https://services.api.unity.com/cloud-code/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/scripts/<SCRIPT_NAME>/publish' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>'
--data '{
"version": 1
}'
现在,第一个脚本版本将作为版本 3 重新发布
{
"version": 3,
"datePublished": "2023-06-23T10:50:00Z"
}
检索脚本
要检查脚本的内容,您可以发送以下请求
curl 'https://services.api.unity.com/cloud-code/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/scripts/<SCRIPT_NAME>' \
--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>'
响应可能如下所示
{
"name": "test",
"type": "API",
"language": "JS",
"activeScript": {
"code": "module.exports=async (cloudCode) =>{cloudCode.logger.info('this message confirms that the logging client is functional!'); return cloudCode.params.someThing;}",
"params": [
{
"name": "someThing",
"type": "STRING",
"required": true
}
],
"version": 2,
"datePublished": "2021-07-23T16:08:35Z"
},
"versions": [
{
"code": "module.exports=async (cloudCode) =>{cloudCode.logger.info('this message confirms that the logging client is functional!'); return cloudCode.params.someThing;}",
"params": [
{
"name": "someThing",
"type": "STRING",
"required": true
}
],
"isDraft": true,
"version": null,
"dateUpdated": "2021-07-23T15:59:32Z"
},
{
"code": "module.exports=async (cloudCode) =>{cloudCode.logger.info('this message confirms that the logging client is functional!'); return cloudCode.params.someThing;}",
"params": [
{
"name": "someThing",
"type": "STRING",
"required": true
}
],
"isDraft": false,
"version": 2,
"dateUpdated": "2021-07-23T16:08:35Z",
"dateCreated": "2021-07-23T16:08:35Z"
},
{
"code": "module.exports=async (cloudCode) =>{cloudCode.logger.info('this message confirms that the logging client is functional!'); return cloudCode.params.someThing;}",
"params": [
{
"name": "someThing",
"type": "STRING",
"required": true
}
],
"isDraft": false,
"version": 1,
"dateUpdated": "2021-07-23T15:59:58Z",
"dateCreated": "2021-07-23T15:59:58Z"
}
],
"params": [
{
"name": "someThing",
"type": "STRING",
"required": true
}
]
}
响应包含以下信息
name
:脚本的名称。type
:脚本的类型。云代码脚本仅支持 API 脚本。language
:脚本的语言。云代码脚本仅支持 JavaScript。
activeScript
指的是当前发布的脚本版本,游戏客户端可以使用该版本。这包括其代码和参数、版本号以及发布日期。
versions
数组包含脚本的所有版本,包括当前发布的版本、工作副本以及任何以前发布的版本。此工作副本是一个脚本版本,其中包含最新的更改,但游戏客户端尚无法使用。对于此版本,属性 isDraft
设置为 true
,并且其 version
为 null。下次发送发布请求时,将发布工作副本。
要查看您创建的所有脚本,您可以发送以下请求
curl 'https://services.api.unity.com/cloud-code/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/scripts' \
--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>'
列表响应可能如下所示
{
"results": [
{
"name": "script",
"language": "JS",
"type": "API",
"published": true,
"lastPublishedDate": "2022-03-08T12:37:48Z",
"lastPublishedVersion": 40
},
{
"name": "test",
"language": "JS",
"type": "API",
"published": true,
"lastPublishedDate": "2023-06-20T15:14:58Z",
"lastPublishedVersion": 4
}
],
"links": {
"next": null
}
}
响应包含您创建的所有脚本的快速概览。要使用分页,您可以使用查询参数来缩小结果范围并获取指向下一页结果的链接。
curl 'https://services.api.unity.com/cloud-code/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/scripts?limit=2' \
--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>'
下一个链接可能如下所示
{
"links": {
"next": "/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/scripts?after=<SCRIPT_NAME>"
}
}
删除脚本
要删除脚本,您可以发送以下请求
curl --request DELETE 'https://services.api.unity.com/cloud-code/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/scripts/<SCRIPT_NAME>' \
--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>'
空响应表示已成功删除。