how to connect photon with playfab unity

How To Connect Photon With PlayFab Unity

If you’re building a multiplayer game, learning how to connect Photon with PlayFab Unity is a game-changing step. Photon provides real-time multiplayer networking, while PlayFab handles backend services like authentication, player data, and leaderboards. By combining the two, you can create scalable, feature-rich online games with ease.

This guide will walk you through the full integration process—setting up Photon and PlayFab, syncing player IDs, authenticating users, and sending matchmaking data. We’ll also dive into best practices and solutions to common pitfalls.

Why Use Photon and PlayFab Together in Unity?

Before we dig into how to connect them, it’s important to understand why you might want to.

Photon: Real-Time Multiplayer Networking

Photon (Photon PUN or Photon Fusion) is known for:

  • Low latency multiplayer

  • Cloud-hosted game rooms

  • Fast synchronization of player actions

  • Cross-platform support

PlayFab: Backend Game Services

PlayFab excels at:

  • User authentication (email, device, social)

  • Storing player data and stats

  • Cloud scripts and server logic

  • Leaderboards and achievements

Together, they give you the best of both worlds—real-time gameplay with persistent backend support.

Prerequisites For Unity Photon And PlayFab Integration

Before starting, make sure you have the following:

  1. Unity (2020 or later)

  2. Photon PUN 2 (or Fusion, depending on your use case)

  3. PlayFab SDK for Unity

  4. Photon App ID

  5. PlayFab Title ID

You can get both SDKs from the Unity Asset Store or GitHub.

Step 1: Set Up Unity Project

  1. Create a new Unity project.

  2. Import both Photon PUN 2 and PlayFab Unity SDK.

  3. Set your build target to your desired platform (PC, Android, iOS).

After importing, Unity will prompt you to fix any missing packages or resolve API changes—do that before continuing.

Step 2: Configure PlayFab in Unity

To begin integrating backend functionality:

  1. In Unity, go to PlayFab > Editor Extensions > PlayFab Editor Extensions.

  2. Enter your PlayFab Title ID.

  3. Click Set Up PlayFab SDK.

This sets up your API key and connects your Unity project to your PlayFab title.

Step 3: Configure Photon in Unity

Now we’ll connect to Photon:

  1. Go to Photon > Photon Unity Networking > PUN Wizard.

  2. Paste your Photon App ID.

  3. Choose your Photon region (e.g., “US” or “Auto”).

  4. Save the configuration.

This step links your game to Photon’s real-time multiplayer servers.

Step 4: Authenticate Players with PlayFab

Before connecting to Photon, users should be authenticated. Here’s how you can log in a user with PlayFab using a device ID (or guest login):

This script does two things:

  • Authenticates the user with PlayFab

  • Uses the PlayFabId as a unique identifier to connect to Photon

Step 5: Photon With PlayFab Unity Authentication

Now that the user is logged in, we authenticate them with Photon using the PlayFab ID as a custom authentication value. Photon will use this ID to identify players consistently across sessions. If you’re wondering how to connect Photon with PlayFab Unity, this step ensures both services recognize the same user identity, allowing for seamless multiplayer integration and persistent player data.

Make sure you’ve set up your Photon account to accept Custom Authentication:

  1. Go to the Photon Dashboard.

  2. Navigate to your App > Authentication tab.

  3. Enable Custom Authentication and select PlayFab as the provider.

Then, update your Photon configuration in Unity:

If everything is set up correctly, Photon will now accept PlayFab-authenticated users.

Step 6: Syncing Player Data Between PlayFab and Photon

Once a player is connected to both services, you may want to sync data like:

  • Display name

  • Player stats (kills, levels, etc.)

  • Custom properties

Example: Set Player Nickname from PlayFab

This allows Photon to display the same player name used in PlayFab.

How to Use Photon Rooms with PlayFab Matchmaking

One powerful feature is combining PlayFab matchmaking with Photon game rooms.

Basic Flow:

  1. Use PlayFab’s Matchmaking API to create a match ticket.

  2. On match success, receive a Photon room name or connection info.

  3. Use Photon to connect to that room.

PlayFab’s matchmaking service allows you to pair players based on criteria (skill, region, latency), then connect them using Photon for real-time gameplay.

This requires a PlayFab server-side script (Cloud Script or Azure Function) that generates a Photon room and returns connection details.

How to Store and Retrieve Multiplayer Stats with PlayFab

Let’s say you want to update a kill count or win/loss ratio after a match. You can call PlayFab APIs from your Unity client like this:

This makes sure your backend stays in sync with what’s happening in real-time.

Troubleshooting Photon and PlayFab Integration

Issue: Photon Connect Fails After PlayFab Login

  • Double-check that Custom Authentication is enabled on Photon Dashboard.

  • Ensure you’re passing the PlayFabId correctly to AuthenticationValues.

Issue: Nickname not syncing

  • Use PhotonNetwork.NickName only after Photon has connected.

Issue: Stats not updating

  • Check that the stat name exists in PlayFab Game Manager under Player Statistics.

Best Practices for Connecting Photon with PlayFab in Unity

  1. Authenticate first, then connect: Always login to PlayFab before attempting Photon connection.

  2. Use PlayFabId as the unique player key: Ensures consistency across services.

  3. Minimize Photon traffic: Offload persistent data (inventory, progress) to PlayFab instead of Photon.

  4. Encrypt sensitive data: Don’t send passwords or private tokens directly.

  5. Monitor logs: Use Debug.Log() generously during integration and test thoroughly.

Conclusion

If you’re serious about developing multiplayer games, understanding how to connect Photon with PlayFab Unity gives you the ultimate advantage. By combining Photon’s fast, reliable multiplayer networking with PlayFab’s robust backend services, you unlock:

  • Seamless authentication

  • Scalable matchmaking

  • Persistent player data

  • Real-time gameplay

Quick Recap:

  • Install and configure Photon & PlayFab SDKs

  • Authenticate users via PlayFab

  • Pass PlayFabId to Photon’s authentication

  • Sync data like nicknames and stats

  • Use PlayFab for matchmaking and persistent storage

By following the steps in this guide, your Unity multiplayer project will be ready to scale with a modern, flexible tech stack.

Once you’ve connected Photon with PlayFab for multiplayer functionality, you might want to enhance player feedback during real-time interactions — like triggering a vibration when a player is hit or a match starts. If you’re targeting mobile devices, this Unity Phone Vibration Tutorial shows you how to easily add vibration effects to make gameplay more immersive.

Leave a Reply