Open6
PlayFabでCloudScriptを使ってみる

CloudScriptとは
サーバー側で機能を実装して、クライアントから呼び出すことができる。
これで実装の幅が広がる。
このやり方↓もあるが、ゲームマネージャー上でLegacyになっていたのでAzureFunctionsを使ったやり方で進める
Azure Functions

Azureのセットアップ
- Azureアカウントの作成
- サブスクリプションの作成

ローカル環境で関数を実行してみる
- コマンドパレットでAzure Functions: Create New Projectを選択
- そのままコマンドパレットで色々聞かれるので、以下のように設定(PlayFabでは承認レベルはFunction推奨)
- そのままF5で実行
- エラー発生
func : このシステムではスクリプトの実行が無効になっているため、ファイル C:
Users\takas\AppData\Roaming\npm\func.ps1 を読み込むことができません。詳細に
ついては、「about_Execution_Policies」(https://go.microsoft.com/fwlink/?Lin
kID=135170) を参照してください。
発生場所 行:1 文字:1
func host start
CategoryInfo : セキュリティ エラー: (: ) []、PSSecurityException
FullyQualifiedErrorId : UnauthorizedAccess - 管理者権限でPowerShellを開いて以下を実行(ChatGPTに聞いて出てきた解決策)
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
- エラー発生
- 左のAzureマークを押して、左下のWORKSPACEウィンドウを見る
- Local Project/Functions/HttpExampleを右クリックしてExecute Function Nowを選択
- 通知に出力されたら成功

関数アプリを作成
- まずRESOURCESでサインインをする
- コマンドパレットでCreate Function App in Azureを選択
- コマンドパレットで色々聞かれるので、順番に選択していく
- サブスクリプションはさっき作ったもの
- PlayFabで使う場合はリージョンはWestUSにするといい
- ランタイムは.NET8 Isolated
- Resource authentication typeはよくわからないけど、Managed identityにするとエラーを吐いたのでので、とりあえずSecretsを選択
- Azure Functions: Deploy to function appで作ったアプリを選択してデプロイ
- Azure Functions:Execute Function NowでHttpExampleを選択して実行
- 通知に出力されたら成功

PlayFabに関数を登録
- ここからURLをコピーしておく
- PlayFabのゲームマネージャーでAutomation->Functions->Register functionを選択
- 関数を設定する
- Trigger typeはHTTP
- Function nameは好きなもの(呼び出すときに指定する)
- Function URLにはさっきのURLをペースト
- Unity側でログイン後に以下のメソッドを呼び出す
private void CallCSharpExecuteFunction()
{
PlayFabCloudScriptAPI.ExecuteFunction(new ExecuteFunctionRequest()
{
Entity = new PlayFab.CloudScriptModels.EntityKey()
{
Id = PlayFabSettings.staticPlayer.EntityId, //Get this from when you logged in,
Type = PlayFabSettings.staticPlayer.EntityType, //Get this from when you logged in
},
FunctionName = "HelloWorld", //This should be the name of your Azure Function that you created.
FunctionParameter = new Dictionary<string, object>() { { "inputValue", "Test" } }, //This is the data that you would want to pass into your function.
GeneratePlayStreamEvent = false //Set this to true if you would like this call to show up in PlayStream
}, (ExecuteFunctionResult result) =>
{
if (result.FunctionResultTooLarge ?? false)
{
Debug.Log("This can happen if you exceed the limit that can be returned from an Azure Function, See PlayFab Limits Page for details.");
return;
}
Debug.Log($"The {result.FunctionName} function took {result.ExecutionTimeMilliseconds} to complete");
Debug.Log($"Result: {result.FunctionResult.ToString()}");
}, (PlayFabError error) =>
{
Debug.Log($"Opps Something went wrong: {error.GenerateErrorReport()}");
});
}
- "Result: Welcome to Azure Functions!"とログに表示されれば成功

PlayFabの機能を使うには
- ターミナルで
dotnet add package PlayFabAllSDK
と入力してSDKをインポートする