Nakama and Photon Fusion demo

A multiplayer platformer built with Unity 6 and Photon Fusion, with player accounts, friends, and leaderboards powered by Nakama.

The Nakama and Photon Fusion demo is a multiplayer platformer that combines two tools in one Unity game: Photon Fusion, a networking library that keeps players in sync during a match, and Nakama, an open-source game backend that stores player accounts, friends, and leaderboards. The game is built on Photon’s Fusion Starter sample, with Nakama added on top.

SpecsDescription
EngineUnity 6 with Photon Fusion 2 in shared mode
ServerNakama 3.42 (Go runtime module)
Nakama featuresAuthentication, Friends, Status, Leaderboards, Hooks
Looking for an in-depth walkthrough?
For a step-by-step look at the code behind each feature, see How to add Nakama to a Photon Fusion game.

Features #

  • Real-time multiplayer: Up to eight players race through the same level in a Fusion session, collecting 10 coins, crossing falling platforms, and reaching the flag first. In shared mode, the session runs in Photon Cloud without a game server, and each player’s client controls their own character.
  • Authentication: Players sign in to Nakama when the game starts. Photon Cloud checks each player’s Nakama session token before it lets them join a match.
  • Friends: Players add each other by username, see whether a friend is in the menu or in a game, and join a friend’s match from the friends list.
  • Leaderboards: The winner of each round saves their time to a leaderboard. The server rejects times that are too fast to be real.

For the full list of gameplay and networking features that come from the Fusion Starter, see Photon’s Fusion Starter overview.

Why use Photon and Nakama #

Photon Fusion is built for the match itself. It keeps game state in sync between players while a match is running, but it doesn’t keep anything once the session ends. Nakama is built for everything around the match: player accounts, social features, and records that last from one session to the next.

Together, they cover both halves of a multiplayer game. Fusion handles the fast, moment-to-moment gameplay, and Nakama gives players an account, friends to play with, and leaderboards to compete on, all of which carry over from one match to the next.

Try the demo #

To play against yourself, open this page in two separate browser sessions. You can also send this page to a friend and play together.

Controls #

Keyboard and mouse

  • Move: W, A, S, D, or the arrow keys
  • Look around: mouse
  • Jump: Space
  • Sprint: Left Shift
  • Open or close the game menu: Esc or Enter

During a match, the mouse controls the camera and the cursor is hidden. To select the Friends or leaderboard buttons in the top-left corner, open the game menu:

Opening the menu in your browser
Press Esc once to release the cursor, then press Esc again to open the game menu.

Play with two players #

In the steps below, Player 1 is your first window and Player 2 is your second window, or your friend.

  1. As Player 1, open Friends. The game assigns you a random username, such as SwiftOtter123.
  2. As Player 2, open Friends, enter Player 1’s username in Username to add, and select Send Invite.
  3. As Player 1, accept the invite. You’re now on each other’s friends lists.
  4. As Player 1, select Start Game, and wait for the match to start.
  5. As Player 2, check the friends list. Player 1 shows as In Game, with a Join button. Select Join to enter the same match.
  6. Race to collect 10 coins and reach the flag first.
  7. When the round ends, press Esc and select the leaderboard button in the top-left corner. The winner’s time is in the list.

Project structure #

The repository has two parts:

PathPurpose
UnityFusionDemo/The Unity project, built on the Fusion Starter
nakama-server/The Nakama Go module, and a Docker Compose file that runs it

In the Unity project, the Nakama code sits beside the Fusion Starter’s own code:

PathPurpose
Assets/02_Platformer/The platformer scene, prefabs, and gameplay scripts from the Fusion Starter
Assets/02_Platformer/Nakama/Scripts/Nakama sign-in, the friends list, and the leaderboard panel
Assets/Common/Scripts/Scripts shared across the Fusion Starter, including the game menu
Assets/Photon/The Photon Fusion SDK

The integration code is in these scripts:

FilePurpose
NakamaSingleton.csCreates the Nakama client, signs the player in, and opens a socket
UIGameMenu.csStarts the Fusion session with the Nakama session token, and updates the player’s status
FriendsList.csLoads friends, follows their status, and joins their match
FriendEntry.csShows a friend’s status and the Join button
Player.csUses the Nakama username as the Fusion nickname
GameManager.csDecides the winner in Fusion and saves the time to Nakama
LeaderboardPanel.csLists leaderboard records

On the server, the Go module is split by feature:

FilePurpose
main.goInitModule, which registers every RPC and hook, and the hook that notifies a player when a friend removes them
auth.goThe photon_auth RPC that Photon Cloud calls to check session tokens
usernames.goGives each new account a random username
leaderboard.goCreates the leaderboard, and the hook that rejects implausible times
Download the demo

How it works #

Authentication #

The game signs in to Nakama with device authentication when it starts, and the server gives each new account a random username. When the player starts or joins a match, the client passes its Nakama session token to Fusion. Photon Cloud sends the token to the photon_auth RPC on the Nakama server, which validates it and tells Photon whether to accept the player. The player’s Nakama username also becomes their nickname in the match.

For the code, see Authenticate players with Nakama.

Friends and joining matches #

The friends list uses Nakama’s friends features to send, accept, and remove friend requests.

Each player also publishes a Nakama status: In Menu, or In Game: followed by the Fusion session name. The friends list follows each friend’s status and shows a Join button when a friend is in a match. Selecting Join starts Fusion with that session name, which puts the player in the same match.

For the code, see Join a friend’s session.

Leaderboards #

The in-game leaderboard panel listing three players by rank, with their usernames and finishing times

Fusion decides who wins each round, and the winner’s client saves the time to a leaderboard. The leaderboard keeps each player’s best time. In this demo, a before hook on the Nakama server rejects times under three seconds to filter out obviously fake times.

Shared mode and cheating
Shared mode has no game server, so a player with a modified client can still submit a fake time. See When to use an authoritative leaderboard.

For the code, see Save the winner’s time.

Run the demo yourself #

To run the demo on your own computer, or connect it to your own Photon app and Nakama server, see Run the demo in the integration guide.

Credits #

Additional resources #