Unity Achievements API
Learn how to integrate Game Jolt Trophies (Achievements) into your Unity game using the official Game Jolt Unity SDK.
đĻ Prerequisitesâ
- Game ID & Private Key: Get these from your Game Jolt Dashboard under Game API > Settings.
- Download SDK: Get the official Unity SDK from Game Jolt API Libraries.
đ Initializationâ
Before calling any trophy functions, you must initialize the API.
using GameJolt.API;
public void Start() {
GameJolt.API.Core.Init(your_game_id, "your_private_key", (bool success) => {
if (success) {
Debug.Log("Game Jolt API initialized!");
}
});
}
đ Unlocking Trophiesâ
To unlock a trophy, use the Trophies.Unlock method. The user must be logged in for this to work.
Parametersâ
| Name | Type | Description |
|---|---|---|
trophyId | int | The unique ID of the trophy from your dashboard. |
callback | Action<bool> | Called when the request completes. |
Example implementationâ
using GameJolt.API;
public void AwardTrophy(int id) {
Trophies.Unlock(id, (bool success) => {
if (success) {
Debug.Log("Trophy Unlocked!");
} else {
Debug.LogError("Error unlocking trophy. Is the user logged in?");
}
});
}
đ Fetching Trophiesâ
You can also fetch a list of all trophies to display them in your own custom UI.
Trophies.Get((Trophy[] trophies) => {
if (trophies != null) {
foreach (var trophy in trophies) {
Debug.Log(trophy.Title + " - " + (trophy.Unlocked ? "Unlocked" : "Locked"));
}
}
});
:::tip Pro Tip Use the Trophy Emulator below to see how this looks in a live environment! :::
đšī¸ Trophy Emulator
Click below to simulate how a Game Jolt achievement looks in-game.
đŽ In-Game Practical Scenarioâ
Let's look at a real-world example: unlocking a trophy when a player defeats a specific enemy. This scenario demonstrates the common "Milestone" pattern: checking for a condition (enemy death) and then triggering the API call.
The Objectiveâ
- Condition: The player reduces an enemy's health to 0.
- Action: Call
Trophies.Unlockwith the specific Trophy ID for "Defeat Mr. Enemy". - Feedback: The user sees the Game Jolt achievement notification instantly.
Recreate the Scenario (C#)â
using UnityEngine;
using GameJolt.API;
public class EnemyController : MonoBehaviour {
public int trophyId = 12345; // The ID for "Defeat Mr. Enemy"
public int health = 1;
public void TakeDamage(int amount) {
health -= amount;
if (health <= 0) {
Die();
}
}
private void Die() {
Debug.Log("Mr. Enemy has been defeated!");
// Trigger the Game Jolt Achievement
Trophies.Unlock(trophyId, (bool success) => {
if (success) {
Debug.Log("Achievement 'Defeat Mr. Enemy' granted!");
}
});
Destroy(gameObject); // Remove enemy from the game world
}
}
Try it Yourselfâ
Move the player with WASD or Arrows, and press F to attack Mr. Enemy when you are close!