A reminder that these plug-ins only work for Unity3D Pro. Also, please excuse the formatting of the code provided. WordPress doesn’t like code 😉
If you still haven’t purchased it, head over to their website now.
(DISCLAIMER! Note that we are in no way affiliated with Prime31, we just use nearly all of their plug-ins! This guide is only intended for instructional purposes. If you have any technical issues, please contact the plug-in owner or Unity.)
From here on we won’t be showing screen grabs of what to do. First, because we want to get this process down on e-paper first, and we may or may not return later to add pretty pictures. We’re big believers in visual aids such as photos, when demonstrating something, so if you really, really need them, then say so! Secondly, we’re assuming that you know Unity, live and breathe Unity, and if you’re at the stage of setting up Game Centre for your app, should know your way around. If you don’t, we suggest you visit the Unity3D website for their wonderful tutorials. Otherwise, read on…
We’ll keep these steps as basic as possible…
Step-by-Step Set-Up
1. Import the Prime31 Game Center Package to your project.
2. We recommend (out of habit) getting latest on the plug-in by navigating to the Prime31 Menu item at the top of screen and selecting “Update”.
3. Perform a NEW FRESH BUILD to implement the system into X-code. Do NOT do an append build (e.g. Control/Command B). Yes, this will mean if you have localisation strings in your X-code build you will have to insert them again after this step. So BACK UP your localisation strings. This is another area fit for another How-To page that we may set up later, but you should always have a “Localisation Register” for your App in spreadsheet format…. We have a special one we use with sweet formulas that format our text exactly how it needs to appear in the String files… more on that another day.
4. When the build is done, navigate to the Scene where you want Game Centre to appear. Usually a user will have to click on a button/link to open Game Centre. Open up the script you use for this button/link’s functionality. We’ll call this your “Button Script”.
5. In your project, we highly recommend setting up (if you haven’t already) a blank scene that is the FIRST TO LOAD. You will need a small script in this blank scene that navigates to the opening scene of your app (e.g. the title) straightaway. This opening blank scene will contain your Game Centre scripts we’re about to make. In our project, it also contains prefabs for our enemy pooling system we use in-game, so that they are loaded from app log-in. Of course, these and your game centre scripts, will need to call the method/function: DontDestroyOnLoad.
6. We created two scripts in C#: “GCManager.cs” and “GameCenterEventListeners.cs”. The Listener script we grabbed from the Prime31 demo folder. You’ll find this folder in Plug-ins > GameCenter. Save your GCManager.cs and Listener.cs scripts to the Plug-ins > GameCenter folder.
7. Open up GameCenterEventListeners.cs and add the following code adjustments to the script:
void playerLoggedOut()
{
GameCenterManager.achievementsLoaded -= achievementsLoaded;
Debug.Log( "playerLoggedOut" );
}
void reportAchievementFinished( string identifier )
{
Debug.Log( "reportAchievementFinished: " + identifier );
//Update the list on completion of the successful achievement....
GameCenterBinding.getAchievements();
}
The above adjustments will ensure that upon the event of a player logging out of Game Centre (via the Game Centre app on the device), that their achievement data is removed. When another player logs IN to Game Centre, the event will trigger an update to the achievement list according to what that player has unlocked. Note that getAchievements() only grabs those achievements that have been unlocked by the player.
8. Add this GameCenterEventListeners.cs script to a gameObject in the scene.
9. Create a new CS script and name it whatever you like, but for clarity we suggest something like “GCManager.cs”. Save this script to the Plug-ins > GameCenter folder.
10. Open the script and enter in the following
(Note: for C# users, don’t forget that your CLASS name should be the same as the script name – Unity will yell at you anyway if you don’t do that):
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Prime31;
public class GCManager : MonoBehaviour {
#if UNITY_IPHONE
public static string playerName;
public static long playerHighScoreGC;
public long _score;
public static bool idFound;
public static List<GameCenterAchievement> _achievements;
// Initialise Game Centre
void Awake ()
{
GameCenterBinding.isGameCenterAvailable();
}
void Start()
{
if (GameCenterBinding.isGameCenterAvailable() )
{
GameCenterBinding.authenticateLocalPlayer(); //Must Have
GameCenterBinding.showCompletionBannerForAchievements(); //Display banners on Achievement Unlocks...
GameCenterBinding.loadReceivedChallenges();
}
//Listener
GameCenterManager.achievementsLoaded += achievementStatus =>
{
_achievements = achievementStatus;
};
}
// Update the GC Leaderboard. The leaderboard will determine if the score is higher than what's already been previously posted and will only post a new high score automatically, thereby stopping the achievement banner from reappearing if subsequent repeats of achievement parameters are fulfilled.
public static void UpdateLeaderWithScore (long myScore)
{
GameCenterBinding.reportScore(myScore, "YOURLEADERBOARDID");
Debug.Log ("New Score reported to GC");
}
// Update the GameCentre with a single parameter new achievement
public static void ReportAchievement(string id)
{
if (_achievements !=null)
{
foreach (GameCenterAchievement i in _achievements)
{
idFound = false;
//search for achievement id in the list
if (!idFound)
{
if ( i.identifier == id)
{
Debug.Log ("Achievement already unlocked "+i.identifier);
idFound = true;
break;
}
}
}
}
//First achievemeent ever to be unlocked or achievement id not in the list of unlocked achievements
if (_achievements == null || !idFound)
{
Debug.Log("Unlocked Achievement: "+id);
GameCenterBinding.reportAchievement( id, 100.0f );
}
}
// Called for achievements where progress to completion occurs over time (e.g. collection of x items)
public static void ReportAchievementProgressive(string id, float progress)
{
if (_achievements !=null)
{
foreach (GameCenterAchievement i in _achievements)
{
idFound = false;
//search for achievement id in the list
if (!idFound)
{
if ( i.identifier == id && i.percentComplete == 100.0f)
{
Debug.Log ("Achievement already unlocked "+i.identifier);
idFound = true;
break;
}
else if (i.identifier == id && i.percentComplete < 100.0f)
{
float idProgress = i.percentComplete;
float updatedProgress = 0.0f;
idFound = true;
if (idProgress+progress > 100)
{
updatedProgress = 100.0f;
GameCenterBinding.reportAchievement(id, updatedProgress);
Debug.Log ("Progressive Achievement is now Complete! Check: "+(updatedProgress)+" percent");
break;
}
else
{
updatedProgress = idProgress + progress;
GameCenterBinding.reportAchievement(id, updatedProgress);
Debug.Log ("Progressive Achievement is now: "+(updatedProgress)+" percent complete!");
break;
}
}
}
}
}
if (_achievements == null || !idFound) //First achievemeent ever to be unlocked or achievement id not in the list of unlocked achievements
{
Debug.Log("Unlocked Achievement: "+id);
GameCenterBinding.reportAchievement( id, progress );
}
}
//Reset this profile's Achievements. Throw a button in your scene if you want your player's to have the ability to reset all of their Achievements.
//Also handy to have for testing purposes.
public static void ResetAchievements()
{
GameCenterBinding.resetAchievements();
}
#endif
}
11. In the script above, replace : “YOURLEADERBOARDID”.. with the ID of the Leaderboard that you created in iTunes Connect.
12. Add the GCManager.cs script to your Game Centre gameObject in the scene where the Listener script is attached.
13. Remember to add the DontDestroyOnLoad() function to one of the above scripts, or a separate script if you prefer, and attach it to your Game Centre gameObject in the scene
14. Now check your PlayerSettings in Unity to ensure that you have the EXACT Bundle Identifier as in iTunes Connect for your App, and the correct Bundle Version Number as submitted in iTunes Connect. Not doing this is guaranteed to cause you grief!
Note that the above GCManager script will ensure that once the Achievement Banner is displayed for the INITIAL unlock of an Achievement, it will NOT display again if the parameters of that Achievement are met again.
This concludes the initial Set-Up of the Game Centre Plug-in. Now we’re on to Accessing and Posting to Game Centre, then finally, Testing!