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. }

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 ...