> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 使用 Azure Functions 的 Cloudscript 本地调试

> 通过添加 ExecuteFunction 实现并从 Unity、Unreal 或 C# 路由 SDK 调用,在本地使用 Azure Functions 运行和调试 PlayFab CloudScript。

# 教程:使用 Azure Functions 的 Cloudscript 本地调试

借助 Azure Functions,你现在可以在本地测试和调试你的 CloudScript 代码。完成本教程后,你可以在调试器(例如在 VS Code 或 Visual Studio)下运行你的本地 Azure Functions 应用,设置断点并运行你的游戏客户端。

在本教程中,你将学习如何:

<Note>
  \[!div class="checklist"] \* 将 ExecuteFunction 的实现添加到你的本地 Azure Functions 应用 \* 添加一个设置文件,告知 PlayFab SDK 从你的游戏中调用该本地实现
</Note>

## 先决条件

* 目前以下 SDK 支持本地调试:
  * [PlayFab C# SDK](https://github.com/PlayFab/CSharpSDK)
  * [PlayFab Unity SDK](https://github.com/PlayFab/UnitySDK)
  * [PlayFab 的 Unreal 4 Marketplace 插件](https://github.com/PlayFab/UnrealMarketplacePlugin)

## 添加 ExecuteFunction 的本地实现

### 对于 C# Azure Functions 应用

若要在你的 C# Azure Functions 应用中设置 ExecuteFunction 的本地实现,请将 [ExecuteFunction.cs](https://github.com/PlayFab/pf-af-devfuncs/blob/master/csharp/ExecuteFunction.cs) 文件添加到你的本地 Azure Functions 应用中。

### 本地 ExecuteFunction 实现所需的环境变量

将以下设置添加到你的 `local.settings.json` 文件中:

| 名称                        | 值                                                                                        |
| ------------------------- | ---------------------------------------------------------------------------------------- |
| PLAYFAB\_TITLE\_ID        | 你的游戏 ID(十六进制形式)                                                                          |
| PLAYFAB\_DEV\_SECRET\_KEY | 你游戏的密钥。你可以在 Game Manager 中通过点击游戏名称右侧的齿轮图标并转到 **Title Settings** > **Secret Keys** 来找到密钥。 |

例如:

```JSON theme={null}
{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "...",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "PLAYFAB_TITLE_ID": "B55D",
    "PLAYFAB_DEV_SECRET_KEY": "AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHHIIIIJJJJKKKKLLLLMM"
  }
}
```

## 配置 PlayFab SDK 以调用本地 ExecuteFunction 实现

若要告知 PlayFab SDK 将 ExecuteFunction API 调用重定向到你的本地实现,请将一个名为 `playfab.local.settings.json` 的文件添加到以下两个位置之一:

* 你机器上的临时目录
  * 在 Windows 上,这是 **TEMP** 环境变量
  * 在 Linux/Mac 上,这是 **TMPDIR** 环境变量
* 你的游戏可执行文件所在的目录

将文件内容设置为如下所示:

```JSON theme={null}
{ "LocalApiServer": "http://localhost:<portNumber>/api/<functionName>" }
```

如果你希望停止本地重定向,让 ExecuteFunction 调用 PlayFab API 服务器,请删除 `playfab.local.settings.json` 文件。

## 其他资源

Azure Functions 有一份关于如何在本地[测试和调试你的 Functions](https://learn.microsoft.com/en-us/azure/azure-functions/functions-develop-local) 的优秀指南。

以下是上述文档中的一些要点。

* 确保你已安装 Azure Functions Core Tools
* 配置你的本地设置文件。有关更多信息,请参阅[使用 Visual Studio Code 开发 Azure Functions](https://learn.microsoft.com/en-us/azure/azure-functions/functions-develop-vs-code?tabs=nodejs#local-settings-file)
* 在你的代码中设置断点
* 选择 **F5** 开始调试
