Test Unlocking Achievements and Leaderboard Scores

QAing Time!

Let’s get to testing! This is the moment we cross our fingers, make another coffee, and hold our breath.

1. First, go to the Game Centre App on your device and LOG OUT if you are logged in on a non-sandbox user account. Don’t worry about logging in at this stage.

2. Build!

3. Wait……

4. Ensure the build to your device is successful and running

5. All going well, Game Centre would’ve automatically opened a log-in screen. Use one of your Sandbox Test User accounts that you set up earlier (nothing else will work).

6. Once logged in, check that you can see all available achievements for your App, as entered into iTunes Connect. So go ahead and click on your Game Centre Button now.

7. The game centre screen should open up. Your leaderboard will be empty at this stage. Click on the Achievements tab to ensure you can see all achievements. If you can’t, return to iTunes Connect and double check that your Bundle ID precisely matches what you have in your code.

8. If you can see all achievements, then congrats! That’s the first successful test.

9. Now click on the button you assigned to post a score to the Achievement board. Do that now.

10. Return to the Game Centre screen button and click to see if your score got posted. If not, again, check that your Leaderboard name in code matches what you submitted in iTunes Connect.

11. Final test… click on the Achievement button you set up. The Achievement Banner should appear on the top of the screen to signify the achievement has been unlocked. If not, rewind back again and check that your Achievement IDs in your code matches the Achievement IDs you submitted  iTunes Connect.

12. If the banner appears, return to the Game Centre screen one last time to make sure that the appropriate Achievement now appears as ‘unlocked’.

13. Now go back and click that VERY SAME button you used to unlock this achievement, and click again. The Achievement Banner should NOT appear. If it does… hm… contact us?

 

The End!

So that’s it! You have Achievements unlocking and scores posting to your Leaderboard! We hope this walkthrough has helped you, and please let us know if you think we’ve missed a step, or something doesn’t work for you – we’ll do our best to assist. And if you have improved methods, be sure to share!

Good luck to all of you for great App success!

If you’d like to send us a comment directly, please use the following form:

Accessing and Posting to Game Centre

1. Return to your  Game Centre “Button” script we asked you to open at the start of this How-To, as we’re about to launch Game Centre!

2. In your script, add the following:

//OPEN GAME CENTRE
GameCenterBinding.showAchievements()

3. Now when your Game Centre button is clicked, Game Centre will open. But don’t do that just yet, let’s move on….

4. For the sake of testing, let’s set up unlocking your first Achievement when the user clicks on another button. Select a quick-to-navigate-to button, that won’t load up a new scene, such as a Help button that you may have in your title menu. If you have no buttons, just make a fake one for now for the sake of testing Achievement unlocks

5. In the script that you use to activate the button in step 4, add the following line of code:

GCManager.ReportAchievement("YOURACHIEVEID");

Note that “YOURACHIEVEID” must be EXACTLY what you entered for the Achievement’s ID in iTunes Connect.

