调用 Cloud Code API
Invoke module endpoints using HTTP requests to the Cloud Code REST API.
阅读时间2 分钟最后更新于 1 个月前
调用 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>
- - 项目的 ID。
PROJECT_ID - - 模块的名称。
MODULE_NAME - - 模块终端的名称。这是您在代码中定义的
FUNCTION_NAME。CloudCodeFunction
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:调用终端时,这些参数将作为 JSON 传入请求正文中。参数的大小写必须与方法签名匹配。[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}.";}
一个完整的 cURL 命令如下所示:{ "params": { "weapon": "arrow", "bodyPart": "knee" }}
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>' \--data-raw '{ "params": { "weapon": "arrow", "bodyPart": "knee" }}'
示例输出
以下是一个模块的示例输出。{ "output": "I used to be an adventurer like you. Then I took an arrow to the knee."}