Skip to main content

Enjin Farmer: Unity Client

This page covers the Unity client for the Enjin Farmer sample game.

Set up the server first

This page assumes the game server, Enjin Platform, and Wallet Daemon are already running. If not, complete the Overview & Server Setup page first, then come back here.


Prerequisites

In addition to the shared prerequisites, the Unity client needs:

  • Unity Hub with Unity Editor version 6000.0.24f1.

Step 1: Set Up the Unity Client

It's time to set up the Unity project and connect it to your game server.

  1. Clone the Game Client:

    git clone https://github.com/enjin/platform-sample-game-client-unity.git
  2. Open Unity Hub.

  3. Click AddAdd project from disk and select the platform-sample-game-client folder you cloned earlier.

  4. Open the project.

  5. Once the project is open in the Unity Editor, you need to configure two things.

1. Stamp the Collection ID onto the NFT Items

The game's three Enjin Item assets (GemGreen, GoldCoin, and GoldCoinBlue, found in Assets/Enjin Integration/Scripts/Data/Items) each need to know the on-chain Collection ID the server created. Rather than paste it by hand, the project ships an Editor menu that fetches it from your running server and stamps it onto all three assets for you.

  • Make sure your game server is still running.
  • In the Unity Editor menu bar, select Enjin → Stamp Collection ID onto EnjinItem Assets.
  • Confirm the prompt. The Editor calls the server's /api/setup/collection-id endpoint and writes the returned ID onto every EnjinItem asset.
note

Run this once after the server's first launch. You only need to run it again if the canary state ever resets and the server creates a new collection.

2. Configure the connection to the Game Server

note

If you are running the game server and client on the same machine, and you haven't changed the default port of 3000, you can skip this step.

  • In the Project window, navigate to Assets/Enjin Integration/Prefabs/.
  • Select the EnjinManager prefab.
  • In the Inspector window, find the Enjin API Service (Script) component and configure the host property to your game server URL. If you are running the game server and client on the same machine, the default http://localhost:3000 is correct.


Step 2: Play the Game! 🎮

You're all set up and ready to play.

  1. In the Unity Editor's Project window, navigate to Assets/HappyHarvest/Scenes.
  2. Double click on the Farm_Outdoor scene.
  3. Press the Play button at the top of the Unity Editor to launch the game.
  4. Look at the Console window in the editor. You should see a log message: Server connection successful (Health Check: OK).

warning

If you see an error, double-check that your server is running and that the Host in the EnjinManager is correct.

  1. In the game, click the Menu button (top-right), then Login.

  1. Enter an email and password and click Login. This will register a new user and create a managed wallet for your player on the Enjin Platform.
  2. Close the menu and use the W, A, S, D keys to move your character.
  3. Walk up to a crop and click on it to harvest it. Keep harvesting until a resource item pops out.

  1. Click on the resource item to collect it. This action tells the game server to mint that item as an NFT to your player's wallet.
  2. From the inventory, you can click Melt to destroy the NFT or enter another wallet address in the Transfer Recipient field and click Send to send it to someone else.

Keep your daemon wallet funded

New managed wallets start empty, so the server automatically drips a little cENJ (1 ENJ by default) from your daemon wallet to each new player wallet so it can pay the fees for melting and transferring. This means the daemon wallet itself needs cENJ — to create the collection, mint tokens, and fund new players. To top up the daemon wallet for testing, use the built-in Canary faucet in the Platform UI.

Understanding the code

To learn how the Unity client and game server work under the hood, see the Implementation Breakdown below (server-side details live on the Overview page).

Happy farming!


Implementation Breakdown

The Unity client handles gameplay and offloads all blockchain operations to the game server. The server side — collection bootstrap, managed wallets, and the API endpoints the client calls — is documented on the Overview page.

Core Components

The Enjin integration is managed by a few key scripts and a central prefab:

  • EnjinManager.prefab: The heart of the integration. This prefab is added to the Farm_Outdoor scene and configures the Host URL (e.g., http://localhost:3000) in the Inspector to connect to your game server.
  • EnjinManager.cs: A singleton controller that manages the player's session (auth token, wallet data) and exposes high-level methods like MintToken() for other game scripts to use.
  • EnjinApiService.cs: Handles all REST API communication with the game server using Unity's UnityWebRequest.
  • EnjinItem.cs: A ScriptableObject that represents the data of a blockchain item, such as its display name and its corresponding on-chain token ID.
  • StampCollectionIdMenu.cs: An Editor utility that adds the Enjin → Stamp Collection ID onto EnjinItem Assets menu. It calls the server's /api/setup/collection-id endpoint and writes the returned ID onto every EnjinItem asset, so you don't have to paste it by hand.
  • UI Scripts (BackpackUI.cs, BackpackItemController.cs): Scripts that manage the UI for viewing and interacting with the player's NFT inventory.

Initial Setup & Player Authentication

  1. Health Check: On launch, the client calls the /api/auth/health-check endpoint to ensure the server is available.
  2. Login/Register: From the login screen, the player clicks "Login", which calls the EnjinManager.Instance.RegisterAndLogin() method.
  3. API Request: This triggers EnjinApiService to send a POST request to the /api/auth/register endpoint.
  4. Store Auth Token: The server responds with a JWT authentication token. The client saves this token locally using PlayerPrefs and loads it on subsequent launches for a seamless experience.

In-Game NFT Interactions

All blockchain actions are initiated by the client but securely executed by the server.

Harvesting and Minting Tokens

When a player harvests a crop with the Hoe tool, they have a chance to find a resource token.

  1. An EnjinToken GameObject appears on the harvested tile.
  2. When the player collects this GameObject, its InteractedWith() method is triggered.
  3. This calls EnjinItem.Collect(), which in turn calls EnjinManager.Instance.MintToken().
  4. EnjinManager then uses EnjinApiService to send a request to the /api/token/mint endpoint.

Viewing the Wallet (Backpack UI)

  1. Clicking the backpack icon opens the inventory screen, managed by BackpackUI.cs.
  2. The UI calls EnjinManager.Instance.GetManagedWalletTokens(), which sends a request to the /api/wallet/get-tokens endpoint.
  3. The BackpackUI then populates the view with the returned tokens.
  4. The BackpackUI also subscribes to the EnjinManager.Instance.OnWalletUpdated event to automatically refresh the inventory after a token is minted, melted, or transferred.

Melting and Transferring Tokens