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:
" + 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:
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.
