Monday 17 February 2014

Technical Post 1: Integrating Facebook into unity C# Part 1

Declaration: I'm sure this may not be the best way to do this, but I hope that I can clear up how to do this for others, so as to save others time.

Whilst making a game in unity for mobile devices, it became apparent that we would need to add in Social media integration to allow users to share the game and post their high scores, this seemed to be harder than it originally looked, hesitant to fork out $65+ on the prime[31] plugin we looked for a cheaper alternative. We have heard good things about the simplicity of prime[31], but with a small budget we were sure we could find an alternative.

After much searching and research, we found an all inclusive package that seemed to be able to do the job we needed, we had bought the UniShare package from the asset store for $35 (Link to Asset Store). Whilst this is a very easy plugin to use, where once you have set the prefabs to contain your app ID, app secret and a callback URL, you can easily call the prefab to log in, share and log out, the three things we were originally looking for.

However, there was one problem, we wanted the user to have confirmation they had shared through a window from Facebook or Twitter which allowed the player to enter their own messages and share them at the click of a button. This may be present in the UniShare package, but with the lacking documentation, we were wasting too much time trying to find the solution that we had to find another.

The UniShare package is great for value, if you need a simple, cheap system to share and post to the users' social media, where it supports all the major networks, but the incomplete documentation makes it quite stressful to use.

As we tested the game, we saw players were able to share their scores but not compare them with their friends, so we clearly needed a new system. We decided to drop Twitter from our game as a way of sharing, there is currently a link to the company Twitter account if there is need for it, but we noticed the majority of games on the market don't provide a Twitter share for your actions. So now we are only left with Facebook, and after a little searching we downloaded the free Facebook SDK (Link) after signing up as a Facebook developer and verifying the account with a mobile phone.

Now came the hard part, we had a working system with UniShare that we took apart and destroyed to replace with a new system that had equally as bad documentation. After searching for answers it seemed that others were struggling with the documentation with little success, now as we are starting to get ours to work, I am going to post some code that works (in 4.3.6) so that it may help others that are struggling.

I have provided two links below gave very useful information used for Initialising the app, logging in and out and sharing to Facebook, check these before heading into the score boards and posting scores to Facebook. 
First Video: iOS Setup 
Second Video: Android Setup

Now we have the player being able to log in, log out and share their scores to their wall (can even challenge friends if you want to implement that), we needed to look at getting data from Facebook itself. We started with getting the user name.
Note this is in C#
It is important before we start to add this before the class declaration
using System.Collections.Generic;
using Facebook.MiniJSON
This allows us to use all the code for Facebook with minimal errors, now we look at getting the name using a HTTP GET request. and the Facebook SDK has made this simple, where to get data from a specific URL from Facebook we can use this code
public void GetUserName()
{
    FB.API("me?fields=name", Facebook.HttpMethod.GET, Callback);
}

void Callback(FBResult result)
{
    nameFound = true;
}

Where we call the GetUserName() method at some point in our code, and this uses the FB.API call to Facebook with the parameters "URL", "Request type"(GET, POST, DELETE), "Callback Method"
Upon successful get request the Facebook SDK calls the Callback method as specified and that can print the name or change text to say hello [+ their name] to the player.





No comments:

Post a Comment