Friday, July 18, 2014

Unity3D: How to Post to Facebook from your Unity Game

We've shown you a simple way to post to Twitter from within your game; today, we're going to tackle a simple way to post to Facebook. The general idea is the same, but there's a little more setup involved. I'm going to supply a template that will work out of the box, and we'll talk about the specifics afterwards.

Before you get cracking in your code, you're going to need to create a Facebook app to handle sharing from your game. Start by going here to create your app.

Once your app is ready, we're going to use it to publish to Facebook. Much like Twitter, we're going to use Application.OpenURL to send the request to Facebook and let them make the magic happen.

If you've read my other posts, you'll know that I'm a big fan of using NGUI for buttons and the like. If you're not using it, I've written a very simple post on getting started with it. With NGUI, just call the ShareToFacebook () method in the OnClick () method of the button of your choice, passing in the proper parameters.

You may have stumbled across similar code, but nearly all of the examples I've found were forming the URL improperly. Here's what I have, and it works wonderfully:

private const string FACEBOOK_APP_ID = "123456789000";
private const string FACEBOOK_URL = "http://www.facebook.com/dialog/feed";
 

void ShareToFacebook (string linkParameter, string nameParameter, string captionParameter, string descriptionParameter, string pictureParameter, string redirectParameter)
{
Application.OpenURL (FACEBOOK_URL + "?app_id=" + FACEBOOK_APP_ID +
"&link=" + WWW.EscapeURL(linkParameter) +
"&name=" + WWW.EscapeURL(nameParameter) +
"&caption=" + WWW.EscapeURL(captionParameter) + 
"&description=" + WWW.EscapeURL(descriptionParameter) + 
"&picture=" + WWW.EscapeURL(pictureParameter) + 
"&redirect_uri=" + WWW.EscapeURL(redirectParameter));
}

If you've followed our Twitter post, you can see that there is a little more to this. The FACEBOOK_APP_ID comes from the app that you built (I've used a fake one here, so remember to replace it with yours). The FACEBOOK_URL is the URL used for posting to the feed. After that, we get to the meat and potatoes of customizing our content.

linkParameter is the link that will be posted to the wall. For example, you could link to your game's website or download page.

nameParameter is the title of your post. You probably want to briefly describe what it is you're posting (i.e., "I'm playing Game XYZ!"). The name will be a link to the URL described by linkParameter.

captionParameter is the caption of your post. This appears in small type right below the title of your post. It may be a good idea here to give a bit more detail on what you're posting (for example, "New high score!"). 

descriptionParameter is the body of the message. Here, you can give the bulk of your message, such as the score you attained, an achievement you unlocked, etc. 

pictureParameter is a link to a picture you'd like to include in your post. Keep in mind that pictures must be at least 200px x 200px.

Finally, redirectURIParameter is the page the user will be redirected to after publishing their message. This is the parameter that is likeliest to cause you a headache. If you want to go the no-hassle route, just link to http://www.facebook.com/.

Improperly forming your URL or trying to redirect to a domain that is not assigned to your Facebook app are almost always the cause of error 100 and error 191. If you experience either of these, ensure that your URL is being formed correctly by comparing it to the URL redirection example on this page.

If you want to redirect to a page that is outside of Facebook's domain, you'll need to take a couple of extra steps. First, go to your app's page, and select "Settings" from the menu on the left. Click the "Add Platform" button, and select "Website". Under "Site URL", enter the page you want to associate with your app. Now, under "App Domains", enter the domain that page belongs to. So, if you added your Site URL as "www.example.com/index.html", you'll want to enter "www.example.com" under App Domains. Now, your redirect should work as intended.

If you'd like to go more in-depth, you can view the Feed Dialog documentation here. Keep in mind that you can use the Share Dialog, as the Feed Dialog is deprecated, but we're going for simplicity here, and I think this is the quickest and easiest way to get Facebook sharing going.

