Nakama Friends Sample Project

The official sample project for the Nakama Friends system. Built with minimal boilerplate so you can easily copy the code into your project and adapt the scripts and UI to suit your game.

Download Sample Project

Features #

  • View and manage friends lists
  • Add and remove friends
  • Block and unblock players
  • Real-time friend updates
  • Built-in account switcher for testing

Installation #

The installation steps and resulting folder structure will vary depending on if you downloaded the project from Github or the Unity Asset Store.

Notice

This project was built with Unity 6.0+ in mind. Whilst the majority of the template is not version specific, the UI may not behave as intended on older versions of Unity.

For optimal display, set your game resolution in the Unity Editor to 1920x1080

  1. Clone or download the Sample Projects repository onto your machine.
  2. From the Unity Hub, click Add -> Add project from disk and choose the top-level UnityNakamaFriends folder.
  3. You may see several warnings about Editor version incompatibility. Feel free to ignore these messages as long as you’re on Unity 6 or greater. Continue on until you arrive at the main Editor UI.
  4. Open the main scene by navigating to Assets -> UnityNakamaFriends -> Scenes -> Main.
  5. Hit Play.

The project connects to our demo server so you can see the friends system in action immediately.

Folder Structure #

Assets/
├── UnityNakamaFriends/
    ├── HeroicUI/           # UI assets and styling
    ├── Scripts/            # Main friends management code
    ├── UI/                 # UI Builder files
    └── ...                 # Everything else
├── Packages/               # Contains the Nakama Unity package

Code Overview #

Main Controller (NakamaFriendsController.cs) #

Handles all core operations including calling the Nakama Friends API.

Authentication

1
2
// Connects to Nakama server and authenticates with device ID
await AuthenticateWithDevice();

Friends Operations

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// Fetch friends list by state
var result = await Client.ListFriendsAsync(Session, (int)friendState, limit, cursor);

// Add a friend (or accept a friend request)
await Client.AddFriendsAsync(Session, null, friendsToAdd);

// Remove a friend (or decline a friend request)
await Client.DeleteFriendsAsync(Session, null, friendsToDelete);

// Block a user
await Client.BlockFriendsAsync(Session, null, friendsToBlock);

Friend States

The system supports four different friend states:

1
2
3
4
5
6
7
public enum FriendState
{
    FRIEND = 0,          // Confirmed friends
    INVITE_SENT = 1,     // Outgoing friend requests
    INVITE_RECEIVED = 2, // Incoming friend requests
    BLOCKED = 3          // Blocked users
}

Real-time Notifications

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
private void OnReceivedNotification(IApiNotification notification)
{
    switch (notification.Code)
    {
        case -2: // Incoming friend request received
            // Update UI for received requests
            break;
        case -3: // Outgoing friend request accepted
            // Update UI for new friends
            break;
    }
}
Web sockets
This sample project uses web sockets in order to handle real-time updates and make changes to the UI, but they are not required to use the Nakama Friends feature.

Record View (FriendsRecordView.cs) #

A class that displays individual friend entries with different actions based on friend state:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
public void SetFriend(IApiFriend record)
{
    usernameLabel.text = record.User.Username;
    var state = (NakamaFriendsController.FriendState)record.State;

    // Show/hide buttons based on friend state
    switch (state)
    {
        case NakamaFriendsController.FriendState.FRIEND:
            // Show remove and block buttons
            break;
        case NakamaFriendsController.FriendState.INVITE_RECEIVED:
            // Show add, remove, and block buttons
            break;
        // ... other states
    }
}

Account Switcher #

The Account Switcher lets you explore the project as different users without managing multiple builds. It’s useful for testing friend requests between different accounts.

How to use:

  1. Open the Account Switcher panel (Tools > Nakama > Account Switcher)
  2. Select different accounts from the dropdown to switch between up to four test users
  3. Each account gets automatically created the first time you select it

Key points:

  • Only works while your game is running in Play mode
  • Usernames will display in the panel after switching to an account for the first time

Setting Up Your Own Nakama Server #

While this project works with our demo server, you’ll want to set up your own Nakama instance in order to create custom friend configurations, behaviors, and rules. Using Docker, you can get up and running in minutes.

Read the full setup guide: Nakama Installation Docs

Connect this Unity Project to your Server #

After installing Nakama and running it locally, edit these settings in the Unity Editor to connect it to your server:

  1. Select NakamaFriendsController from the scene hierarchy panel.
  2. Open the Inspector tab.
  3. Look for the field inputs under Nakama Settings and replace them with the following:
    1. Scheme: http
    2. Host: 127.0.0.1
    3. Port: 7350
    4. Server Key: defaultkey

Additional Resources #