调用 Cloud Code API
调用 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>/modules/<MODULE_NAME>/<FUNCTION_NAME>
定义路径参数:
PROJECT_ID
- 项目的 ID。MODULE_NAME
- 模块的名称。FUNCTION_NAME
- 模块终端的名称。这是您在代码中定义的CloudCodeFunction
。
一个简单的 cURL 命令如下所示:
curl --request POST 'https://cloud-code.services.api.unity.com/v1/projects/<PROJECT_ID>/modules/<MODULE_NAME>/<FUNCTION_NAME>' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <BEARER_TOKEN>'
请求有效负载
您可以在模块函数的方法签名中定义参数。如需有关如何构建模块函数的信息,请参阅模块结构。例如,以下方法定义了字符串参数 weapon 和 bodyPart:
[CloudCodeFunction("IntroduceGuard")]
public string IntroduceGuard(string weapon, string bodyPart)
{
return $"I used to be an adventurer like you. Then I took an ${weapon} to the ${bodyPart}.";
}
调用终端时,这些参数将作为 JSON 传入请求正文中。参数的大小写必须与方法签名匹配。
{
"params": {
"weapon": "arrow",
"bodyPart": "knee"
}
}
示例输出
以下是一个模块的示例输出。
{
"output": "I used to be an adventurer like you. Then I took an arrow to the knee."
}