user2786465
user2786465

Reputation: 1

Posting screenshot through Facebook SDK for Unity

I have a problem with posting screenshot to FB. Connection is not the problem because I collect, deserialize and show my fb data. Post score and message to friend work fine but I cannot manage to post a shot from my phone. I am using Eclipse for debugging but I don't get any errors. My code look like this: `

IEnumerator TakeScreenShot()
{    
        //coroutine because I want to wait until end of the frame for shot
   yield return new WaitForEndOfFrame();
    //create new texture that will be my screenshot
      Texture2D tex = new Texture2D(Screen.width,Screen.height,TextureFormat.RGB24, false); 
        // read pixels from screen
       tex.ReadPixels(new Rect(0,0,Screen.width,Screen.height),0,0); 
        //and apply them to texture
      tex.Apply(); 
        // now we have screenshot
        // now we need to encode texture to array of bytes
       byte[] buffer = tex.EncodeToPNG();
    //so we can save it to storage 
    // System.IO.File.WriteAllBytes(Application.dataPath + "/screenshots/screen/screen" + ".png", buffer);  
        //or to convert it to string for posting to facebook
       string s = Convert.ToBase64String(buffer);
        //????? maybe my string format is not correct
       var query = new Dictionary<string, string>();
       query["photos"] = s;
       FB.API("me?fields=photos",Facebook.HttpMethod.POST, delegate (string r) { ;     }, query);
        }    

` when I go to GraphExplorer and paste command "me?fields=photos" I get nothing. It means function did nothing. I forgot to say that i granted permission for user_photos. This is frustrating. I have lost three days on problem that looked trivial, and yet no solution in sight. I will appreciate any suggestions.

Upvotes: 0

Views: 2715

Answers (1)

Brian Jew
Brian Jew

Reputation: 906

Right now there's no easy way to handle this through FB.API since it takes in a Dictionary<string, string>.

However FB.API() is really just a wrapper around Unity's WWW class. You can call the graph endpoints at graph.facebook.com and pass in the byte[] buffer = tex.EncodeToPNG() as a param in WWWForm().

UPDATE: FB.API() has been updated in version 4.3.3 to support WWWForm as well. You can now upload photos via that method now.

Upvotes: 1

Related Questions