Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Houseman · Jun 24, 2016 at 05:00 AM · facebookscreenshotsharing

Posting screenshot on Facebook timeline using FB.API seems impossible

Hi all,

I am creating a game, and I would like the users to be able to post a screenshot (of score screen) to their timeline. I have tried many solutions in several topics, but get none of them working. After weeks of struggling hopefully someone can help me out.

Below is the code, which is called by the OnClick() event of a button.

 using UnityEngine;
 using System.Collections;
 using Facebook.Unity;
 using Facebook.MiniJSON;
 
 public class FB_Test : MonoBehaviour {
  
     // Awake function from Unity's MonoBehavior
     void Awake()
     {
         if (!FB.IsInitialized)
         {
             // Initialize the Facebook SDK
             FB.Init(InitCallback, OnHideUnity);
         }
         else {
             // Already initialized, signal an app activation App Event
             FB.ActivateApp();
         }
     }
 
     private void InitCallback()
     {
         if (FB.IsInitialized)
         {
             // Signal an app activation App Event
             FB.ActivateApp();
             // Continue with Facebook SDK
             // ...
         }
         else {
             Debug.Log("Failed to Initialize the Facebook SDK");
         }
     }
 
     private void OnHideUnity(bool isGameShown)
     {
         if (!isGameShown)
         {
             // Pause the game - we will need to hide
             Time.timeScale = 0;
         }
         else {
             // Resume the game - we're getting focus again
             Time.timeScale = 1;
         }
     }
 
     public void ShareFb()
     {
         StartCoroutine(TakeScreenshot());
     }
 
     private IEnumerator TakeScreenshot()
     {
         yield return new WaitForEndOfFrame();
 
         var width = Screen.width;
         var height = Screen.height;
         var tex = new Texture2D(width, height, TextureFormat.RGB24, false);
         // Read screen contents into the texture
         tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
         tex.Apply();
         byte[] screenshot = tex.EncodeToPNG();
 
         var wwwForm = new WWWForm();
         wwwForm.AddBinaryData("image", screenshot, "Screenshot.png");
  
         FB.API("me/photos", HttpMethod.POST, APICallback, wwwForm);
     }
     
     void APICallback(ILoginResult result)
     {
         if (result.Error != null)
             lastResponse = "Error Response:\n" + result.Error;
         else if (!FB.IsLoggedIn)
             lastResponse = "Login cancelled by Player";
         else 
             lastResponse = "Login was successful!";
     }    
 }
 

I get the following errors when running this code:

alt text

Currently I am using: Facebook SDK version 7.5.0, and Unity version 5.3.1

Is there anyone who can tell me what I am doing wrong and send an example of how I can take a screenshot and post this to facebook timeline? Your help is highly appreciated.

errors-fbapi.png (15.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by steakpinball · Jun 24, 2016 at 03:30 PM

Your method APICallback needs to accept IGraphResult as a parameter.

 void APICallback(IGraphResult result)

Fixing that will also resolve the error about converting WWWForm to IDictionary.

The variable lastResponse is not declared anywhere. It seems like it should be declared as a member variable in the class.

 private string lastResponse;

Everything else looks correct.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Houseman · Jul 03, 2016 at 07:44 PM 0
Share

Thank you very much $$anonymous$$! This seems O$$anonymous$$, since the application is running error free now. However when I click on the button to which this script is attached, no reaction is noticeable. The facebook app is not started, and I do not notice that a screenshot is taken and tempting to upload to my timeline. $$anonymous$$y code is now as follows:

 using UnityEngine;
 using System.Collections;
 using Facebook.Unity;
 using Facebook.$$anonymous$$iniJSON;
 
 public class FacebookShare : $$anonymous$$onoBehaviour {
 
     private string lastResponse;
     // Awake function from Unity's $$anonymous$$onoBehavior
     void Awake()
     {
         if (!FB.IsInitialized)
         {
             // Initialize the Facebook SD$$anonymous$$
             FB.Init(InitCallback, OnHideUnity);
         }
         else {
             // Already initialized, signal an app activation App Event
             FB.ActivateApp();
         }
     }
 
     private void InitCallback()
     {
         if (FB.IsInitialized)
         {
             // Signal an app activation App Event
             FB.ActivateApp();
             // Continue with Facebook SD$$anonymous$$
             // ...
         }
         else {
             Debug.Log("Failed to Initialize the Facebook SD$$anonymous$$");
         }
     }
 
     private void OnHideUnity(bool isGameShown)
     {
         if (!isGameShown)
         {
             // Pause the game - we will need to hide
             Time.timeScale = 0;
         }
         else {
             // Resume the game - we're getting focus again
             Time.timeScale = 1;
         }
     }
 
     public void ShareFb()
     {
         StartCoroutine(TakeScreenshot());
     }
 
     private IEnumerator TakeScreenshot()
     {
         yield return new WaitForEndOfFrame();
 
         var width = Screen.width;
         var height = Screen.height;
         var tex = new Texture2D(width, height, TextureFormat.RGB24, false);
         // Read screen contents into the texture
         tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
         tex.Apply();
         byte[] screenshot = tex.EncodeToPNG();
 
         var wwwForm = new WWWForm();
         wwwForm.AddBinaryData("image", screenshot, "Screenshot.png");
  
         FB.API("me/photos", Http$$anonymous$$ethod.POST, APICallback, wwwForm);
     }
 
     void APICallback(IGraphResult result)
     {
         if (result.Error != null)
             lastResponse = "Error Response:\n" + result.Error;
         else if (!FB.IsLoggedIn)
             lastResponse = "Login cancelled by Player";
         else 
             lastResponse = "Login was successful!";
     }
         
 }

Also I defined all the Andoid build Facebook settings within Unity.

Any Idea why there is no reaction when I click the button?

avatar image Houseman · Aug 02, 2016 at 04:11 PM 0
Share

Hi All / @brianturner,

The code above still is not working. There is no reaction when I push the button. I see nothing happening. Has anyone an idea why it is not working?

avatar image steakpinball Houseman · Aug 09, 2016 at 08:45 PM 0
Share

I'm not sure why it wouldn't work. Is there anything in the logs reported by logcat? Does saving the screenshot to the gallery work?

avatar image Switch_Z Houseman · May 17, 2018 at 07:28 PM 0
Share

Do you have the "publish_actions" permission properly authorized by Facebook.?

avatar image Suwas93 · Feb 20, 2018 at 06:56 PM 0
Share

Hey @Houseman Did you get that working?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to open Facebook Share Dialog from Unity in Android device? 0 Answers

Social sharing on Standalone build 0 Answers

Problem when take a screenshot and share to Facebook 1 Answer

Share Screenshot Image to Facebook with Facebook SDK 0 Answers

Facebook app review for screenshot share 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges