Pixelshot Games

Making making from around the globe

Confusion with Unity Social API

GeneralCarleton DiLeo

I’ve been working on integrating features like a leaderboard and achievements into Wordsum. Instead of building my own systems or using third party services, I decided to use native services like Game Center for iOS and Google Play Services for Android. Unity provides a nice abstraction for these layers in the form of the Social (http://docs.unity3d.com/ScriptReference/Social.html) API. The main purpose of this post isn’t for me to talk about how great the Social API is but to share a misunderstanding I had with it's implementation.

After integrating Game Center into my game, I noticed some weird behavior. When I suspended the game and resumed on iOS the game would go back to the main menu. After much debugging I realized two things were happening.

Game Center re-authenticates on resume

If the users suspends your game and then loads the game from the multitasking tray, Game Center re-authenticates. This would be find if the following wasn’t true.

Callback provided when authenticating is resused

When you authenticate with Game Center you call the following:

Social.localUser.Authenticate(success => {
 //Doing some stuff
 Application.LoadLevel("MainMenu");
});

You provide a callback you want to invoke after the user has authenticates. I'm my case, I was attempting to load the Main Menu scene. The idea was to wait until the player authenticated or did not to load the main menu. This didn't work so when when the player resumed their game, authenticated automatically and loaded the main menu. Once I changed this my problem disappeared.

Summary

My writeup isn't meant to discourage you from using the Social API. It provides a great abstracted layer to work with. Unfortunately for me, I assumed that the callback I provided was going to be used once and then discarded. In my opinion, using C# Delegates would have been a clearer choice to store an action to run every time a player authenticates.