BLACKISH DEV BLOG Support
 

 

Unity: GameCenter Achievements

Starting with version 3.5, Unity supports GameCenter out of the box! Here’s how to put GameCenter Achievements in your game:

1) Log into iTunes Connect and set up all your achievements. Make sure you have a name, a pre/post description and 512×512 images ready to upload.

2) In Unity you need to start up GameCenter – do this as soon as possible in the game! Code adapted from the docs:

void Start () {
	Debug.Log("SocialHelper: Trying to start up");
	Social.localUser.Authenticate(ProcessAuthentication);
}

// This function gets called when Authenticate completes
// Note that if the operation is successful, Social.localUser will contain data from the server.
void ProcessAuthentication (bool success) {
    if (success) {
        Debug.Log ("Authenticated, checking achievements");
        // Request loaded achievements, and register a callback for processing them
        Social.LoadAchievements (ProcessLoadedAchievements)
    } else
        Debug.Log ("Failed to authenticate");
    }

    // This function gets called when the LoadAchievement call completes
    void ProcessLoadedAchievements (IAchievement[] achievements) {
    if (achievements.Length == 0) Debug.Log ("No achievements found"); //This only means that the user has not earned any yet!
    else Debug.Log ("Got " + achievements.Length + " achievements");
}

3) Each time the player earns an achievement, do something like this:

IAchievement achievement = Social.CreateAchievement();
achievement.id = "AchID001"; //This is the id you had to set up for each achievement in iTunes Connect...
achievement.percentCompleted = 100.0; //progress as double
achievement.ReportProgress(result => {
if (result) {
    Debug.Log ("Successfully reported progress");
} else {
    Debug.Log ("Failed to report progress");
}});

4) Log out of GameCenter – then build it, run it on your device. The GameCenter-Login window will pop up – make a new account. It should say “Sandbox” on the top left of the screen because this will be your test-account. Once you’re done it should report how many achievements you have in the console: “Achievements loaded 0, No achievements returned, No achievements found” (This is what it should read the very first time before you’ve earned any achievements) NOTE: If you’re not getting the Sandbox watermark and achievements can’t be loaded, make sure you’re not building with a wildcard-provisioning-profile (com.yoursite.*) – those won’t work with GameCenter! Best delete the app and build again with the correct Provisioning Profile.

5) Go earn an achievement and have a look at the console: “Looking for AchID001, cache count is 0, Reporting achievement AchID001, with 100.000000, Successfully reported progress” – Yay!

6) Start the game again through XCode and once it’s logged in it will now report the earned achievement in the console! “Achievements loaded 1, 2012-02-14 18:04:56.543 yourgame[20000:707] Nr 0: AchID001 – 1 – 2012-02-14 16:41:16 +0000, Got 1 achievements”

7) Now you just need to add a way for the player to get to the Achievements screen in GameCenter and you have the most basic stuff working!

Social.ShowAchievementsUI();

And that’s it! But beware: I haven’t actually submitted the update to Apple yet.

Anyway, here’s a sneak-peak of NEW ORBIT‘s Achievements in GameCenter!

Bookmark the permalink. Follow any comments here with the RSS feed for this post. Post a comment or leave a trackback: Trackback URL.

2 Comments

    Hizral on March 7, 2012 at 09:36 | Permalink

    How do you set the look inside GameCenter. Do you have to set it or it will set automatically how the look will be?

    col000r on March 7, 2012 at 10:11 | Permalink

    You can’t change the look of the default GameCenter view (except for the achievement icons of course), you can however just gather all the data and use it to create your own GUI in Unity. For NEW ORBIT I’m using the GameCenter view for iOS and I built my own solution for the PC/Mac version…

One Trackback

  1. By Unity3D Links on July 24, 2013 at 17:37

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>