Search Unity

Unity Facebook (using Graph Api) example + documentation

Discussion in 'Made With Unity' started by Ethan, Dec 24, 2010.

  1. Ethan

    Ethan

    Joined:
    Jan 2, 2008
    Posts:
    501
    Hey guys,

    as you can read in the topic i am selling an Unity Facebook example + documentation for setting up your own Unity game in Facebook in less than 5 minutes.

    For this purpose (and for some artist buddies) i set up http://www.polyload.com for selling our stuff a few weeks/month ago. It's now in a kind of beta stage, so please feel free to contact me if you find bugs or strange behaviours.

    Get it here: http://polyload.com


    Features of the example i am providing with full source:

    - Display your facebook picture and name in your Unity3d webplayer app
    - Post to your wall by pushing a Unity GUI Button
    - Using new Facebook Graph API php-sdk (not the old Rest API)
    - Unity3d 3 compatible

    You can preview the result here:

    http://apps.facebook.com/unity_example/

    To extend the example you just have to find the right Graph Api calls from here and insert them on the right spots in the example source:

    http://developers.facebook.com/docs/api

    Feel free to post your feedback! And tell me if sth. is unclear or doesn't work oooor could be improved.

    Thanks Merry Christmas,
    Ethan
     
    Last edited: Dec 24, 2010
  2. Qvist

    Qvist

    Joined:
    Dec 2, 2010
    Posts:
    76
    I might want to buy this for a school project. But before i do so, I have some questions:
    Is it possible to invite friends?
    Is it possible to send gifts?
    Is it possible to run the scripts without having the webplayer in the facebook canvas page?
    Im getting some issues in reading non english letters(im from Denmark), could that be fixed?
    If we are going to buy it, and you make further development on it, would we be able to get the updates for free?

    Beside all that I just want to tell that it's a really awesome project you have been working:D I have been looking for something like this for a while.
     
  3. Ethan

    Ethan

    Joined:
    Jan 2, 2008
    Posts:
    501
    Hi Qvist,

    you can do everything what the Graph Api is capable of, just in the same way i loaded the userpicture or posted to your wall.
    The example uses Unity's WWW class to call Graph Api URLs or it's own php scripts, which contain Graph Api stuff, too.
    You don't have to mind about the authorization and Graph Api setup - that's already done. Just place your Graph Api calls.

    Here is a simplified example how to get your own picture (the used Graph Api call is in bold):
    Code (csharp):
    1.  
    2. // we get m_myFacebookUserId via external javascript functions from index.php
    3. var www : WWW = new WWW ("[B]https://graph.facebook.com/" + m_myFacebookUserId + "/picture[/B]");
    4.    
    5.    // Wait for download to complete
    6.   yield www;
    7.    
    8.   var newTextureFromWeb : Texture2D = www.texture; // here you get the picture as texture
    You can find more Graph Api calls for all your needs here:
    http://developers.facebook.com/docs/api

    Hope this helped you :)
    Regards,
    Ethan
     
  4. Qvist

    Qvist

    Joined:
    Dec 2, 2010
    Posts:
    76
    Hi Ethan
    Thanks for the quick response!
    Then i want to hear about the letter issue. Can that be fixed? Need these letters: æøå.
    I like coding in C#. I wont get some issues when using the Graph API?
    These might be stupid questions, but i'm not into all unity stuff yet:D

    Best Regards
    Qvist
     
  5. Ethan

    Ethan

    Joined:
    Jan 2, 2008
    Posts:
    501
    The letter stuff is just about the right charset. If you want to display those chars in html you have to find the right shortcut to the char (see this table: http://htmlhelp.com/reference/html40/entities/latin1.html). If you want to display such chars in Unity you have to pick the right charset in your codeeditor.

    It works with C# or JavaScript, there's no difference. :)
     
  6. Qvist

    Qvist

    Joined:
    Dec 2, 2010
    Posts:
    76
    Thanks for the answer:D

    I will buy it in about a month or two:D
     
  7. cupsster

    cupsster

    Joined:
    Apr 14, 2009
    Posts:
    363
    interesting aproach on that WWW.texture call... however I done it trough regular url.. My API is little more complicated but works just right.. nice work btw
     
  8. Ethan

    Ethan

    Joined:
    Jan 2, 2008
    Posts:
    501
    cupsster: Thanks ;)

    Qvist: Great! I hope it will help you.
     
  9. eyalfx

    eyalfx

    Joined:
    Oct 8, 2010
    Posts:
    108
    Hi Ethan,
    I bought the package and it was super easy to implement.
    I Just followed the instructions and got it all working in no time.
    I'll keep you posted as I make some progress on my game.
    Thanks for putting it out there ;)
     
  10. dotmos

    dotmos

    Joined:
    Jan 2, 2011
    Posts:
    41
    Hi,
    i started working on a fb app some months ago.
    Some weeks ago i wanted to finish the app, port everything to the new fb api and then stumbled upon your handy little script. After 3 hours i had everything ported and working! Thanks for that! :)

    I can only recommend this to everyone interisted in connecting unity and facebook. It's a great kick-start script ;)

    The only critique i have is that the php code could be a bit more organized. (Important variables are scattered over multiple files. One can easily change that himself though). At the end of the day the 18€ were very well worth it though.

    A big timesaver, thanks!

    Link to fb-app ported to the "new" api using ethan's prefab: http://apps.facebook.com/messagemadness
     
    Last edited: Jan 2, 2011
  11. Ethan

    Ethan

    Joined:
    Jan 2, 2008
    Posts:
    501
    thank you guys :)
     
  12. eyalfx

    eyalfx

    Joined:
    Oct 8, 2010
    Posts:
    108
    Hi guys,
    Were you able to access any other features of the Graph API besides profile picture and name.
    I'm trying to access photos or albums , but no luck so far.
    Any idea how to do that?
    Thanks,
    E
     
  13. Ethan

    Ethan

    Joined:
    Jan 2, 2008
    Posts:
    501
    Maybe you missed to get some more permissions at startup? In the example the app just asks for post-to-own-wall permissions.
     
  14. eyalfx

    eyalfx

    Joined:
    Oct 8, 2010
    Posts:
    108
    That's the part that I actually did figured out how to do. So I did ask for all the permission, just not sure how to write the functions to grab the photos.
     
  15. Ethan

    Ethan

    Joined:
    Jan 2, 2008
    Posts:
    501
    hey eyal

    sorry for the late answer :)

    I found a link which should help you:
    http://developers.facebook.com/docs/reference/api/photo/

    I would guess it should work like this:

    Code (csharp):
    1. $photo = $fb->api('/98423808305'); // id of photo - see link above
    2. echo '<img>'. $photo[data]['picture'] .'</img>'; // $photo[data]['picture'] should contain the link to the .jpg
    maybe you have to leave the [data] away or sth (sorry, i cant test it atm).

    hope this helps anyway,

    cheers
    ethan
     
  16. seon

    seon

    Joined:
    Jan 10, 2007
    Posts:
    1,441
    Ethan, does this system work in a web player that is not a Facebook App? For instance, if the webplayer was running on my site, or kongregate etc... could I still allow my player to authenticate with FB and post to their wall?
     
  17. Ethan

    Ethan

    Joined:
    Jan 2, 2008
    Posts:
    501
    Hi Sean sorry for the late reply,

    unfortunately the facebook php-sdk always redirects to facebook, even if you call your url which is displayed in the facebook iframe.

    Cheers
    e
     
  18. lima3b

    lima3b

    Joined:
    Jun 24, 2008
    Posts:
    66
    I am feeling kind of stupid. I've bought the package, followed the instructions, uploaded the app, but when I try to access it, the browser keeps loading in loop but never loads the app. Any idea of what may I did wrong?
     
  19. lima3b

    lima3b

    Joined:
    Jun 24, 2008
    Posts:
    66
    Have anyone experienced the same problem? Ethan, can you give me some help, please?
     
  20. Ethan

    Ethan

    Joined:
    Jan 2, 2008
    Posts:
    501
    Hey lima,

    sorry i wasn't available for some days. The problem is, that facebook changed something in the settings, which forces you to do the following:

    1. Go to your developer page "My Applications"
    2. Select your app and 'Edit Settings'
    3. Advanced > Migrations > Enable the "OAuth 2.0 for Canvas (beta)"

    Maybe you should also update the facebook php-sdk (the facebook.php in facebook/src):
    https://github.com/facebook/php-sdk/
    But this wasn't necessary for my apps.

    Please tell me if this worked for you. I just updated the package in the AssetStore.

    Cheers
    Ethan
     
  21. MrBodean

    MrBodean

    Joined:
    Jan 25, 2008
    Posts:
    103
    I went to your example
    http://apps.facebook.com/unity_example/

    I see my pic and name but when I click the button to post to wall, nothing happens and there is nothing on my wall. This still working?
     
  22. Ethan

    Ethan

    Joined:
    Jan 2, 2008
    Posts:
    501
    Hey MrBodean,

    i just tried the unity3d-example and it posted to my wall without any problems.
    Maybe facebook lagged in some way? Can you try it again and tell me if it's still not working?

    Thanks Cheers
    Ethan
     
  23. MrBodean

    MrBodean

    Joined:
    Jan 25, 2008
    Posts:
    103
    Yeah must have been a fluke, seems to post fine now, but did not say it posted anything (not sure if it suppose to, would be nice though) but when I went to my page they were there....
     
  24. Ethan

    Ethan

    Joined:
    Jan 2, 2008
    Posts:
    501
    You are right, it would be better to tell the user, that sth. was posted to his wall. But at least it worked. ;)
     
  25. techmage

    techmage

    Joined:
    Oct 31, 2009
    Posts:
    2,133
    Will this work on iOS?

    As in, could a person login into their facebook account from inside the iOS app, then invite friends, and have the iOS app retrieve photos from facebook ?
     
  26. Ethan

    Ethan

    Joined:
    Jan 2, 2008
    Posts:
    501
    Hi eem,

    the Unity game itself just uses the WWW class to communicate with .php files on your webserver which manage the facebook stuff.
    (I also implemented webplayer to javascript communication just to demonstrate that this is possible, too)

    That said you could do quite anything via WWW and WWWForm, because it's just like calling a page in your browser.
    The example i am providing doesn't handle this case though (Facebook login IN Unity).

    Inviting friends and displaying photos are peanuts - just Graph Api calls you make in a .php file you create, which will get called by WWW from your app. The example contains a "posttowall.php" file, which demonstrates how to use Graph Api calls and how to stuff them into your app.

    Cheers
    Ethan
     
  27. eyalfx

    eyalfx

    Joined:
    Oct 8, 2010
    Posts:
    108
    Hi Ethan,
    I've yet to figure out how to use the postwall.php example and and grab photos or anything else from the graph for that matter.
    Any chance you can post a working example?
    Sorry, this is not peanuts for me.
    Thanks,
    Eyal.
     
  28. Ethan

    Ethan

    Joined:
    Jan 2, 2008
    Posts:
    501
    Hey eyal,

    sorry for the late reply, but i didnt find any time for putting sth. together till now (weekend!)
    Here you go:
    http://apps.facebook.com/unity_example/showfriends.php

    This page uses Graph Api calls to display all of your friends photos! This should help you to get a further idea how to use Grap Api stuff you get from here: http://developers.facebook.com/docs/reference/api/

    and here is the source for the showfriends.php example:

    Code (csharp):
    1. <?php
    2.  
    3.     include('inc.php');
    4.  
    5.     $friends = $facebook->api('/'.$UID.'/friends');
    6.     $friendcount = count($friends[data]);
    7.    
    8.     for($i=0;$i<$friendcount;$i++)
    9.     {
    10.         echo '<img src="https://graph.facebook.com/'.$friends[data][$i]['id'].'/picture"><br />';
    11.     }
    12.  
    13. ?>
     
  29. eyalfx

    eyalfx

    Joined:
    Oct 8, 2010
    Posts:
    108
    Thanks Ethan.
    I'll check it out.
     
  30. techmage

    techmage

    Joined:
    Oct 31, 2009
    Posts:
    2,133
    Ethan, thank you for your help. When I get around to doing the facebook integration on my app I will probably be purchasing your example.

    Something else I would really like to see an example of is the facebook game credits integrated into a Unity game.
     
  31. codonnell

    codonnell

    Joined:
    Oct 5, 2009
    Posts:
    12
  32. Ethan

    Ethan

    Joined:
    Jan 2, 2008
    Posts:
    501
    Mh, it works for me!
    Can you try again and if it still doesnt work, can you delete the app and readd it to facebook and see if it works then?

    Maybe my webspace or facebook were laggy to this point of time.
    If anyone else has the same issue, please post here

    thanks
    e
     
  33. codonnell

    codonnell

    Joined:
    Oct 5, 2009
    Posts:
    12
    Pretty strange. Still doesn't work for me. Posting to the wall works though.



    Casey
     
  34. Ethan

    Ethan

    Joined:
    Jan 2, 2008
    Posts:
    501
    I got it: your javascript is disabled i guess! ;)

    For showing how to use the Unity <-> Web Communication i made two javascript functions to get the users name photo url.
    The post to wall stuff is in a seperate php file - that's why it works.

    Of course you can get the names, photos and everything through php, too to avoid such problems (as you can see in my code some posts above)!

    Phew ;)
    Ethan
     
  35. codonnell

    codonnell

    Joined:
    Oct 5, 2009
    Posts:
    12
    Hmmm... No, javascript is on. For sure, I'm teaching a class now that uses both the PHP side FB SDK and JS SDK. I wonder if it is a plugin version issue or something. Anyway, not sure. No worries I guess. :)
     
  36. Ethan

    Ethan

    Joined:
    Jan 2, 2008
    Posts:
    501
    wrote you a pm ;)
     
  37. MrDude

    MrDude

    Joined:
    Sep 21, 2006
    Posts:
    2,569
    I just got this code last night but due to recent lack of sleep I didn't touch it until this morning...
    So this morning I got up, staggered to my computer and less than 15 minutes after unzipping the package, I was staring at my ugly mug on my own Facebook app...

    Seriously, guys, I was struggling with this whole Facebook integration thing for... i don't know... It feels like decades! Tried doing it on my own, tried using other packages, struggled, struggled, struggled. I couldn't even see my own ugly mug... That was the benchmark I set myself: At least see my profile photo and my name. I simply couldn't do it. It all seemed so simple, but it just didn't work... not getting this simple thing to work made me feel stupid, then frustrated then desperate...

    Then I got this kit and managed to do it in under 15 minutes while still half asleep... It was awesome!
    I would seriously recommend this kit to anyone who wants to integrate Facebook into their games...
    It can't be any easier than this!

    The only complaint I do have is that you have to call your app something silly like "example-project02.unity3d" and in the web page where you can change that to whatever YOU want to call it, THAT he didn't mention in the docs. Wow! Big problem, huh? Now you see what I mean! Trying to get Facebook to work within my game, the hardest part was changing "example-project02.unity3d" to "webplayer.unity3d" inside a plain old simple PHP file...

    Sorry for the rant but I am way excited at finally getting Facebook working!
    Now, back to work. Got many ideas to implement now...
     
  38. pepworks

    pepworks

    Joined:
    Jan 18, 2010
    Posts:
    19
    Hi Ethan,

    very good work! I needed some time on the facebook side to find
    the best setting, but your solution totally rocks!

    Many thanks, saved me a lot of time!

    pep
     
  39. Ethan

    Ethan

    Joined:
    Jan 2, 2008
    Posts:
    501
    Thanks for the kind words. I am glad that it worked for you. :)
     
  40. enriquetosabioiii

    enriquetosabioiii

    Joined:
    Jun 23, 2011
    Posts:
    6
  41. the_gnoblin

    the_gnoblin

    Joined:
    Jan 10, 2009
    Posts:
    722
    Hi,
    this doesn't work with unicode (my name is written in Russian).
    And also wall post doesn't seem to work.

    thanks,
    Slav
     
  42. Ethan

    Ethan

    Joined:
    Jan 2, 2008
    Posts:
    501
    @enrique: glad that i could help you ;)

    @gnoblin: did you take my solution or enrique's? If it's mine, call "publishtowall.php" directly in your browser and tell me if there are any errors. If not it's maybe just facebook which is laggy (sometimes it takes some time till posts made by apps appear).
    What's the problem with unicode? Is your name displayed wrong in the app?

    Btw you should check if PHP5 is enabled on your webserver.

    Cheers
    e
     
  43. MrDude

    MrDude

    Joined:
    Sep 21, 2006
    Posts:
    2,569
    @ethan
    I recently had an issue with my UDE where a customer complained he couldn't use special characters. They simply don't show up. All this time I thought that was a Unity+fonts limitation but then I read on this forum that if you change the file's encoding to UTF-16 instead of UTF-8, it works. So I tried it and true to form it worked. I was so surprised you wouldn't believe it!

    Run the program, it doesn't work. Open settings, change the encoding type for my PLAINT TEXT FILE, hit save, run the game again and now everything works! Incredible!!!!

    So then I tried to make a change to the file and the file became useless in Unity. When I open it in any text editor, my text is there but when I USE it in Unity, Unity sees only character 0 as end of file and thus sees only an empty file. I tried changing the encoding type again using Monodevelop, Textedit and unitron but nothing worked. I tried creating a new file with a valid encoding and tried copying text from the other file and irrespective of if I copy the entire line or just portions inside a line of text, as soon as I paste that line, Unity stops reading the file at the end of whatever I pasted. Claims it found invalid characters in the text file and stopped right there.

    I ended up having to retype everything by hand. All 300 lines of text. Had 3 files to retype... all because I used a text encoding type that unity doesn't like. So yeah, although people like gnobin are forced to use those encoding types, I personally think they suck because of the problems they caused me...

    @gnobin
    See if you are having the same issue as me. On line 1 of the script, type: Debug.Log("test"); and see if it actually works...
    If it does then kick ethan. If it doesn't then send Unity a bug report. :p
     
    Last edited: Jul 8, 2011
  44. MrDude

    MrDude

    Joined:
    Sep 21, 2006
    Posts:
    2,569
    When oh when are you going to do a Credits add-on?
    ;)
     
  45. maidamedia

    maidamedia

    Joined:
    Nov 19, 2009
    Posts:
    224
    I am sorry but I was not so lucky with the Facebook prefab. I tried twice to follow the instructions, but I got this error when I try to go in FB to the application page


    Fatal error: Uncaught exception 'Exception' with message 'Facebook needs the CURL PHP extension.' in D:\inetpub\webs\plus3dnet\unityfacebook2\facebook\src\facebook.php:20 Stack trace: #0 D:\inetpub\webs\plus3dnet\unityfacebook2\inc.php(5): require() #1 D:\inetpub\webs\plus3dnet\unityfacebook2\publishtowall.php(3): include('D:\inetpub\webs...') #2 {main} thrown in D:\inetpub\webs\plus3dnet\unityfacebook2\facebook\src\facebook.php on line 20

    Any idea? Thanks
     
  46. Ethan

    Ethan

    Joined:
    Jan 2, 2008
    Posts:
    501
    This problem is caused by your webhost. It needs to have the cURL PHP extension for the Facebook API.
    Maybe you can install cURL or contact your webhost support to install it for you.

    Just google for "curl php" for more information about it.

    Sorry for the inconvenience anyway ;)

    @MrDude: there is a credits facebook product in the shop already, so i stopped doing an own.
     
  47. maidamedia

    maidamedia

    Joined:
    Nov 19, 2009
    Posts:
    224
    Ok it was a windows host without CURL extension. Now it is a linux server with all the extension, but I was not able to customize your prefab to work correctly. I called the web player differently from your demo and I have modified the reference to it inside header.php, to match with mine.
    I followed your short documentation, but no way: my application starts without the authentication step.
     
  48. Ethan

    Ethan

    Joined:
    Jan 2, 2008
    Posts:
    501
    If you call the app on your webhost (like www.yourpage.com/yourfunkyapp), does it redirect to facebook - showing your app in an iframe?
    Did you replace the URLs carefully (maybe an wrong placed "/" in the end)?
    Did you enter your secret ID and app ID in the inc.php?
    Did you upload all files + the facebook folder which were in the "unityfacebook" folder?
    Did you check if PHP5 is enabled on your webhost?
    Do you get any PHP errors?
     
  49. maidamedia

    maidamedia

    Joined:
    Nov 19, 2009
    Posts:
    224
    when I call http://www.yourpage.com/yourfunkyapp it remains in my domain not in facebook.

    where do I have to care about URL wrong characters?

    all the rest is ok

    Thanks
    Stefano
     
  50. maidamedia

    maidamedia

    Joined:
    Nov 19, 2009
    Posts:
    224
    at the moment it completes the authentication step, my photo and name appear but it doesn't write in my wall.
    thanks