Edited: I build this game for Android and Window Phone platfomr.
Hello guys,
In my game, when player die, there is a stat board appear with a “Share” button, what I want is when player hit the button, it capture game’s screenshot and then share to Facebook. For easily understand, It work exactly like the “Share” button in Flappy Bird game.
Here is my code, I tested with a Debug.log(“done”) at the end of the TakeScreenshot() function and when I hit the button, I got the result: there is a word “done” in console window, the game is stopped just a bit of second (I think it’s has captured the screenshot) but nothing appeared.
if (// share button)
callFB();
void callFB () {
if (!FB.IsLoggedIn) {
FB.Login("email,publish_actions", LoginCallback);
}
else StartCoroutine(TakeScreenshot());
}
void LoginCallback(FBResult result) {
FbDebug.Log("LoginCallback");
if (FB.IsLoggedIn) {
OnLoggedIn();
}
}
void OnLoggedIn() {
FbDebug.Log("Logged in. ID: " + FB.UserId);
}
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, "InteractiveConsole.png");
wwwForm.AddField("message", "herp derp. I did a thing! Did I do this right?");
FB.API("me/photos", Facebook.HttpMethod.POST, Callback, wwwForm);
Debug.Log("done");
}
private Texture2D lastResponseTexture;
private string lastResponse = "";
public string ApiQuery = "";
void Callback(FBResult result)
{
lastResponseTexture = null;
if (result.Error != null)
lastResponse = "Error Response:
" + result.Error;
else if (!ApiQuery.Contains(“/picture”))
lastResponse = "Success Response:
" + result.Text;
else
{
lastResponseTexture = result.Texture;
lastResponse = "Success Response:
";
}
}