Free facebook Plugin for Unity iOS

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 GitHub - facebook/facebook-ios-sdk: Used to integrate the Facebook Platform with your iOS & tvOS apps. 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 :wink:

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:

@interface AppController : NSObject<UIAccelerometerDelegate, UIApplicationDelegate, FBSessionDelegate>

Then you add the plugin in the interface declaration.

FacebookPlugin* facebookPlugin;

Directly under the curly brackets, above the function declarations - you put this:

@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:@synthesize facebookPlugin;

Scroll down to the applicationDidFinishLaunching: function and put this in there at the first line:

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.

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:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url 
{
    return [[facebookPlugin facebook]handleOpenURL:url]; 
}


// For 4.2+ support
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication annotation:(id)annotation 
{
    return [[facebookPlugin facebook] handleOpenURL:url]; 
}

Then - when you are coding in Unity, you can test everything this with this:

#if UNITY_IPHONE
		if(FacebookUnityPlugin.FacebookIsSessionValid())
		{
			FacebookUnityPlugin.FacebookRequestUserInfo();
			FacebookUnityPlugin.FacebookRequestFriendInfo();
		}
		else
		{
			FacebookUnityPlugin.FacebookLogin();
		}
#endif

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!

Hi! I’m glad the code could be useful :slight_smile: 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 :slight_smile: 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 :slight_smile:

/Niklas

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.

I’m sorry, I’m not sure I understand your question. Would you please clarify?

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!

Kindly post the code.

Thanks.

You can get the code here.

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,

how can i post only score and one string to the fb wall? can you show me the way that will be great help…

Yeah, that should not be a problem. If you’ve installed the plugin correctly you should only have to use the method:

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.

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!

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?

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.

#if UNITY_IPHONE

        if(FacebookUnityPlugin.FacebookIsSessionValid())

        {

            FacebookUnityPlugin.FacebookRequestUserInfo();

            FacebookUnityPlugin.FacebookRequestFriendInfo();

        }

        else

        {

            FacebookUnityPlugin.FacebookLogin();

        }

#endif

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!

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.

I’m getting the same error as janettfidm. The IsSessionValid is always returning false.

Any assistance or pointers would be very much appreciated!

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!

Yeah, I figured that was going to be the case, I’ll start looking into that then!

Thanks for the response!

Hey guys, is someone figured out what is causing the problem exactly and can post the updated code? :slight_smile: