Accessing Facebook within Unity
I’ve been struggling lately to get Facebook to work inside my Unity game as I wanted to use the new Graph API and the compiled .dll had to be .NET 2.0 to work within Unity. There was also the issue of finding a good JSON library that worked with .NET 2.0 and the fact that Mono had issues accessing https via the System.Net library…
There are a lot of ways to access Facebook if you are building a game for the web player but it’s difficult if your game is desktop based as you have to somehow get the user to login to Facebook and authorize your application, this is difficult in Unity as it’s WWW class doesn’t have enough control over it nor does the launch application. I couldn’t find a solution that existed so I’ve come up with something that works perfectly, probably not-as elegant but it is a good trade-off.
The above two downloads should explain all but basic usage is:
- Compile the above VS project and copy the FacebookAPI.dll and JsonFx.Json.dll in to your Assets\Plugins folder.
- You can now use it in a C# script within Unity – refer to the official C# Facebook API
- ???
- Profit!
Here’s an example C# script that you can use within Unity:
using UnityEngine;
using Facebook;
public class UFacebook : MonoBehaviour
{
public static string MagicToken;
public static string GetUserProperty(string user, string prop)
{
try
{
FacebookAPI api = new FacebookAPI(MagicToken);
Facebook.JSONObject request = api.Get("/" + user);
return request.Dictionary[prop].String;
}
catch
{
return null;
}
}
}
You can then do UFacebook.GetUserProperty(“eth0izzle”, “name”); in a C# or even a JavaScript file – easy
Of course this is very basic class and can be extended for your needs.
To authenticate the user and access the API via OAuth you will need an OAuth access token which is returned in the redirect_uri as a hash tag once the user authorises your application. The issue here is how do we get the access_token back into our Unity game? Technically you can’t so we need some user intervention…
In your game project use Application.OpenURL("https://graph.facebook.com/oauth/authorize?client_id=147305235313757 when the user clicks “Facebook connect”, for example. Our redirect_uri is where the magic happens, all we do is grab the access_token from the URI with some JavaScript:
&redirect_uri=http://www.ifc0nfig.com/Facebook/UnityGame/
&type=user_agent&display=page");var access_token = window.location.hash.split('&')[0].replace('#access_token=', '');, print it to the screen and ask the user to paste it back into the Untiy game – you can take a look at the HTML source code for a better example and don’t be shy to look at how I’ve implemented this.
So what could we do with this? A game that randomly spawns AI enemies with the names of your Facebook friends and their profile picture as it’s face. A game that keeps track of the users Places and updates the game location accordingly. A game that reads what movies, books and music you like and then displays relevant ads in-game – the possibilities are endless, good luck!
Related posts:
- Dear Java, I hate you – accessing the Facebook API with Java
- Facebook Application – Call of Duty 5 Statistics
10 Responses to “Accessing Facebook within Unity”
Leave a Reply



steffan on February 3rd, 2011
Hi man
Thx for tutorial- it’s really cool, but only in the unity. Cause when i’m trying create the exe file from that project it’s shows me the error:
”
ArgumentException: The Assembly System.Web is referenced by FacebookAPI. But the dll is not allowed to be included or could not be found.”
Maybe u know what’s wrong ?
Bless ya.
Paul Price on February 10th, 2011
Hi Steffan, did you compile the project to .NET 2.0, and place the two .DLLs in the correct directory?
mi-go on February 21st, 2011
@steffan:
go to menu: Edit > Project Settings > Player. On “Other settings” set “.NET 2.0″ instead default “.NET 2.0 Subset”.
Sargo Darya on May 25th, 2011
Just wanted to mention that you can do it a bit easier when using a canvas application. Use SendMessage from JS and ExternalCall from within Unity3D to request and send tokens.
eth0 on May 27th, 2011
Hi Sargo, do you have an example? Canvas isn’t supported in IE so it’ll be a slight issue for them..
Penny on June 23rd, 2011
Hi Paul,
I’d like to reference this tutorial in the book I am writing about Unity. Can you email me directly so I can give you more details?
Thanks
Penny
Paul Price on July 1st, 2011
Hi Penny, that sounds great. You didn’t seem to leave an e-mail address though; you can e-mail me at eth0@ifc0nfig.com
Regards, Paul.
Philipp on September 7th, 2011
Hey Paul,
I’m not writing a book, but my bachelor thesis. Just wanted to tell you, that I’m going to reference this tutorial there too
(only if its ok ^^)
And i added it to my favourites, so i can implement it into my game as soon as the thesis is finished
Thank you very much!
Philipp
Erbomba on September 20th, 2011
Im using some aproach from it, and is workign perfect on Unity3D but not when I build it. Same happens when I use your project, directly.
On the other hand, what if we add the Facebook.cs directly from git, Unity3D complains about the System.web…
kiran patel on January 16th, 2012
how to post on facebook’wall of a user??