Thursday, August 3, 2017

How to share Screenshot/Image using Facebook Unity SDK

We can login to facebook using following two functions provided by Facebook Unity SDK
1- FB.LogInWithReadPermissions
2- FB.LogInWithPublishPermissions

These two function use two type of permissions read and publish from more than 30 permissions provided by Facebook.If we want to share a link and description we can do simply by using FB.ShareLink which open a share dialogue in our app.This dialogue have everything if I'm not wrong except the option to upload stuff from local storage.So what should we do to share screenshot or image from device.It's very simple.we can do this in two steps


  1. First login using FB.LogInWithPublishPermissions by adding "publish_actions" permission in  parameters.
  2. Use Facebook Graph API to upload image
NOTE: If the image you want to share is imported in Unity Editor then make sure it has read enabled in import setting

Her is code snippet to post Screenshot/Image on Facebook
  1. void Start()
  2. {
  3.      FB.Init(OnInit);
  4. }

  5. private void OnInit()
  6. {
  7.      LoginToFB();
  8. }

  9. public void LoginToFB()
  10. {
  11.       FB.LogInWithPublishPermissions(new List<string>() { "publish_actions" }, LoginResult);
  12. }

  13. private void LoginResult(IResult result)
  14. {
  15.     //TODO:do what you want
  16. }

  17. // Call this function on the sharing event
  18. public void ShareScreenShot(Texture2D screenShot)
  19. {
  20.       byte[] encodedScreenShot = screenShot.EncodeToPNG();
  21.      
  22.       var wwwForm = new WWWForm();
  23.       wwwForm.AddBinaryData("image", encodedScreenShot, "ScreenShot.png");
  24.       wwwForm.AddField("message", "Write description here");
  25.       //calling graph api 
  26.       FB.API("me/photos", HttpMethod.POST, ShareScreenShotCallback, wwwForm);
  27. }
  28. void ShareScreenShotCallback(IResult result)
  29. {
  30.     //TODO:do what you want
  31. }

12 comments:

  1. How to add link / message to this post?

    ReplyDelete
    Replies
    1. check line 28.... change it a little bit like
      wwwForm.AddField("message", "check my game \n" +
      "https://play.google.com/store/apps/details?id=com.KayGames.JumpPlus");

      Delete
  2. using wwwForm.Addfield

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

    ReplyDelete
  4. Thank you! This helped very much

    ReplyDelete
  5. When I use this it automatically posts the screenshot without confirming or showing a popup. Is there a way to fix that?

    ReplyDelete
    Replies
    1. Hi Megh, You can create a custom dialogue which contains screenshot,input field for description and a post button.Call "ShareScreenShot" method on post button click.

      Delete
  6. Hi Khalid Mahmood, I followed your tutorial, but I am getting 403 error in ShareScreenShotCallback. Any idea about that?

    ReplyDelete
  7. It is trying to open the dialog, and show the spin loading for a moment, shows the white box with spin and 'loading', but it closes it and do nothing. In Log it calls the share callback, but don't make actual sharing. :(
    Can you help me?

    ReplyDelete
  8. Hi, on ShareScreenShotCallback(IResult result), can you please give some examples on how to handle the result, I'm sorry but I'm kind of new on this, and I don't know how to know if everything went ok to say the user that the image was shared or not.
    Thanks in advance.

    ReplyDelete
  9. Hello. How can i do that today? Once the Publish_actions is decrapted.
    Can you help me please?

    ReplyDelete

Changing Build Setting Dynamically/Programmatically in Unity 3D

Suppose we are publishing app on three platforms Google Play, iTunes Connect and Amazon. For every single change we have to make a separate ...