Alternatively, if you have an achievement that requires progress or a collection in order to unlock it (e.g. collect x items, and it shows the progress of those items collected, call the following:

GCManager.ReportAchievementProgressive("YOURACHIEVEID", 25.0f);
//Note the float number provided must have "f" next to it
//Where "25.0f is the percentage of this particular instance - i.e. it's 25% complete.
//e.g. If you have 100 items to collect in order to gain the achievement,
//in each instance when the player collects that item, you would call the above
//and insert the number "1.0". 

6.The above script will call on the method from the GCManager.cs script. Don’t test just yet… we want to set a score to the Leaderboard.

7. Now we’ll add in the call to update the Leaderboard with a new high score. If you don’t already have end-game functionality set up for your scores yet, then just choose some other random button that you can fire this score posting from. Add the following line to your end-game or button script:

GCManager.UpdateLeaderWithScore(thescore);

Of course “thescore” should be a LONG integer type. You can either just enter in any random number for now for the sake of testing, like “9999999”, or add the string variable that your score posts to. We’ll let you sort that bit out…

So that’s it! You’re all set-up and ready to go. That wasn’t so painful was it? 😛
Time to Test Unlocking Achievements and Leaderboard!

Prime31 Game Center Plug-in Set-up

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!

Prep iTunesConnect for Game Centre – Leaderboard and Achievements

For this How-To we won’t be focussing on multiplayer (that requires a small add-on to the GameCentre plug-in), nor on customised Leaderboards and Game Centre screens as we’ll be using Apple’s generic Game Centre screen.

For more information about this set up procedure, visit the iOS Developer Library.

At this stage, you’re now ready to set up your App’s Leaderboards and Achievements.

Prep Work

Before we do this, take five minutes now to draft up some achievements if you don’t have any prepped. Just three is enough to start with if you haven’t drafted any. It’s always good practice to keep this information as a section in your Design document or App Blueprint as we like to call it. We use a spreadsheet format, with the following headers:

  • Achievement Cat Number
  • Achievement Name (One column for each language: US/UK/Other languages)
  • Description (localised columns per above)
  • Hidden to User before Unlock? (Yes or No)
  • Release Version (We use for internal use only)
  • Game Centre Point Value

Achievement Cat Number: Use any format you like to catalogue your achievements. We use the format: ACH_ATAG_01_01 (where “ATAG” is a four letter tag acronym for our App). Note that once you set the Cat Number up in iTunes Connect, it cannot be changed. The only way to change it is to delete the Achievment – and you don’t want to be doing that once the game is live (in fact, I’m not sure you can even delete Achievements once live – will have to check on that)

Game Centre Point Value: You can assign points/values to each achievment unlock in any way you see fit for your App. For example, if the player enters the game for the first time you might have an achievement for that, for which you reward 5 points. On the other hand, more difficult to accomplish achievements may reward 40 points. It’s up to you how you distribute points on your achievements. However, keep in mind longevity. You have a maximum of 100 achievements totalling 1000 points to assign across ALL achievements. Don’t use them all up at once – keep points up your sleeve in case you want to release new Achievements in the future.

All other headers are self-explanatory. Keep in mind, too, that if you intend to localise(ze!) your App, that you’ll need localisations for the Achievement header and descriptions, as well.

Finally, you will need an icon to represent each achievement. They should be in the format, RGB 1024×1024 pixels.

Activating and Entering Achievements

1. Navigate to “Manage Your Apps” from the iTunes Connect home page

2. You will see your list of Apps – select the App for which you want to set up Game Centre.

3. At the App Home Screen, select Manage Game Centre, then Enable for Single Game and finally switch to “Enabled”

(C) Copyright the link owner

4. Once you’ve enabled, you’ll see a screen like this:

(C) Copyright link owner

To set up Leaderboard, click “Set Up”, and same for “Achievements”. So go ahead and click on both now.

5.  From here, it’s quite straight forward to add your leaderboard and achievements, using the table we asked you to create at the start of this post. If you require details for each element of the achievement set-up or leader board parameters, visit the iOS Developer Library page for Game Centre.

Once you’ve entered in your Achievements and set up your Leaderboard, you’re now ready to head back into Unity to Setup the Prime31 Plugin. Have a coffee ready!

Prep iTunesConnect for Game Centre – Sandbox Users

Before you do anything in Unity, we suggest you stop to spend time setting up your App in iTunes Connect. If you haven’t got your App registered, then do so now. If you’re not sure how to do this, we’ll be writing a blog page for that soon. In the meantime, please visit the Apple Developer website. When your App is set-up on iTunes Connect, you’re ready to go…..

a. Create a few Sandbox Test Users

i  On the “Opening Page” of iTunes Connect, after you log in, select Manage Users

ii. Select User type: Test User

iii Add new users

Make up any details you want for your Sandbox Tester User Accounts.
You can also make up the email address – e.g. cool@diddlysquat.com… Just make sure you remember the password you set! And choose the appropriate local store, too.

v Repeat steps iii – iv above until you have a few users to play and test with

Now you’re ready to move to the next step in prepping iTunes – setting up your Achievements and Leaderboard