Monday, April 21, 2014

Facebook + Unity + Parse


Recently I got some time to do some tests about how to communicate among Facebook, Unity and Parse. It worked quite well. Above is a diagram I made to demonstrate the relation among them.

For Facebook, we need to create an app first which would come with an App ID and Name. For Unity to communicate with Facebook, it has to import Facebook SDK for Unity package. Once Unity knows what Facebook App ID and Name is via Facebook setting in Unity, the link between them is settled.

When player log in the app as a Facebook user, the app would execute FB.LogIn. To get user profile info, use FB.API with HttpMethod.GET, the callback function will give us FBResult which is in JSON format that can be easily parsed. FB.Feed is for posting on wall for sharing. FB.AppRequest is for friend invitation. FB.API with HttpMehod.POST can be used to have custom activity post.

To communicate between Parse and Unity, we need to create a Parse App first. For Unity, we have to put Parse.Unity.dll into plugins folder. With Parse-given script, copy and paste Parse Application ID and .NET Key into Unity. Once player log in as a Facebook user, ParseFacebookUtils.LogInAsync would help save user data into Parse DB. Retrieving data back from Parse is also quite straightforward, just use ParseObject.GetQuery with given constraints. To change DB data, use ParseObject.SaveAsync.