Overview
This Azure Functions app is the server-side backend for the Tic Tac Toe Unity sample. It handles all game logic that runs on the server: validating player moves, running AI move algorithms, checking win conditions, and resetting board state. Each function is a standalone HTTP-triggered Azure Function written in C#. The game client calls them through PlayFab’sExecuteFunction API, which routes the request to the registered URL. The functions use PlayFab Player Data to persist board state between turns.
Unity Tic Tac Toe sample
The companion Unity game that calls these Azure Functions through PlayFab.
Samples overview
Browse all available PlayFab samples.
Prerequisites
- .NET Core 2+
- Azure Functions Core Tools
- VS Code or Visual Studio (VS Code recommended to follow this guide)
- An Azure subscription (required for remote deployment)
- A PlayFab title with the Title ID and Developer Secret Key on hand
Project dependencies
The.csproj includes two NuGet packages:
| Package | Purpose |
|---|---|
PlayFabAllSDK | Makes PlayFab API calls from within functions |
PlayFabCloudScriptPlugin | Provides FunctionContext<T> and helper types for deserializing the PlayFab payload |
Local setup
Open the project in VS Code
Open VS Code and navigate to the following folder in the cloned repository:
Install required extensions
Open the Extensions panel (
Ctrl+Shift+X) and install:Configure the Azure Functions extension
- Open the Azure Extension (
Ctrl+Shift+A) and sign in to Azure. - Click Create new project… in the Azure Functions extension toolbar.
- Select the
TicTacToeFunctionsfolder as your project directory. - Choose C# as the project language.
- Choose Skip for now on the Function Template dropdown.
.vscode/, host.json, local.settings.json, and the .csproj file.Configure local.settings.json
Open
local.settings.json and add your PlayFab credentials under Values:local.settings.json
Add NuGet packages
- Open the command palette (
Ctrl+Shift+P), type NuGet, and select NuGet Package Manager: Add Package. - Search for
PlayFab, selectPlayFabAllSDK, and add the latest version. - Repeat to add
PlayFabCloudScriptPlugin. - When prompted about unresolved dependencies, click Restore.
Azure deployment
Create a Function App in the Azure Portal
Go to the Azure Portal, search for Function App, and click Add. Follow the prompts to create a new Function App. You may need to create a new Subscription and Resource Group.
Set Application Settings
Once the app is created, navigate to its Overview page and click Configuration. Add two new application settings:
PLAYFAB_TITLE_ID— your PlayFab Title IDPLAYFAB_DEV_SECRET_KEY— your PlayFab Developer Secret Key
Deploy the functions
In the Azure Functions extension (
Ctrl+Shift+A), find the Function App you created, right-click it, and select Deploy To Functions App. Confirm the deployment when prompted.You can also deploy via the Azure Functions Core Tools CLI or the Azure Functions and Web Jobs Tools for Visual Studio.PlayFab setup
The PlayFab configuration steps are covered in both this guide and the Tic Tac Toe Unity sample. You only need to complete this setup once.Create a PlayFab title
Create a title named TicTacToe in the PlayFab Game Manager. Note the Title ID and Developer Secret Key — you will need them in both the Unity game and this functions app.
Enable Azure Functions integration
In Game Manager, navigate to Automation → Cloud Script → Functions and enable the integration.
Register your functions
For each deployed function:
- In the Azure Portal or VS Code extension, copy the function’s invokable URL. Make sure the key is set to default (Function key).
The URL format is:
[function-app-name].azurewebsites.net/api/[function-name] - In Game Manager under Automation → Cloud Script → Functions, click Register Function.
- Enter a name and the URL, then click Register.
/Functions folder: MakePlayerMove, MakeRandomAIMove or MakeMinimaxAIMove (register one as MakeAIMove), WinCheck, and ResetGameState.Available functions
MakePlayerMove
MakePlayerMove
Validates a player’s requested move and, if the target cell is unoccupied, applies it to the board state in PlayFab Player Data.Request model:
MakePlayerMoveRequest — contains PlayFabId and a Move with row and col.Response model: MakePlayerMoveResult — contains a Valid boolean indicating whether the move was accepted.MakeRandomAIMove
MakeRandomAIMove
Selects a random unoccupied cell on the board as the AI’s next move, stores it in Player Data, and returns the chosen
TicTacToeMove.Both AI move functions live in MakeAIMove.cs. Register one of them under the name MakeAIMove on PlayFab so the game client can call a single consistent name regardless of which algorithm backs it.MakeMinimaxAIMove
MakeMinimaxAIMove
Selects the optimal AI move using the Minimax algorithm, stores it in Player Data, and returns the chosen
TicTacToeMove.Swap between MakeRandomAIMove and MakeMinimaxAIMove by updating the registered URL on PlayFab without changing the client code. See Function registration URL flexibility below.WinCheck
WinCheck
Loads the current board state and checks whether any player has won or the board is full. If a winner is found, it updates the leaderboard, archives the game state to history, and clears the board.Response model:
WinCheckResult — contains a Winner field of type GameWinnerType (NONE, PLAYER, AI, or DRAW).ResetGameState
ResetGameState
Clears the current board state for the player by writing a fresh empty
TicTacToeState (9-element int array of zeros) to Player Data.Important concepts
Function registration URL flexibility
When registering a function with PlayFab you can choose any name — it does not have to match the actual function name in code. For example, you can registerMakeMinimaxAIMove’s URL under the name MakeAIMove on PlayFab, and the game client calls MakeAIMove. To swap the AI algorithm, update the URL of the MakeAIMove registration to point to MakeRandomAIMove — no client rebuild required.
Instance APIs vs. static APIs
Use PlayFab instance APIs instead of static APIs inside Azure Functions. TheFunctionContext<T> object from the CloudScript Plugin provides a PlayFabAPISettings and PlayFabAuthenticationContext for this purpose. Set the secret key on the settings object yourself before passing it to an instance API constructor.
The PlayFab CloudScript Plugin and FunctionContext
Add the plugin to every Azure Functions project that integrates with PlayFab. The plugin’sFunctionContext<T>.Create(req) factory method deserializes the full PlayFab payload — including the caller’s entity profile, player ID, API settings, and authentication context — into a strongly typed object.
Local debugging
To redirect your game client’sExecuteFunction calls to your local functions runtime instead of PlayFab’s servers:
Add the local ExecuteFunction shim
Download the
ExecuteFunction.cs Azure Function and place it in the /Functions folder alongside your other functions.Do not modify this function, do not deploy it to Azure, and do not check it into version control. It is a local-only development helper that mimics the PlayFab server’s behavior of building the function payload.
Create playfab.local.settings.json
Create a file named The value must end with a trailing
playfab.local.settings.json in either the game client root directory or your OS TEMP directory:playfab.local.settings.json
/. Update it if you change the default port or route prefix.Configure host.json route prefix
Edit The
host.json to explicitly declare the route prefix:host.json
LocalApiServer value in playfab.local.settings.json must match this prefix.