Unity WebGL Integration
This guide explains how to integrate a Unity WebGL game client with Game Hub, allowing players to access the game from the Game Hub platform.
1. Build and Set Up the Unity Client
Add the WebGL Template
To build the Unity client for Game Hub, start by adding the provided WebGL Template to your Unity project. Drag and drop the package into your project files. For code reference see example project here.
Configure and Build for WebGL
Configure your Unity project to build for the WebGL target and ensure that the WebGL Template is correctly applied. In Unity ProjectSettings, set the WebGL Platform template to Aghanim. Complete the build process, preparing the output files for hosting on Game Hub.
Host the Build Files
The Unity game build can be hosted on Game Hub using one of two options:
-
Hosting on Aghanim CDN: Send the
build.zipfile to the Aghanim Team, who will host it on Aghanim CDN. -
External Hosting: If you host the game externally, provide the URL to the
index.htmlfile of the unzipped WebGL build for integration.
2. Authorize Players on the Game Hub
Token-Based User Authorization
When a user logs into Game Hub and clicks on "Play," Game Hub opens the game iframe and includes a one-time token in the URL’s query string.
Retrieve and Send the Token
The Unity client should extract the token from the URL and send it to your game backend for verification.
Authenticate the User on the Game Backend
The game backend authenticates the token by by using Authenticate User endpoint.
Request format:
{
"token": "819e1c655ab5188f8055494cfd8493b665071f57e280597203c7802e734aebf0"
}
Success response format for user with linked player:
{
"id": "usr_eCEGnkEHeKA",
"name": "Player6739",
"player_id": "7d7cc29d277844adb6c89b239facbfbd",
"facebook_id": "1234567890",
"telegram_id": null
}
Success response format for user without linked player:
{
"id": "usr_eCEGnkEHeKA",
"name": "Player6739",
"player_id": null
"facebook_id": "1234567890",
"telegram_id": null
}
When the response is successful, and the user has a linked player, the game backend should authorize the player directly. If the user does not have a linked player, the game backend should create a new player and link it to the user.
3. Linking Players
Check whether Player is Linked
Before linking a player to a user, check whether the player is already linked to another user by using Get User endpoint.
Success response format:
{
"id": "usr_eCEGnkEHeKA",
"name": "Player6739",
"player_id": "7d7cc29d277844adb6c89b239facbfbd"
"facebook_id": "1234567890",
"telegram_id": null
}
Link Player
To link a player to a user use Link Player endpoint.
Request format:
{
"player_id": "7d7cc29d277844adb6c89b239facbfbd"
}
Success response format:
{
"id": "usr_eCEGnkEHeKA",
"name": "Player6739",
"player_id": "7d7cc29d277844adb6c89b239facbfbd"
"facebook_id": "1234567890",
"telegram_id": null
}
Unlink Player
To unlink a player from a user use Unlink Player endpoint.
Request format:
{
"player_id": "7d7cc29d277844adb6c89b239facbfbd"
}
Success response format:
{
"id": "usr_eCEGnkEHeKA",
"name": "Player6739",
"player_id": null
"facebook_id": "1234567890",
"telegram_id": null
}
4. In-Game Purchases on Aghanim Platform
Purchase Items via URL
Players can make in-game purchases by opening the checkout page using the following URL format:
https://{your-game-hub-domain}/go/checkout?player_id={PLAYER_ID}&item_sku={SKU}
Optionally, you can include the following query parameters to customize the checkout page:
locale: The player's localeitem_name: The name of the itemitem_description: The description of the itemitem_image_url: The URL of the item image
Purchase Items via SDK
Alternatively, you can use the AghanimSDK.PurchaseItem(string sku) method to trigger the purchase flow from within the game client.
5. Save Guest Player Progress
If you game supports guest players, you can use AghanimSDK.Authorize(string playerId, string playerName) to trigger authorization flow the Game Hub. Upon successful authorization, the Game Hub will return back the user to the game client with a one-time token in the iframe URL which can be used to link the guest player to the user.
Need help?
Contact our integration team at integration@aghanim.com