Skip to main content
This sample is a complete Tic-Tac-Toe game built in Unity that demonstrates PlayFab’s Azure Functions (Cloud Script v2) integration. All game logic — move validation, win checking, and AI move generation — runs as Azure Functions, making the game fully server-authoritative.
This game requires the companion Azure Functions app to be deployed before the Unity project can run. See the Tic-Tac-Toe Functions sample for setup instructions.

Requirements

Unity 2019.1.8f1+

The project requires Unity 2019.1.8f1 or later.

PlayFab Unity Beta SDK

Included with the source code. The Azure Functions integration features are in beta and require the Beta SDK.

MK Glow Free plugin

A free third-party shader plugin for visual effects, available on the Unity Asset Store.

Azure Functions app

The companion Tic-Tac-Toe Functions app must be deployed and registered in PlayFab before running the game.

PlayFab configuration

1. Create a PlayFab title

Create a new PlayFab title named TicTacToe in Game Manager. Note the Title ID — you will need to enter it in Assets/Scripts/Constants.cs.

2. Enable Azure Functions integration

In Game Manager, navigate to Automation → Cloud Script → Functions and enable the Azure Functions integration.

3. Register your Azure Functions

For each function in the Azure Functions app:
  1. Get the invokable URL from the Azure Portal (Function → Get function URL, key type: default (Function key)). URLs follow the format [function-app-name].azurewebsites.net/api/[function-name].
  2. In Game Manager under Automation → Cloud Script → Functions, click Register Function.
  3. Enter the function name and paste the URL, then click Register.
Register all functions found in the /Functions folder of the Tic-Tac-Toe Functions repository.
Only the URL of a registered function can be edited after registration. The function name cannot be changed, so double-check names before registering.

Setup

1

Deploy the Azure Functions app

Follow the setup guide in the Tic-Tac-Toe Functions sample to build and deploy the Azure Functions app, then complete the PlayFab configuration steps above.
2

Clone the repository

git clone https://github.com/Myst0gan1/PlayFab-Samples.git
3

Open the project in Unity

In Unity Hub, click Open and navigate to:
PlayFab-Samples/Samples/Unity/TicTacToe
4

Add the PlayFab Unity Beta SDK

Drag the PlayFabSDK folder from the Beta SDK repository’s Source/ directory into the game’s Assets/ folder via the Unity Project View (not the OS file browser). Unity will recompile and generate .meta files automatically.
5

Set the Title ID

Open Assets/Scripts/Constants.cs and set TITLE_ID to the Title ID from your PlayFab title.
Constants.cs
public class Constants
{
    public const string TITLE_ID = "";        // Set this to your PlayFab Title ID
    public const string CLOUD_NAME = "";      // Set if using a non-default cloud
    public const bool COMPRESS_API_DATA = false;
}
6

Open the main scene and play

Open Assets/Scenes/Main.unity, then press Play. Log in when prompted and the game loop will begin.

Game flow

Once the player is logged in, the game loop runs as follows:
Login
  └── Player move
        ├── Execute PlayerMove function (validate move server-side)
        ├── If valid → place token
        └── If invalid → prompt again
              └── Win check (execute WinCheck function)
                    ├── Player wins → game over
                    └── No winner → AI move
                          ├── Execute MakeAIMove function
                          ├── If AI moves → place token
                          └── If no space → skip AI turn
                                └── Win check (AI win or draw)
                                      └── Repeat from Player move
1

Player move

The player taps a cell. The client calls the MakePlayerMove Azure Function, submitting the chosen row and column. If the move is valid the token is placed; otherwise the player must choose again.
2

Win check after player move

The WinCheck Azure Function evaluates the current board state. If the player has won, the game ends.
3

AI move

The MakeRandomAIMove or MakeMinimaxAIMove Azure Function generates the AI’s next move. If the AI cannot move (board full), its turn is skipped.
4

Win check after AI move

WinCheck runs again. If the AI has won or the board is full with no winner, the game ends as a draw.
5

Repeat

If the game is not over, control returns to step 1.

Code structure

The game’s code lives in Assets/Scripts/ and is organized into the following files and folders.

Top-level files

FileResponsibility
Game.csEntry point. Manages the overall game lifecycle: player login, starting the game loop, detecting game-over conditions, and allowing the game to be restarted.
Board.csMonoBehaviour attached to the physical board object in the scene. Holds three TokenRows, each containing three TokenPlaceholders representing the nine Tic-Tac-Toe cells.
Settings.csStores runtime configuration (Title ID, Azure Function URLs) and exposes it to the rest of the game.
Constants.csCompile-time constants, including TitleID.

/Handlers

Request handlers that wrap PlayFab SDK calls using Unity Coroutines, producing flat, readable async code without nested callbacks.
HandlerAzure Function calledPurpose
LoginHandler.csLogs the player in via the PlayFab client SDK.
PlayerMoveHandler.csMakePlayerMoveSubmits a player move and receives a validity result.
WinCheckHandler.csWinCheckSubmits the board state and receives a winner determination.
AIMoveHandler.csMakeAIMove (registered name on PlayFab; backed by MakeRandomAIMove or MakeMinimaxAIMove)Requests an AI move and receives the chosen cell.
ResetGameStateHandler.csResetGameStateClears the server-side game state so a new game can start.
RequestHandler.csBase class / utility shared by the other handlers.

/Models

Data structures used for serialization between the Unity client and the Azure Functions.
ModelDescription
TicTacToeMoveRepresents a move with row and col fields (lower-cased to match Azure Functions default serialization).
MakeMoveRequestRequest payload for player and AI move functions.
MakePlayerMoveResultResponse from MakePlayerMove, indicating whether the move was valid.
MakeAIMoveResultResponse from the AI move functions, containing the AI’s chosen cell.
WinCheckResultResponse from WinCheck, containing the GameWinnerType.
GameWinnerTypeEnum: NONE, PLAYER, AI, or DRAW.
OccupantTypeEnum representing a cell state: NONE, HUMAN, or AI.
TokenPlaceholderModel for a single board cell, tracking its OccupantType.
TokenRowHolds three TokenPlaceholders forming one row of the board.
PlayerInfoStores the player’s PlayFab ID and display name.
ResetGameStateRequestPayload sent to ResetGameState.
Azure Functions default serializer responds with lower-cased field names. Ensure all model fields shared between the client and Azure Functions use lower-case property names to avoid deserialization errors. See TicTacToeMove (row, col) as a reference.

Key PlayFab features

Azure Functions integration

Replaces traditional CloudScript with Azure Functions, enabling strongly-typed C# server logic with full IDE support and local debugging.

ExecuteFunction

The PlayFab client API call used to invoke a registered Azure Function from the Unity game. Passes a request payload and returns a typed response.

LoginWithCustomID

Authenticates the player anonymously for quick local testing without requiring account creation.

Server-authoritative game state

Move validation, win detection, and AI logic all run on the server via Azure Functions, preventing client-side cheating.

Tic-Tac-Toe Functions

The companion Azure Functions app that provides the server-side game logic required by this Unity client.

Sign-in

A focused sample demonstrating the PlayFab authentication flows used as the login step in this game.

Build docs developers (and LLMs) love