Set up Hiro with Nakama
This guide walks you through installing the Hiro Unity SDK and connecting your Unity game to Nakama. You’ll set up the NakamaSystem, Hiro’s connection manager that handles authentication, sessions, and network connectivity. Once you complete this guide, you’ll be ready to add Hiro’s game systems to your project.
Get started with sample projects
Dive into the codebase right away with hands-on demos and sample projects.
Explore sample projects 🚀Install the Hiro Unity package #
The Unity client is available from Heroic Labs. Contact us
to request a Hiro.unitypackage file.
Once you have the package:
- Drag
Hiro.unitypackageinto your Unity project or use Assets > Import Package > Custom Package. - Set your API Compatibility Level to .NET Standard 2.1:
- Open Edit > Project Settings
- Select Player from the sidebar
- Navigate to Other Settings > Configuration
- Set API Compatibility Level to .NET Standard 2.1
- If you plan to use in-app purchases, install the In App Purchasing package:
- Open Window -> Package Manager
- Switch to Unity Registry
- Search for “In App Purchasing”
- Click Install
What is the NakamaSystem? #
The NakamaSystem is one of Hiro’s core systems. It’s the foundation that other Hiro systems (like Economy, Inventory, and Achievements) build upon. Without it, your game can’t communicate with Nakama. In addition to managing your connection to the Nakama server, it handles:
- Authentication: Logs players in using device IDs, email, social accounts, or custom authentication.
- Session management: Keeps track of auth tokens and automatically refreshes them before they expire.
- Connection monitoring: Detects when you’re online or offline and reconnects when needed.
- Client access: Provides your game with access to the Nakama client, session, and socket objects.
NakamaSystem manages your player’s session internally. While most authentication flows work the same as in vanilla Nakama, the difference is important when you need to switch to a different account or override an existing session. See Authentication
.Understand Hiro’s deterministic startup #
Hiro uses a structured approach to initialize your game using a deterministic startup. Instead of scattering initialization code throughout your project, you inherit from the HiroCoordinator class and configure everything in one place.
This approach gives you:
- Predictable initialization order: Systems start up in the sequence you define.
- Centralized configuration: All system setup lives in one file.
- Easy debugging: You can see exactly how your game initializes.
- Clean architecture: Separates game systems from game logic.
The HiroCoordinator is a MonoBehaviour that stays alive throughout your game’s lifetime. You’ll create your own coordinator class that inherits from it and override the CreateSystemsAsync() method to configure your systems.
Create your game coordinator #
Let’s create the coordinator that will initialize Hiro and set up the NakamaSystem.
Set up the GameObject #
In your Unity scene, create a new empty GameObject:
- Right-click in the Hierarchy panel.
- Select Create Empty.
- Name it
GameCoordinator.
Create a new C# script:
- Right-click in your Project panel.
- Select Create > C# Script.
- Name it
GameCoordinator.
Attach the script to your GameObject:
- Select the
GameCoordinatorGameObject. - Drag the
GameCoordinatorscript onto it in the Inspector.
- Select the
Write the authentication function #
Your authentication function needs to handle logging players in and keeping them logged in between sessions. This function will:
- Store authentication tokens in
PlayerPrefsso users don’t have to log in every time. - Check if a saved session exists and is still valid.
- Refresh sessions before they expire (adding a one-hour buffer).
- Create new accounts automatically using device authentication.
- Return cached sessions when offline (won’t work for first-time users).
Open your GameCoordinator script and add the following code:
| |
Initialize NakamaSystem
#
Now you need to initialize the NakamaSystem by overriding the CreateSystemsAsync() method. This method will:
- Configure your Nakama server connection (host, port, and scheme).
- Set up network monitoring to detect when your internet connection goes up or down.
- Create the
NakamaSystemwith your server settings and authentication function. - Listen for session updates and save new tokens when sessions refresh automatically.
- Create the systems container that holds all your Hiro systems.
Add the CreateSystemsAsync() method to your GameCoordinator class:
| |
Server connection settings:
scheme: Use"http"for local development,"https"for production servers.host: Your Nakama server address (127.0.0.1for local, your domain for production).port: Nakama’s HTTP API port (default is7350).serverKey: Your server key from the Nakama configuration (change this in production!).
Complete example #
Here’s the complete script with both functions:
| |
Test your setup #
Make sure your Nakama server is running.
Press Play in the Unity Editor.
Check the Console for these log messages:
Network is online: True(confirms network monitoring works)New user account '[user-id]' created.(on first run only)- No errors about connection failures
If you see errors, verify your server connection settings.
Use the NakamaSystem in your game
#
Now that the NakamaSystem is initialized, you can access it from any MonoBehaviour in your game.
Access the system #
From any script, use the GetSystem() extension method:
| |
Refresh system data #
Hiro systems cache data locally for performance. Call RefreshAsync() to fetch the latest data from the server:
| |
You typically call this when:
- Your game starts.
- The player returns from being offline.
- You want to ensure you have the latest server data.
Access account information #
The NakamaSystem provides quick access to the current user’s account:
| |
Read storage objects #
You can read Nakama storage objects directly through the NakamaSystem:
| |
Access the Nakama client directly #
For operations not wrapped by the NakamaSystem, access the Nakama client directly:
| |
Reset a player account for QA #
When you’re testing, it’s often useful to return a player to a clean slate. ResetAccountAsync() deletes the current account, re-runs your authorizer function to create a fresh one, and updates every Hiro system with the new account:
| |
After the reset, the client’s ReceivedSessionUpdated event fires with the new session, so cached tokens (for example in PlayerPrefs) are refreshed automatically.
ResetAccountAsync() permanently deletes the current player account, so use it only in development and QA builds, never in production. Your authorizer function must create a new account when called, for example with device authentication, rather than returning a cached session.Next steps #
Now that you’ve set up Hiro with Nakama, next you can:
Initialize other Hiro systems to add economy, inventory, achievements, and other game features to your game.
Explore sample projects for complete ready-to-run examples that demonstrate Hiro in action.
