Unlocking achievements from Unity
This tutorial assumes you already have setup your app, if not please follow these tutorials (only one platform is really necessary, but preferably all):
- Introduction
- Adding achievements on Google GameServices
- Adding achievements on Appstore Connect
- Adding achievements in Unity
- Authentication
Achievement service
To be able to unlock achievements you have to use the CP Social achievement service, this service handles all the logic for unlocking and keeping track of the achievements.
**NOTE** Only after authenticating the user you can get access to the achievement service
You can use the following example to get the instance of the achievement service after authenticating the user:
private ICP_AchievementService _achievementService;
//Unity start
void Start()
{
CP_Social.Authenticated += (result) =>
{
if (result.success)
{
_achievementService = CP_Social.GetAchievementService();
}
};
}
Unlocking achievements
After getting the instance of the achievement service, you can start unlocking achievements.
Unlocking achievements can be done two ways.
Using the universal name
You can unlock an achievement by passing in the universal name set in the configurator:
public void UnlockAchievement()
{
if (CP_Social.isAuthenticated)
{
_achievementService.UnlockAchievement("YOUR_ACHIEVEMENT_NAME", (result) =>
{
Debug.Log("Achievement unlocked response: " + result.success);
});
}
}
Referencing the achievement info
You can unlock an achievement by passing in the CP_AchievementInfo
created by the configurator:
public void UnlockAchievement()
{
if (CP_Social.isAuthenticated)
{
CP_AchievementSettings achievementSettings = CP_Social.socialSettings.AchievementSettings;
CP_AchievementInfo info = achievementSettings.GetAchievementInfoByUniversalName("YOUR_ACHIEVEMENT_NAME");
_achievementService.UnlockAchievement(info, (result) =>
{
Debug.Log("Achievement unlocked response: " + result.success);
});
}
}
Adding progression to achievements
After getting the instance of the achievement service, you can start unlocking achievements.
Incrementing achievements can be done two ways.
Using the universal name of the incremental achievement
You can unlock an achievement by passing in the universal name set in the configurator:
public void AddProgressionToAchievement(int numSteps)
{
_achievementService.IncrementAchievement("YOUR_ACHIEVEMENT_NAME", numSteps, (result) =>
{
Debug.Log("Increment result status: " + result.success);
Debug.Log("Achievement unlocked response: " + result.value.state + " " + result.value.currentSteps + " " + result.value.totalSteps);
});
}
Referencing the incremental achievement info
You can unlock an achievement by passing in the CP_IncrementalAchievementInfo
created by the configurator:
public void AddProgressionToAchievement(int numSteps)
{
CP_AchievementSettings achievementSettings = CP_Social.socialSettings.AchievementSettings;
CP_IncrementalAchievementInfo info = achievementSettings.GetIncrementalAchievementInfoByUniversalName("TEST_ACHIEVEMENT");
_achievementService.IncrementAchievement(info, numSteps, (result) =>
{
Debug.Log("Increment result status: " + result.success);
Debug.Log("Achievement unlocked response: " + result.value.state + " " + result.value.currentSteps + " " + result.value.totalSteps);
});
}