49 comments:

  1. Thanks! This will help a lot!

    ReplyDelete
  2. Now, to complete the set, any chances of a "rate me" pop up tutorial? Hahaha. BTW, to whom do I give thanks in my game credits?

    ReplyDelete
    Replies
    1. Thanks for the suggestion. I'll pop that up next. No credit is necessary, but if you'd like to, a link to my blog would be great, and of course donations are always welcome. Also, once it's released, send me a link to your game so I can check it out!

      Delete
    2. The "Rate Me" tutorial is up now. Enjoy!

      http://unity3dtrenches.blogspot.com/2014/07/unity-3d-how-to-let-user-rate-your.html

      Delete
  3. hi, great share, but can you show if there's a way to return to the app after posting to facebook?

    ReplyDelete
    Replies
    1. oh nvm, I figured it out from
      http://yashesh87.wordpress.com/2012/01/03/open-your-iphone-app-from-safari-browser-in-iphone-using-url-schemes/

      Delete
    2. Great find! I'm sure that's going to help some folks out. As you've determined, it's dependent on what OS you're targeting.

      Delete
  4. is there a way to check if the post was actually posted?

    ReplyDelete
    Replies
    1. No, as has been discussed on other posts, there's no way to tell if the user actually posted or not ... and really, you don't want to anyway. It's better to trust the user and assume they've posted than to harangue them until they do.

      Delete
  5. So this solution does not require anything to be added to the Android manifest file?

    ReplyDelete
    Replies
    1. That is correct. No fooling around with the manifest is required.

      Delete
  6. I noticed the "Say something" text field is not populated. Is there a way to populate that as well?

    ReplyDelete
    Replies
    1. Not as far as I know. The Feed Dialog documentation does not mention taking in any parameters that would populate that field.

      https://developers.facebook.com/docs/sharing/reference/feed-dialog/v2.0

      Delete
    2. As of right now that is against Facebook's policy.

      https://developers.facebook.com/docs/apps/review/prefill

      Delete
  7. hi,great thing it saves me lot,cant able return back to game in webplayer or any other possible way to open facebook in newtab.

    ReplyDelete
    Replies
    1. You can, using Application.ExternalCall, but it would change how you're handling things quite a bit. Check out this link:

      http://answers.unity3d.com/questions/32531/applicationopenurl-on-webplayer-to-open-in-a-new-t.html

      Delete
  8. Heyy great article . Thanks for that, it really helps. But I want to ask one thing, it opens url in mobile browser once I hit share button. I want to perform post on facebook via mobile app not via mobile browser..

    ReplyDelete
    Replies
    1. Off the top of my head, I'd say you could try replacing "http://" with "fb://", but I don't if that work for this particular task. It is, however, the typical way to launch the Facebook app on mobile instead of the browser.

      Delete
  9. could you please provide me the script in java please i'm getting error since i hate c#

    ReplyDelete
    Replies
    1. I may add Javascript in the future, but in Unity I work solely in C#. It's not difficult to convert. Here is a starting point:

      http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html

      You can also reference a C# script from a Javascript file, although that comes with its own headaches -- I'd recommend just manually re-writing it. However, here's some info on that as well:

      http://forum.unity3d.com/threads/calling-a-c-function-from-javascript.34061/

      Delete
  10. Hi please help i get error here are my script.:

    using UnityEngine;
    using System.Collections;

    public class scharing : MonoBehaviour {

    // Use this for initialization
    void OnMouseDown() {
    ShareToFacebook ();

    }

    private const string FACEBOOK_APP_ID = "123456789000";
    private const string FACEBOOK_URL = "http://www.facebook.com/dialog/feed";

    void ShareToFacebook (string linkParameter, string nameParameter, string captionParameter, string descriptionParameter, string pictureParameter, string redirectParameter)
    {
    Application.OpenURL (FACEBOOK_URL + "?app_id=" + FACEBOOK_APP_ID +
    "&link=" + WWW.EscapeURL(linkParameter) +
    "&name=" + WWW.EscapeURL(nameParameter) +
    "&caption=" + WWW.EscapeURL(captionParameter) +
    "&description=" + WWW.EscapeURL(descriptionParameter) +
    "&picture=" + WWW.EscapeURL(pictureParameter) +
    "&redirect_uri=" + WWW.EscapeURL(redirectParameter));
    }
    }

    and found this error : Assets/scharing.cs(8,17): error CS1501: No overload for method `ShareToFacebook' takes `0' arguments

    completely new to c# please don't ask me to use nGUI i want to attach this script to a facebook share image on my game. thank you

    ReplyDelete
  11. or if you can give me a complete script it will be very apreciate. thank you

    ReplyDelete
  12. Replies
    1. As I replied to your comments on the Twitter post, you're not going to find anyone to write the code for you. Please try not to spam blog comments with requests for people to write your code for you -- it's bad etiquette. If you want to write C#, you're going to have to learn the basics of C#. You can start here:

      https://msdn.microsoft.com/en-us/library/aa288436(v=vs.71).aspx

      Delete
  13. Question about the picture parameter? Does the picture need to be uploaded to a site prior to being posted on FB? Can I submit a screenshot from a Unity iOS app using this method?

    ReplyDelete
  14. Hii.. I get an error on windows phone

    is there any suggestion, if we want to do it on windows phone?

    ReplyDelete
  15. Hi, Is this post about Unity3D web player? How can I get parameter from redirect_uri ? I guess that Unity3D doesn't allow you to get controll of browser. How can I pass this parameter to my game?

    ReplyDelete
  16. any way regarding the facebook app share?. your twitter code work just fine in android, and can use the twitter app, but not the facebook

    ReplyDelete
  17. Hello. I have created app in Facebook and when I try to publish it from my game, Facebook tooks my app ID but not other parameters. In Facebook page it asks me to write message to post and shows blank text input.

    ReplyDelete
    Replies
    1. same problem, have you solved it? Right now, I've figured out, if it doesn't link to the FB page, if will show your other text, but if you do, it won't.

      Delete
    2. This comment has been removed by the author.

      Delete
    3. ok, I found out that if you link to FB, or the google play store facebook will use info from those sites and ignore what you put it.

      Delete
  18. I didn't find any information about how to use Share Dialog features using facebook unity SDK. Looks like it's not possible to share an open graph story via Share Dialog without any additional permissions

    ReplyDelete
  19. Hello,

    I'm trying to use this code, but if I have my facebook page in the "Link Parameter" it won't display any of the text fields.

    if I leave the "Link Parameter" black, it will display my text, but clicking on it will direct to my picture link (imgur).

    know what I could do to fix this?

    ReplyDelete
    Replies
    1. nvm, found out facebook will take information from the site if you link to facebook or the google play store.

      Delete
  20. I am trying to share game score on facebook. all are going okay, but the picture is not working. my picture size is 417px * 417px.

    ReplyDelete
  21. This comment has been removed by the author.

    ReplyDelete
  22. Thank you so much for the tutorial and i really appreciated for your well description. Thank you :D

    Twitter :The twitter one worked like a magic,no error,no problem at all.

    Facebook : When i try to run it on my android device,after loading the facebook in website,gives me the error "The parameter href is required" . Can you please tell me if you know anything about the error.

    It would be really appreciated.
    Thanks in advance :)

    ReplyDelete
  23. Hey i tried this but i am getting the errors the error is "The redirect_uri URL must be absolute"
    please help me out from this issue.

    ReplyDelete
    Replies
    1. You should have http or https in your redirecturl.
      ex. redirect to http://www.google.com instead of www.google.com.

      This will work out.

      Delete
  24. Can we do Activate.openlurl() method type as post instead of get ?
    To hide browser link data.

    ReplyDelete
  25. And thank you for sharing this.

    ReplyDelete
  26. This comment has been removed by the author.

    ReplyDelete
  27. When i click Share button the facebook browser automatically close. any idea?

    ReplyDelete