Wednesday, September 24, 2014

Facebook API Example with Unity

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.