Search Unity

Free facebook Plugin for Unity iOS

Discussion in 'Assets and Asset Store' started by NiklasBorglund, Feb 9, 2012.

  1. NiklasBorglund

    NiklasBorglund

    Joined:
    Feb 9, 2012
    Posts:
    262
    I have made a Facebook plugin for use within your unity game.

    Add FacebookUnityPlugin.cs to your unity project and you can use the functions in that class.

    When you built your scenes in Unity into an xCode-project, you will have to download the facebook iOS SDK from https://github.com/facebook/facebook-ios-sdk and
    add the source files to your project.
    Also add the FacebookPlugin.h and Facebookplugin.mm to your xCode project. In the .mm file you need to write your facebook appID in the init function -
    or you could rewrite the function to take in the ID as a parameter.

    Follow the instructions at https://developers.facebook.com/docs/mobile/ios/build/ and change your .plist file accordingly

    Init the plugin in AppController.h and .mm and you should be good to go.

    You can download the script HERE.

    Feel free to use this however you like, commercially or not. If you do want to use the plugin or parts of it commercially,
    we would appreciate if you mentioned Cry Wolf Studios in the credits section of your game.

    If you want to see the plugin "In Action", you can check out our game Invading:Insanity ;-)
     
    Last edited: Sep 30, 2013
  2. NiklasBorglund

    NiklasBorglund

    Joined:
    Feb 9, 2012
    Posts:
    262
    Here are some things that you could try if you are having problems implementing this.

    First, try extending your AppController.h in the class definition with FBSessionDelegate, so it looks something like this:
    Code (csharp):
    1. @interface AppController : NSObject<UIAccelerometerDelegate, UIApplicationDelegate, FBSessionDelegate>
    Then you add the plugin in the interface declaration.
    Code (csharp):
    1. FacebookPlugin* facebookPlugin;
    Directly under the curly brackets, above the function declarations - you put this:
    Code (csharp):
    1. @property(nonatomic, retain)FacebookPlugin* facebookPlugin;
    Then you go into the AppController.mm -file and scroll down to the line where it says "@implementation AppController" and put this in the line directly below that one:
    Code (csharp):
    1. @synthesize facebookPlugin;
    Scroll down to the applicationDidFinishLaunching: function and put this in there at the first line:
    Code (csharp):
    1. facebookPlugin = [[FacebookPlugin alloc] init];
    Go into the FacebookPlugin.mm and make sure you have the correct appID(The one you get from facebook) in the init function too.
    Code (csharp):
    1. facebook = [[Facebook alloc] initWithAppId:@"[COLOR="red"]YOUR_FACEBOOK_APP_ID[/COLOR]" andDelegate:self];
    If this doesn't work, then you could try and put these two functions below the applicationDidFinishLaunching:
    Code (csharp):
    1. - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
    2. {
    3.     return [[facebookPlugin facebook]handleOpenURL:url];
    4. }
    5.  
    6.  
    7. // For 4.2+ support
    8. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
    9.   sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
    10. {
    11.     return [[facebookPlugin facebook] handleOpenURL:url];
    12. }

    Then - when you are coding in Unity, you can test everything this with this:
    Code (csharp):
    1. #if UNITY_IPHONE
    2.         if(FacebookUnityPlugin.FacebookIsSessionValid())
    3.         {
    4.             FacebookUnityPlugin.FacebookRequestUserInfo();
    5.             FacebookUnityPlugin.FacebookRequestFriendInfo();
    6.         }
    7.         else
    8.         {
    9.             FacebookUnityPlugin.FacebookLogin();
    10.         }
    11. #endif
     
  3. Rigato

    Rigato

    Joined:
    Jun 17, 2011
    Posts:
    5
    Niklas,

    You are awesome! Your code helped me A LOT.

    I wrote one extra method, based on the existing "- (void) fb_PostMessage: (const char*)message" to post not a message, but a link to a web hosted picture.

    But i found that the original "FacebookPostMessage()" from FacebookUnityPlugin.cs woudn't post anything more than a char on facebook feed. Really don't know if I was unable to make it work properly as it was implemented, but i worked my way to change the structure of this method inside FacebookUnityPlugin.cs to a similar way that "FacebookPostToFeed()"does, with a intermediate method "FacebookPostFeedMessage" that converts the passed strings with "Marshal.StringToHGlobalAnsi" and the data can sucessfuly pass trough to the FacebookPlugin.m.

    If the code i created is needed, i can post it here.

    My question is:

    If i take a picture with a device with the unity webcamdevice and webcamtexture, how can i push the picture trough the facebook ios sdk to post it inside the user's album?

    Thanks!
     
  4. NiklasBorglund

    NiklasBorglund

    Joined:
    Feb 9, 2012
    Posts:
    262
    Hi! I'm glad the code could be useful :) And I'm really sorry about the late answer, I haven't checked this thread for a while.

    About posting a picture to the user's album. I haven't done that myself, but i did a quick read in the facebook sdk and I found somethings that might be useful.

    http://developers.facebook.com/docs/reference/api/#publishing

    There it says more in depth of what parameters you can use when posting stuff through the Graph API.
    And to send a photo, you need a valid URL to the source image in the parameters when you are using the requestWithGraphPath method.

    I haven't tried any of this. So I don't really know what solution that would be best for this problem, but the first one that comes to mind is if you could convert your webcamtexture into a texture2D and then save it to your device.

    Make an external function that takes the path to an image in FacebookPlugin.m. Once there you use the saved image and you post it to facebook with the information you sent from unity. Don't know if the string to the storage on your device counts as a valid URL though.

    Maybe you can convert it into an UIImage first before you send it. (Found some information on stackoverflow that could be helpful)

    Anyway, hope this will help :) Haven't tried it, so don't know if it works.

    Just let me know if you run into any problems if you haven't already solved it and I'll try and help the best i can :)

    /Niklas
     
  5. smyte100

    smyte100

    Joined:
    Mar 10, 2012
    Posts:
    53
    Please, is it possible to post 'int' and 'float' with this.. Sorry , i don't have an iOS device to test this but i want to get one soon.
     
  6. NiklasBorglund

    NiklasBorglund

    Joined:
    Feb 9, 2012
    Posts:
    262
    I'm sorry, I'm not sure I understand your question. Would you please clarify?
     
  7. Rigato

    Rigato

    Joined:
    Jun 17, 2011
    Posts:
    5
    Yep,

    Pulled this couple of weeks ago:

    Unity saved the picture taken at persistent data path, and at facebookPlugin.mm the code loads that image and pushes through facebook iOS SKD to user`s album or wall.

    Could not have done this without your plugin. Thanks again!
     
  8. rastoarpit

    rastoarpit

    Joined:
    Feb 20, 2012
    Posts:
    8
    Kindly post the code.

    Thanks.
     
  9. NiklasBorglund

    NiklasBorglund

    Joined:
    Feb 9, 2012
    Posts:
    262
    You can get the code here.
     
  10. Dhaval Sarvaiya

    Dhaval Sarvaiya

    Joined:
    Jul 28, 2012
    Posts:
    9
    can i use this method to post only score and string to Facebook wall ? and if yes can you tell me how, ? i am having prime31 but there is some problem in it it is not showing up the prime31 in menu bar so i am not able to add the app id in that,
     
  11. Dhaval Sarvaiya

    Dhaval Sarvaiya

    Joined:
    Jul 28, 2012
    Posts:
    9
    how can i post only score and one string to the fb wall? can you show me the way that will be great help..
     
  12. NiklasBorglund

    NiklasBorglund

    Joined:
    Feb 9, 2012
    Posts:
    262
    Yeah, that should not be a problem. If you've installed the plugin correctly you should only have to use the method:

    Code (csharp):
    1. FacebookPostFeedMessage(string friendID, string name, string caption, string description, string link, string pictureLink);
    from within Unity.

    Then put your own ID in friendID and empty strings in the fields you don't want to use.

    EDIT: If you have purchased a plugin from Prime31, the easiest way would probably just to try and get that working for you, because that is a plugin that's way more supported and documented than mine.
     
    Last edited: Sep 14, 2012
  13. janettfidm

    janettfidm

    Joined:
    Sep 13, 2012
    Posts:
    2
    Hey, it's very nice. I would love to use it.
    However, I faced some problems about isSessionValid. No matter I have logined or not, FacebookUnityPlugin.FacebookIsSessionValid() always returns false.

    Can you please suggest me some solution >_<? Thanks!
     
  14. NiklasBorglund

    NiklasBorglund

    Joined:
    Feb 9, 2012
    Posts:
    262
    hmm. That's odd. The first thing that crossed my mind was the login process.

    So you get the login screen every time you start the app as it is now? Does that work as it should?
     
  15. janettfidm

    janettfidm

    Joined:
    Sep 13, 2012
    Posts:
    2
    Yup. I can go to Facebook Login page. Login successful. Then, redirect to the app again. However, there is no valid session. The function always returns false.

    Between, I am new to Unity and iOS. I don't know if I did anything wrong. Can you please help me to check the progress.

    1.) Create a new unity project.
    2.) Create a new Scene.
    3.) Add the following code you provided.
    Code (csharp):
    1.  
    2. #if UNITY_IPHONE
    3.  
    4.         if(FacebookUnityPlugin.FacebookIsSessionValid())
    5.  
    6.         {
    7.  
    8.             FacebookUnityPlugin.FacebookRequestUserInfo();
    9.  
    10.             FacebookUnityPlugin.FacebookRequestFriendInfo();
    11.  
    12.         }
    13.  
    14.         else
    15.  
    16.         {
    17.  
    18.             FacebookUnityPlugin.FacebookLogin();
    19.  
    20.         }
    21.  
    22. #endif
    23.  
    4.) Build and Run for iOS.
    5.) A Xcode project is created.
    6.) Add FacebookPlugin.mm and FacebookPlugin.h into Xcode project.
    7.) Add the whole src folded in facebookSDK into Xcode project.
    8.) Since there is some errors occurs, I added Accounts.Framework, Socials.Framework and AdSupport.Framework.
    9.) Besides, in build setting, I add -lsqlite3.0 in other linkers.
    10.) In FacebookPlugin.mm, I modify the AppID which is got from facebook APP I created before.
    11.) Finally, I modified the plist to add FacebookAppID and URL Types.

    Did I miss anything else>_<? Thanks!
     
    Last edited: Oct 24, 2012
  16. NiklasBorglund

    NiklasBorglund

    Joined:
    Feb 9, 2012
    Posts:
    262
    You have to initialize the facebookplugin in AppController.h .mm. There's instructions on how to do that in the second post of this thread(first 5 instructions).

    I don't recall that I got any errors when I added the code(Like your step 8-9). It was a while ago I wrote this, so something with the facebook SDK might have changed since then.

    Edit: Also when you build your project, the .plist will be replaced. So save a copy of the one that you have your appID in and replace it after each build.
     
    Last edited: Oct 24, 2012
  17. Gazcraft

    Gazcraft

    Joined:
    Nov 8, 2012
    Posts:
    4
    I'm getting the same error as janettfidm. The IsSessionValid is always returning false.

    Any assistance or pointers would be very much appreciated!
     
  18. NiklasBorglund

    NiklasBorglund

    Joined:
    Feb 9, 2012
    Posts:
    262
    Ok, I googled the issue some. And there seem to be some changes in the new facebook iOS SDK since i used it.

    Maybe the information on this link will help : http://stackoverflow.com/questions/6157211/facebook-issessionvalid-is-always-no
    It will require you to do some coding in objective C in the facebookplugin classes, but it doesn't seem to be that much.

    Hope that helps!
     
  19. Gazcraft

    Gazcraft

    Joined:
    Nov 8, 2012
    Posts:
    4
    Yeah, I figured that was going to be the case, I'll start looking into that then!

    Thanks for the response!
     
  20. devast3d

    devast3d

    Joined:
    May 30, 2010
    Posts:
    74
    Hey guys, is someone figured out what is causing the problem exactly and can post the updated code? :)
     
    Last edited: Nov 13, 2012
  21. NiklasBorglund

    NiklasBorglund

    Joined:
    Feb 9, 2012
    Posts:
    262
    The only thing I do have to help you is basically the contents of this thread. I don't have any example projects available. So your best bet would be to dig in what's being said here and adjust to that.

    And I think more people would be willing to help(including me) if you posted some of your own coding attempts. Please don't just copy/paste my plugin code like that and expect someone to help you - I find it hard to believe that someone will if you do.

    So, try and get everything to work. Post some of your code - and I will try to help.
     
  22. gutorocher

    gutorocher

    Joined:
    Sep 20, 2012
    Posts:
    4
    was a bit confused and get a bit of understanding about the same.

    would have some documentation about the class .. concerned or could someone referential ...


    I'm having a problem that code below:

    so that when you click the button the same facebook connect, show the name on the screen.


    i have only error:

    Error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected (CS0119) (Assembly-CSharp)
    Error CS1502: The best overloaded method match for `UnityEngine.GameObject.GetComponent(System.Type)' has some invalid arguments (CS1502) (Assembly-CSharp)
    Error CS1503: Argument `#1' cannot convert `object' expression to type `System.Type' (CS1503) (Assembly-CSharp)


    I'm already getting everything right to get the name via console in xcode

     
  23. gutorocher

    gutorocher

    Joined:
    Sep 20, 2012
    Posts:
    4
    I would like to understand more about the class FacebookUnityPlugin.cs, has some reference material that used to write the same.

    'm thinking rewrite in javascript

    therefore facilitate this conversion problem I have at the moment

    by chance you have this class in javascript
     
  24. NiklasBorglund

    NiklasBorglund

    Joined:
    Feb 9, 2012
    Posts:
    262
    In your update() method, you need to make sure you are logged in to facebook first before you get your friends information.

    Try to get the facebook login part to work for you first. If you have set up everything correctly in xCode. This code would be enough to get you logged in:
    Code (csharp):
    1.  
    2. #if UNITY_IPHONE
    3.  
    4.         if(FacebookUnityPlugin.FacebookIsSessionValid())
    5.  
    6.         {
    7.  
    8.             FacebookUnityPlugin.FacebookRequestUserInfo();
    9.  
    10.             FacebookUnityPlugin.FacebookRequestFriendInfo();
    11.  
    12.         }
    13.  
    14.         else
    15.  
    16.         {
    17.  
    18.             FacebookUnityPlugin.FacebookLogin();
    19.  
    20.         }
    21. #endif
    22.  
    You might have to change some code on the obj-c side to get the authorization to work as mentioned previously in this thread.

    Then if your are logged in, you should be able to get information from you and your fb friends by using the methods in the c# class
     
  25. gutorocher

    gutorocher

    Joined:
    Sep 20, 2012
    Posts:
    4
    Concerning the access already calling the FacebookUnityPlugin.FacebookRequestUserInfo get ();, lah in xcode did verificaçnao be logged in and stuff like that and it worked good, ja estoa showing me that it takes the first name the problem in this relationship grab this dice and return to again show unity on the screen, referring to the error in the code that demostrei what can I do to solve
     
  26. john_ML_777

    john_ML_777

    Joined:
    Oct 22, 2012
    Posts:
    8
    hi, i am trying to get friendlist from facebook using FacebookUnityPlugin , i can logged in when i call FacebookUnityPlugin.FacebookLogin(); but when control goes to

    #if UNITY_IPHONE


    if(FacebookUnityPlugin.FacebookIsSessionValid())
    {
    Debug.Log("Session is Valid");
    FacebookUnityPlugin.FacebookRequestUserInfo();
    FacebookUnityPlugin.FacebookRequestFriendInfo();
    Invoke("GetFriends",2f);

    }
    else
    {
    FacebookUnityPlugin.FacebookLogin();
    Debug.Log("Session is not valid unity");

    }

    #endif


    this part of code xcode console shows

    FACEBOOK: Logging in
    Session is not valid

    this statements.

    Please tell how to access the friend list using FacebookUnityPlugin.
     
  27. gutorocher

    gutorocher

    Joined:
    Sep 20, 2012
    Posts:
    4
    how do you make in xcode code, the code the FacebookUnityPlugin.cs work in xcode 4.5 but i have change code because the new sdk facebook is updated.
     
  28. NiklasBorglund

    NiklasBorglund

    Joined:
    Feb 9, 2012
    Posts:
    262
    The isSessionValid property in the plugin always seem to return false at the moment. To fix this, you will probably have to do some Objective-C coding, don't think it's that much though. Found a thread on stack overflow which might give you some help: http://stackoverflow.com/questions/6157211/facebook-issessionvalid-is-always-no

    I currently only have Unity 4 without the iOS plugin, so I cannot help with the coding and testing for that. I would be interested to know if and how you succeed, so If you do - please post your code here. I'm sure alot of people here would appreciate it :)
     
  29. yashesh

    yashesh

    Joined:
    Jan 11, 2013
    Posts:
    10
    Hi NiklasBorglund,
    I want to access your FB plugin into my unity3d project. My project source code is in Javascript so how can i access your file "FacebookUnityPlugin.cs" in my Javascript files.
    Please help me
    Thanks a lot in advance.
     
  30. NiklasBorglund

    NiklasBorglund

    Joined:
    Feb 9, 2012
    Posts:
    262
  31. kkarunakaran

    kkarunakaran

    Joined:
    Apr 15, 2013
    Posts:
    2
    Can anyone upload the sample project. I would be great.


    Thanks
     
  32. NiklasBorglund

    NiklasBorglund

    Joined:
    Feb 9, 2012
    Posts:
    262
    There is no sample project, sorry. There's only the code for the plugin and the instructions in this thread.

    /Niklas
     
  33. CoixdeNono

    CoixdeNono

    Joined:
    Dec 10, 2012
    Posts:
    3
    First thank you very much for this thread.
    I'm very interested in using this plugin, but i'm still noob with iOS, and the ".h " ".mm" or other "framework" are pretty difficult for me to understand, so as kkarunakaran said, can we have juste a tiny sample project ? just a scene with login button or something like it ?
    Thank you :)
     
  34. NiklasBorglund

    NiklasBorglund

    Joined:
    Feb 9, 2012
    Posts:
    262
    Sorry, wish I could help, but I don't have Unity with the iOS extension anymore. I only have Unity 4.0 pro at the moment.

    Anyway, there's a big chance this tiny plugin is a bit outdated with the facebook sdk updating and all. You could probably fix it with some minor changes, but I think you should at least be familiar with Obj-C and Xcode before you try.

    If that's not an option, I think your best bet would be to buy a plugin from Prime31 or the like. From what I've heard, those plugins should be easy to use and doesn't cost that much. (I think it's about 60 - 70$ for the fb plugin)
     
  35. CoixdeNono

    CoixdeNono

    Joined:
    Dec 10, 2012
    Posts:
    3
    Thank you for the answer, i'll try to find an other plugin , or i'll get familiar with Obj-C
     
  36. cuongkimh4

    cuongkimh4

    Joined:
    Oct 4, 2012
    Posts:
    1
    sorry about my English, can you make a video and up on youtube so i can follow you to setup facebook plugin. thanks