I integrated one of my games (Death Arena) with Facebook today. I encountered a few difficulties along the way and, although there was information online, it took me a while to get it working properly. The code examples I found online didn't work without modification (I had to combine bits and pieces from various sources), so I thought I'd share my Facebook wrapper class in case others find it useful.
The class is pretty simple at the moment and only caters for the basics -- connecting to Facebook and posting a message to the user's wall / timeline. It takes in a heading string, a main message string as well as a placeholder name string.
For example, one might choose to use 'Bob' as a placeholder name. When calling the PostMessageToFacebook() method you could pass in a message of the form "Bob kicked ass in xyz game" and then pass in 'Bob' as the placeholder name. Once it logs onto Facebook and retrieves the user's first name, it will replace all instances of 'Bob' with the real name.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using Facebook.MiniJSON; | |
using System.Collections.Generic; | |
// Example of useage: | |
// FacebookWrapper.PostMessageToFacebook( "Bob kicked ass", "Bob really is an impressive human being", "Bob" ); | |
class FacebookWrapper : MonoBehaviour | |
{ | |
// Public methods. | |
//----------------------------------------------------------------------------- | |
public void PostMessageToFacebook( string heading, string message, string placeholderName ) | |
{ | |
m_heading = heading; | |
m_message = message; | |
m_placeholderName = placeholderName; | |
BeginInitAndLoginProcedure(); | |
} | |
// Private methods. | |
//----------------------------------------------------------------------------- | |
public void BeginInitAndLoginProcedure() | |
{ | |
if ( FB.IsInitialized ) | |
{ | |
InitComplete(); | |
} | |
else | |
{ | |
FB.Init( InitComplete, c_appId ); | |
} | |
} | |
private void InitComplete() | |
{ | |
if ( FB.IsLoggedIn ) | |
{ | |
PostCurrentMessageToFacebook(); | |
} | |
else | |
{ | |
FB.Login( "", LoginComplete ); | |
} | |
} | |
private void LoginComplete( FBResult result ) | |
{ | |
if ( result.Error == null ) | |
{ | |
Debug.Log( "Facebook success in LoginComplete." ); | |
FB.API( "me?fields=first_name", Facebook.HttpMethod.GET, AcquiredNameComplete ); | |
} | |
else | |
{ | |
Debug.Log( "Facebook error occured in LoginComplete: " + result.Error ); | |
} | |
} | |
private void AcquiredNameComplete( FBResult result ) | |
{ | |
if ( result.Error == null ) | |
{ | |
Debug.Log( "Facebook success in AcquiredNameComplete." ); | |
var dictionary = Json.Deserialize( result.Text ) as Dictionary< string, object >; | |
Debug.Log( "About to query dictionary." ); | |
m_userName = dictionary[ "first_name" ].ToString(); | |
Debug.Log( "User name is " + m_userName ); | |
PostCurrentMessageToFacebook(); | |
} | |
else | |
{ | |
Debug.Log( "Facebook error occured in AcquiredNameComplete: " + result.Error ); | |
} | |
} | |
private void ReplacePlaceholderNameWithUserName() | |
{ | |
m_message = m_message.Replace( m_placeholderName, m_userName ); | |
m_heading = m_heading.Replace( m_placeholderName, m_userName ); | |
} | |
private void PostCurrentMessageToFacebook() | |
{ | |
ReplacePlaceholderNameWithUserName(); | |
FB.Feed( "", | |
c_appLinkUrl, | |
m_heading, | |
m_message ); | |
} | |
// Private fields. | |
//----------------------------------------------------------------------------- | |
private string m_heading; | |
private string m_message; | |
private string m_userName; | |
private string m_placeholderName; | |
// Constants. | |
//----------------------------------------------------------------------------- | |
private const string c_appId = "123456789012345"; | |
private const string c_appLinkName = "Check out Death Arena"; | |
private const string c_appLinkUrl = "http://www.lunarpulse.com/DeathArena.htm"; | |
} |