How to add Game Center in Unity3D

One thing I tend to do when I'm learning to code something or try to use a specific API is to try and make it simpler to use than it already is. Now, Unity's Social API with Gamecenter you can get up and running in a couple of minutes already because its so easy to use - but I had some different goals with my GameCenter implementation than that.

I wanted to report an achievement from anywhere in my code in only one or two lines of code. I decided to use the singleton pattern for that so that I could access my instance of GameCenter/Social from anywhere i wanted.

This code below is the only lines you will have to use to report an achievement to apple. (If you added your achievements correctly in itunesconnect.apple.com first).

#if UNITY_IPHONE && !UNITY_EDITOR

GameCenterSingleton.Instance.ReportAchievementProgress("ACHIEVEMENT_ID", 100.0f);

#endif

I'm going to explain how i coded my implementation of GameCenter. Explanations of how different functions and methods are used you'll find in the Unity Documentation.

In the beginning of my class I started with the singleton pattern and initialization function.

This code is pretty straight forward. I only check if there is an instance available in the static function and returns it. If there isn't one available, I create one and initializes it before returning it. In the Initialize() function I only Authenticate the user with Game Center and pass in a function(ProcessAuthentication) that's being called when the authentication is complete.

The first functions here are only helper functions to check if the user is authenticated and functions to show Leader boards and achievement UI. 

Then there's a function called AddAchievementProgress. This function is used if you want to add only a percentage of progress to an achievement you already started. It calls a function to see if you started that particular achievement before and adds your percentage to it's own, If you haven't started on it - then just report it as new.

This function is not very different from the one documented in the Unity3D documentation, it checks whether or not the achievement is already done before it reports the achievement. If it succeeds, then load all the achievements again to save it into your started achievements.

These are helper functions that are being called when loading and authentication processes are being done except the ResetAchievements()-function, which calls the Social API to reset all and then we send in a function for it to call when it's done.

This function loops through all the completed and started achievements and sends back a boolean if it's complete or not.

Gets an achievement by name. The other function is being called when the ResetAchievement process is done.

That's it! I recommend to initialize the class in the beginning of your game to get the initialization going right away, preferrably right when you start the game.

#if UNITY_IPHONE

GameCenterSingleton gcSingleton;

void Start () 

{

//This initializes gamecenter

gcSingleton = GameCenterSingleton.Instance;

}

#endif

Please ask questions, comment and share your thoughts below.

/Niklas

Lead Programmer/ Chairman 

It's available for download here:

GameCenterSingleton.cs 

EDIT:

If you would like to make this code even easier to use - you can put the #if UNITY_IPHONE && !UNITY_EDITOR part in the GameCenterSingleton class itself.

That way you can reduce the lines of code when you use it from 3 to 1. Thank you Jakob Hillerström for this improvement. See the first comment below for more information. 

EDIT:

If you would like to make this code EVEN easier to use:P - you can put the platform check in the IsUserAuthenticated method and return false if your not in UNITY_IPHONE.

Then you can use the code in the project(with 1 line of code) and all the other functions without having to check the platform all the time. The system just disregards it as not being logged in to Game Center.

For more information, take a look at the comment from Charles below. Thank you Charles for this improvement!